pub trait CacheEngine<K, V>{
// Required methods
fn get<Q>(&self, key: &Q, context: &ThreadContext) -> Option<Ref<K, V>>
where Key<K>: Borrow<Q>,
Q: Eq + Hash + ?Sized;
fn insert(
&self,
entry: Entry<K, V>,
context: &ThreadContext,
quota: &mut RequestQuota,
);
fn remove<Q>(&self, key: &Q, context: &ThreadContext) -> bool
where Key<K>: Borrow<Q>,
Q: Eq + Hash + ?Sized;
fn capacity(&self) -> usize;
fn metrics(&self) -> MetricsSnapshot;
}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, context: &ThreadContext) -> Option<Ref<K, V>>
fn get<Q>(&self, key: &Q, context: &ThreadContext) -> 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(
&self,
entry: Entry<K, V>,
context: &ThreadContext,
quota: &mut RequestQuota,
)
fn insert( &self, entry: Entry<K, V>, context: &ThreadContext, quota: &mut RequestQuota, )
Inserts a key-value pair into the cache.
This is a convenience wrapper around [insert_with] that always
admits the new entry. If the cache is full, an existing entry
is evicted based on the engine’s internal policy.
Sourcefn remove<Q>(&self, key: &Q, context: &ThreadContext) -> bool
fn remove<Q>(&self, key: &Q, context: &ThreadContext) -> 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.
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.