pub struct Cache<K, V>{ /* private fields */ }Expand description
A thread-safe in-memory LRU cache with TTL and tag-based invalidation.
Implementations§
Source§impl<K, V> Cache<K, V>
impl<K, V> Cache<K, V>
Sourcepub fn new(max_size: usize, default_ttl: Option<Duration>) -> Self
pub fn new(max_size: usize, default_ttl: Option<Duration>) -> Self
Create a new cache with the given max size and optional default TTL.
Sourcepub fn set_with(&self, key: K, value: V, ttl: Option<Duration>, tags: &[&str])
pub fn set_with(&self, key: K, value: V, ttl: Option<Duration>, tags: &[&str])
Set a value with custom TTL and tags.
Sourcepub fn get(&self, key: &K) -> Option<V>where
V: Clone,
pub fn get(&self, key: &K) -> Option<V>where
V: Clone,
Get a value from the cache. Returns None if not found or expired.
Increments the hit counter on success, or the miss counter on failure.
Sourcepub fn invalidate_by_tag(&self, tag: &str) -> usize
pub fn invalidate_by_tag(&self, tag: &str) -> usize
Invalidate all entries with the given tag. Returns count removed.
Sourcepub fn remove_expired(&self) -> usize
pub fn remove_expired(&self) -> usize
Remove all expired entries from the cache. Returns the number of entries removed.
Sourcepub fn get_or_insert_with<F>(&self, key: K, f: F) -> V
pub fn get_or_insert_with<F>(&self, key: K, f: F) -> V
Get a value from the cache, or insert one computed by the given closure if absent or expired.
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Return a snapshot of cache performance counters.
Sourcepub fn get_many(&self, keys: &[K]) -> HashMap<K, V>where
V: Clone,
pub fn get_many(&self, keys: &[K]) -> HashMap<K, V>where
V: Clone,
Retrieve multiple values at once. Returns a map of keys to their cached values, omitting any keys that are absent or expired.
Sourcepub fn delete_where<F>(&self, predicate: F) -> usize
pub fn delete_where<F>(&self, predicate: F) -> usize
Delete all entries for which the predicate returns true.
Returns the number of entries removed.