Trait quick_cache::Lifecycle

source ·
pub trait Lifecycle<Key, Val> {
    type RequestState;

    // Required methods
    fn begin_request(&self) -> Self::RequestState;
    fn on_evict(&self, state: &mut Self::RequestState, key: Key, val: Val);

    // Provided methods
    fn before_evict(
        &self,
        state: &mut Self::RequestState,
        key: &Key,
        val: &mut Val,
    ) { ... }
    fn end_request(&self, state: Self::RequestState) { ... }
}
Expand description

Hooks into the lifetime of the cache items.

The functions should be small and very fast, otherwise the cache performance might be negatively affected.

Required Associated Types§

Required Methods§

source

fn begin_request(&self) -> Self::RequestState

Called before the insert request starts, e.g.: insert, replace.

source

fn on_evict(&self, state: &mut Self::RequestState, key: Key, val: Val)

Called when an item is evicted.

Provided Methods§

source

fn before_evict(&self, state: &mut Self::RequestState, key: &Key, val: &mut Val)

Called when a cache item is about to be evicted. Note that value replacement (e.g. insertions for the same key) won’t call this method.

This is the only time the item can change its weight. If the weight becomes 0 then the item will be left in the cache, otherwise it’ll still be removed.

source

fn end_request(&self, state: Self::RequestState)

Called after a request finishes, e.g.: insert, replace.

Notes: This will not be called when using _with_lifecycle apis, which will return the RequestState instead. This will not be called if the request errored (e.g. a replace didn’t find a value to replace). If needed, Drop for RequestState can be used to detect these cases.

Implementors§

source§

impl<Key, Val> Lifecycle<Key, Val> for quick_cache::sync::DefaultLifecycle<Key, Val>

source§

impl<Key, Val> Lifecycle<Key, Val> for quick_cache::unsync::DefaultLifecycle<Key, Val>