pub trait Lifecycle<Key, Val> {
type RequestState;
// Required method
fn begin_request(&self) -> Self::RequestState;
// 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 on_evict(&self, state: &mut Self::RequestState, key: Key, val: Val) { ... }
fn on_evict_cold(&self, state: &mut Self::RequestState, key: Key, val: Val) { ... }
fn on_evict_hot(&self, state: &mut Self::RequestState, key: Key, val: 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§
type RequestState
Required Methods§
Sourcefn begin_request(&self) -> Self::RequestState
fn begin_request(&self) -> Self::RequestState
Called before the insert request starts, e.g.: insert, replace.
Provided Methods§
Sourcefn is_pinned(&self, key: &Key, val: &Val) -> bool
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.
Sourcefn before_evict(&self, state: &mut Self::RequestState, key: &Key, val: &mut Val)
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.
Sourcefn on_evict(&self, state: &mut Self::RequestState, key: Key, val: Val)
fn on_evict(&self, state: &mut Self::RequestState, key: Key, val: Val)
Called when an item is evicted.
To distinguish evictions from the hot vs cold queues, override
Lifecycle::on_evict_hot and/or Lifecycle::on_evict_cold instead;
they default to delegating here.
If none of on_evict, on_evict_hot, or on_evict_cold is overridden,
eviction notifications are silently dropped.
Note: items that are rejected without ever being admitted to the cache
(oversized inserts and oversized placeholder values) are routed through
Lifecycle::on_evict_cold, which by default reaches this method.
Sourcefn on_evict_cold(&self, state: &mut Self::RequestState, key: Key, val: Val)
fn on_evict_cold(&self, state: &mut Self::RequestState, key: Key, val: Val)
Called when an item is evicted from the cold queue.
By default delegates to Lifecycle::on_evict.
Note: items that are rejected without ever being admitted to the cache (oversized inserts and oversized placeholder values) are also reported via this method.
Sourcefn on_evict_hot(&self, state: &mut Self::RequestState, key: Key, val: Val)
fn on_evict_hot(&self, state: &mut Self::RequestState, key: Key, val: Val)
Called when an item is evicted from the hot queue.
By default delegates to Lifecycle::on_evict.
Note: rejected (never-admitted) items are reported via
Lifecycle::on_evict_cold, not this method.
Sourcefn end_request(&self, state: Self::RequestState)
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.