pub trait CacheEngine<K, V>{
// Required methods
fn get<Q>(&self, key: &Q) -> Option<Ref<K, V>>
where Key<K>: Borrow<Q>,
Q: Eq + Hash + ?Sized;
fn insert_with<A>(
&self,
key: K,
value: V,
expired_at: Option<Instant>,
admission: A,
)
where A: Fn(&K, &K) -> bool;
fn remove<Q>(&self, key: &Q) -> bool
where Key<K>: Borrow<Q>,
Q: Eq + Hash + ?Sized;
fn capacity(&self) -> usize;
fn metrics(&self) -> MetricsSnapshot;
// Provided method
fn insert(&self, key: K, value: V, expired_at: Option<Instant>) { ... }
}Expand description
A thread-safe, fixed-capacity caching interface.
This trait abstracts the underlying storage and eviction mechanics, allowing callers to interact with different cache implementations through a consistent API.
§Type Parameters
Required Methods§
Sourcefn get<Q>(&self, key: &Q) -> Option<Ref<K, V>>
fn get<Q>(&self, key: &Q) -> Option<Ref<K, V>>
Retrieves a protected reference to a cached value.
Returns Some(Ref<K, V>) if the key exists and is not expired. Accessing
a key typically updates internal frequency metadata, which the eviction
algorithm uses to protect “hot” data from removal.
§Memory Safety
The returned Ref pins the entry in memory using an epoch guard.
The data will remain valid and allocated until the reference is dropped
and the global epoch advances.
Sourcefn insert_with<A>(
&self,
key: K,
value: V,
expired_at: Option<Instant>,
admission: A,
)
fn insert_with<A>( &self, key: K, value: V, expired_at: Option<Instant>, admission: A, )
Inserts a key-value pair with a custom admission policy.
When the cache is full, the admission closure is invoked with the
(incoming_key, potential_victim_key). If the closure returns false,
the insertion is aborted to preserve the current cache state.
§Guarantees
- Updates: If the key already exists, the value is updated and the admission policy is typically bypassed.
- Expiration: Expired entries in a target slot are evicted regardless of the admission policy.
Sourcefn remove<Q>(&self, key: &Q) -> bool
fn remove<Q>(&self, key: &Q) -> bool
Removes an entry from the cache.
Returns true if the entry was found and successfully marked for
removal.
§Consistency
This method performs an atomic “unlinking.” The entry is immediately
made unreachable for new get requests, while existing readers
can continue to access the data safely until their Ref is dropped.
Sourcefn metrics(&self) -> MetricsSnapshot
fn metrics(&self) -> MetricsSnapshot
Returns a snapshot of internal cache statistics.
Provided Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.