Lifecycle

Trait 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 is_pinned(&self, key: &Key, val: &Val) -> bool { ... }
    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 is_pinned(&self, key: &Key, val: &Val) -> bool

Returns whether the item is pinned. Items that are pinned can’t be evicted. Note that a pinned item can still be replaced with get_mut, insert, replace and similar APIs.

Compared to zero (0) weight items, pinned items still consume (non-zero) weight even if they can’t be evicted. Furthermore, zero (0) weight items are separated from the other entries, which allows having a large number of them without impacting performance, but moving them in/out or the evictable section has a small cost. Pinning on the other hand doesn’t separate entries, so during eviction the cache may visit pinned entries but will ignore them.

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 item weight becomes zero (0) it will be left in the cache, otherwise it’ll still be removed. Zero (0) weight items aren’t evictable and are kept separated from the other items so it’s possible to have a large number of them without negatively affecting eviction performance.

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>