pub trait Lifecycle: Send + Sync {
// Required method
fn on_evict(&self, key: &dyn Any);
// Provided methods
fn on_remove(&self, key: &dyn Any) { ... }
fn on_clear(&self, key: &dyn Any) { ... }
}Expand description
Hooks into the lifetime of cache items.
The functions should be small and very fast, otherwise the cache performance might be negatively affected. Lifecycle methods are called synchronously after releasing the shard lock.
§Type Erasure
Because the cache supports heterogeneous key types via type erasure, the lifecycle
methods receive type-erased &dyn Any references. Use downcast_ref to recover
the concrete types:
ⓘ
fn on_evict(&self, key: &dyn Any) {
if let Some(key) = key.downcast_ref::<MyKey>() {
// Handle eviction of MyKey
}
}Required Methods§
Provided Methods§
Implementations on Foreign Types§
Source§impl<L: Lifecycle> Lifecycle for Arc<L>
Lifecycle implementation that wraps an Arc<L> for shared ownership.
impl<L: Lifecycle> Lifecycle for Arc<L>
Lifecycle implementation that wraps an Arc<L> for shared ownership.
This is used internally when the cache needs to clone the lifecycle.