pub trait Cache<K, V> {
Show 14 methods
// Required methods
fn get(&mut self, key: &K) -> Option<&V>;
fn put(&mut self, key: K, value: V) -> Option<V>;
fn len(&self) -> usize;
fn cap(&self) -> usize;
fn remove(&mut self, key: &K) -> Option<V>;
fn clear(&mut self);
fn peek(&self, key: &K) -> Option<&V>;
fn contains_key(&self, key: &K) -> bool;
fn resize(&mut self, new_cap: usize);
// Provided methods
fn put_with_ttl(&mut self, key: K, value: V, ttl: Duration) -> Option<V> { ... }
fn is_empty(&self) -> bool { ... }
fn get_or_insert(&mut self, key: K, default: impl FnOnce() -> V) -> &V
where K: Clone { ... }
fn values(&self) -> Vec<&V> { ... }
fn warm(&mut self, iter: impl IntoIterator<Item = (K, V)>) { ... }
}Expand description
Unified interface for key-value caches with bounded capacity.
All four implementations (LruCache, ArcCache, LfuCache,
WTinyLfuCache) implement this trait, allowing callers to write generic
code against the cache interface.
Required Methods§
Sourcefn get(&mut self, key: &K) -> Option<&V>
fn get(&mut self, key: &K) -> Option<&V>
Look up key, returning a reference to the value if present.
Implementations update internal bookkeeping (e.g. promoting the entry to MRU or incrementing frequency counts) as a side effect.
If the entry exists but has expired (TTL), it is removed and None is
returned without updating recency or frequency.
Sourcefn put(&mut self, key: K, value: V) -> Option<V>
fn put(&mut self, key: K, value: V) -> Option<V>
Insert or update key -> value without a TTL.
If inserting a new key would exceed the cache’s capacity, the implementation evicts one entry (per its policy) and returns the evicted value. On a key update, the old value is not returned.
Sourcefn remove(&mut self, key: &K) -> Option<V>
fn remove(&mut self, key: &K) -> Option<V>
Remove the entry associated with key, returning its value if present.
Unlike eviction, this is an explicit removal requested by the caller.
Sourcefn peek(&self, key: &K) -> Option<&V>
fn peek(&self, key: &K) -> Option<&V>
Look up key without updating access metadata (no promotion).
If the entry has expired (TTL), it is removed and None is returned.
Returns None if the key is not present or has expired.
Sourcefn contains_key(&self, key: &K) -> bool
fn contains_key(&self, key: &K) -> bool
Return true if key is present in the cache (without promotion).
Expired entries are treated as absent.
Provided Methods§
Sourcefn put_with_ttl(&mut self, key: K, value: V, ttl: Duration) -> Option<V>
fn put_with_ttl(&mut self, key: K, value: V, ttl: Duration) -> Option<V>
Insert or update key -> value with a time-to-live.
After ttl has elapsed, any access to key will treat it as a miss
and remove the entry lazily.
Concrete implementations in this crate override this to actually store
the expiry. External impls that don’t need TTL may rely on the default
which falls back to a plain put (no expiry).
Sourcefn get_or_insert(&mut self, key: K, default: impl FnOnce() -> V) -> &Vwhere
K: Clone,
fn get_or_insert(&mut self, key: K, default: impl FnOnce() -> V) -> &Vwhere
K: Clone,
Return &V for key, inserting default() if the key is absent.
The closure default is called at most once. If the key is already
present the closure is never invoked.
Implementations may override this for efficiency (e.g. to avoid a
second hash lookup). The default implementation uses Cache::peek
after ensuring the key exists.
Sourcefn values(&self) -> Vec<&V>
fn values(&self) -> Vec<&V>
Return all live (non-expired) values currently stored in the cache.
The default implementation returns an empty Vec. Concrete
implementations override this to return actual values.
Sourcefn warm(&mut self, iter: impl IntoIterator<Item = (K, V)>)
fn warm(&mut self, iter: impl IntoIterator<Item = (K, V)>)
Pre-populate the cache with (key, value) pairs from an iterator.
Each pair is inserted via Cache::put, respecting the cache’s
eviction policy. Pairs that exceed capacity cause earlier entries to
be evicted according to the policy.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".