Skip to main content

Cache

Trait Cache 

Source
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§

Source

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.

Source

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.

Source

fn len(&self) -> usize

Return the number of live entries currently in the cache.

Source

fn cap(&self) -> usize

Return the maximum number of entries the cache can hold.

Source

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.

Source

fn clear(&mut self)

Remove all entries from the cache.

Source

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.

Source

fn contains_key(&self, key: &K) -> bool

Return true if key is present in the cache (without promotion).

Expired entries are treated as absent.

Source

fn resize(&mut self, new_cap: usize)

Dynamically resize the cache capacity.

If new_cap is smaller than the current length, excess entries are evicted according to the cache’s eviction policy.

Provided Methods§

Source

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).

Source

fn is_empty(&self) -> bool

Return true if the cache holds no entries.

Source

fn get_or_insert(&mut self, key: K, default: impl FnOnce() -> V) -> &V
where 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.

Source

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.

Source

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".

Implementors§

Source§

impl<C> Cache<Vec<u8>, Vec<u8>> for BoundedCache<C>
where C: Cache<Vec<u8>, Vec<u8>>,

Source§

impl<C> Cache<Vec<u8>, Vec<u8>> for StatsCache<C>
where C: Cache<Vec<u8>, Vec<u8>>,

Source§

impl<K, V> Cache<K, V> for ArcCache<K, V>
where K: Eq + Hash + Clone,

Source§

impl<K, V> Cache<K, V> for LfuCache<K, V>
where K: Eq + Hash + Clone,

Source§

impl<K, V> Cache<K, V> for LruCache<K, V>
where K: Eq + Hash,

Source§

impl<K, V> Cache<K, V> for WTinyLfuCache<K, V>
where K: Eq + Hash + Clone,