Trait light_cache::policy::Policy

source ·
pub trait Policy<K, V>: Clone {
    type Inner: Prune<K, V, Self>;

    // Required methods
    fn lock_inner(&self) -> MutexGuard<'_, Self::Inner>;
    fn get<S: BuildHasher>(
        &self,
        key: &K,
        cache: &LightCache<K, V, S, Self>,
    ) -> Option<V>;
    fn insert<S: BuildHasher>(
        &self,
        key: K,
        value: V,
        cache: &LightCache<K, V, S, Self>,
    ) -> Option<V>;
    fn remove<S: BuildHasher>(
        &self,
        key: &K,
        cache: &LightCache<K, V, S, Self>,
    ) -> Option<V>;

    // Provided method
    fn prune<S: BuildHasher>(&self, cache: &LightCache<K, V, S, Self>) { ... }
}
Expand description

A Policy augments accsess to a LightCache instance, managing the entry and eviction of items in the cache.

A policy usally requires shared mutable state, therefore the Policy::Inner type is used to represent this.

Required Associated Types§

source

type Inner: Prune<K, V, Self>

The inner type of this policy, likely behind a lock

Required Methods§

source

fn lock_inner(&self) -> MutexGuard<'_, Self::Inner>

§Panics

This method will panic if the lock is poisoned

source

fn get<S: BuildHasher>( &self, key: &K, cache: &LightCache<K, V, S, Self>, ) -> Option<V>

source

fn insert<S: BuildHasher>( &self, key: K, value: V, cache: &LightCache<K, V, S, Self>, ) -> Option<V>

source

fn remove<S: BuildHasher>( &self, key: &K, cache: &LightCache<K, V, S, Self>, ) -> Option<V>

Provided Methods§

source

fn prune<S: BuildHasher>(&self, cache: &LightCache<K, V, S, Self>)

§Warning:

Calling this method while holding a lock from Policy::lock_inner will cause a deadlock

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<K, V> Policy<K, V> for NoopPolicy
where K: Eq + Hash + Copy, V: Clone + Sync,

§

type Inner = ()

source§

impl<K, V> Policy<K, V> for TtlPolicy<K, V>
where K: Copy + Eq + Hash, V: Clone + Sync,