pub struct LruCache<K: Eq + Hash + Clone, V> { /* private fields */ }Expand description
High-performance LRU cache backed by an arena of Option<LruNode> slots.
§Type parameters
K– key type; must implementEq + Hash + Clone.V– value type.
Implementations§
Source§impl<K: Eq + Hash + Clone, V> LruCache<K, V>
impl<K: Eq + Hash + Clone, V> LruCache<K, V>
Sourcepub fn with_default_ttl(capacity: usize, ttl: Duration) -> Self
pub fn with_default_ttl(capacity: usize, ttl: Duration) -> Self
Create a new LruCache with a default TTL applied to all entries
unless overridden by insert_with_ttl.
Sourcepub fn get(&mut self, key: &K) -> Option<&V>
pub fn get(&mut self, key: &K) -> Option<&V>
Look up key, move it to the MRU head, and return a shared reference
to its value. Records a cache hit/miss in the statistics.
If the entry has a TTL and has expired, it is lazily evicted and None
is returned (counted as a miss).
Sourcepub fn insert(&mut self, key: K, value: V, size_bytes: usize)
pub fn insert(&mut self, key: K, value: V, size_bytes: usize)
Insert (key, value) into the cache.
- If the key already exists, its value is updated and it is promoted to the MRU head.
- If the cache is at capacity the LRU entry is evicted first.
The default TTL (if configured via with_default_ttl) is applied.
Sourcepub fn insert_with_ttl(
&mut self,
key: K,
value: V,
size_bytes: usize,
ttl: Duration,
)
pub fn insert_with_ttl( &mut self, key: K, value: V, size_bytes: usize, ttl: Duration, )
Insert (key, value) with an explicit TTL duration.
Overrides any default TTL configured on the cache.
Sourcepub fn insert_pinned(&mut self, key: K, value: V, size_bytes: usize)
pub fn insert_pinned(&mut self, key: K, value: V, size_bytes: usize)
Insert a pinned entry that will not be evicted by LRU eviction.
Pinned entries remain in the cache until explicitly removed via
remove or unpin + subsequent eviction.
Sourcepub fn remove(&mut self, key: &K) -> Option<V>
pub fn remove(&mut self, key: &K) -> Option<V>
Remove the entry for key, returning its value if present.
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Return a snapshot of cache statistics.
Sourcepub fn peek(&self, key: &K) -> Option<&V>
pub fn peek(&self, key: &K) -> Option<&V>
Return a shared reference to the value for key without updating LRU
order or statistics.
Sourcepub fn evict_lru(&mut self) -> Option<(K, V)>
pub fn evict_lru(&mut self) -> Option<(K, V)>
Manually evict the least-recently-used unpinned entry, returning
(key, value).
Pinned entries are skipped. Returns None if the cache is empty or
every entry is pinned.
Sourcepub fn set_default_ttl(&mut self, ttl: Option<Duration>)
pub fn set_default_ttl(&mut self, ttl: Option<Duration>)
Set the default TTL for newly inserted entries.
Sourcepub fn purge_expired(&mut self) -> usize
pub fn purge_expired(&mut self) -> usize
Eagerly purge all expired entries. Returns the number of entries removed.
Sourcepub fn pin(&mut self, key: &K) -> bool
pub fn pin(&mut self, key: &K) -> bool
Pin an existing entry so it cannot be evicted by LRU eviction.
Returns true if the entry was found and pinned.
Sourcepub fn unpin(&mut self, key: &K) -> bool
pub fn unpin(&mut self, key: &K) -> bool
Unpin an existing entry so it becomes eligible for LRU eviction again.
Returns true if the entry was found and unpinned.
Sourcepub fn refresh_ttl(&mut self, key: &K) -> bool
pub fn refresh_ttl(&mut self, key: &K) -> bool
Refresh the TTL of an existing entry, resetting its expiration clock.
If the entry has no TTL (neither explicit nor default), this is a no-op
and returns false. Returns true if the TTL was successfully
refreshed.
Sourcepub fn set_entry_ttl(&mut self, key: &K, ttl: Duration) -> bool
pub fn set_entry_ttl(&mut self, key: &K, ttl: Duration) -> bool
Set an explicit TTL on an existing entry, overriding any previous TTL.
Returns true if the entry was found and the TTL was set.
Sourcepub fn clear_entry_ttl(&mut self, key: &K) -> bool
pub fn clear_entry_ttl(&mut self, key: &K) -> bool
Remove the TTL from an existing entry, making it live indefinitely (until evicted by LRU or explicitly removed).
Returns true if the entry was found and TTL cleared.
Sourcepub fn remaining_ttl(&self, key: &K) -> Option<Duration>
pub fn remaining_ttl(&self, key: &K) -> Option<Duration>
Return the remaining TTL for the entry, or None if the entry does
not exist or has no TTL.
Sourcepub fn pinned_count(&self) -> usize
pub fn pinned_count(&self) -> usize
Return the number of pinned entries.
Sourcepub fn unpin_all(&mut self) -> usize
pub fn unpin_all(&mut self) -> usize
Unpin all currently pinned entries. Returns the number of entries unpinned.
Sourcepub fn resize(&mut self, new_capacity: usize) -> usize
pub fn resize(&mut self, new_capacity: usize) -> usize
Resize the cache to new_capacity, evicting excess entries if
necessary.
Returns the number of entries evicted.
Sourcepub fn keys(&self) -> Vec<K>
pub fn keys(&self) -> Vec<K>
Return a vector of all keys currently in the cache (in no particular order).
Sourcepub fn access_count(&self, key: &K) -> Option<u64>
pub fn access_count(&self, key: &K) -> Option<u64>
Return the access count for a specific entry, or None if the key
does not exist.