pub struct LruCache<K, V> { /* private fields */ }Expand description
An LRU cache with a fixed capacity and optional per-entry TTL.
§Type parameters
K: key type – must beEq + Hash.V: value type.
Implementations§
Source§impl<K, V> LruCache<K, V>
impl<K, V> LruCache<K, V>
Sourcepub fn new(cap: usize) -> Self
pub fn new(cap: usize) -> Self
Create a new LruCache with the given capacity.
A capacity of 0 is valid but every put will immediately evict the
newly inserted entry on the next insertion.
Sourcepub fn get(&mut self, key: &K) -> Option<&V>
pub fn get(&mut self, key: &K) -> Option<&V>
Return the value associated with key, moving it to the MRU position.
If the entry has expired, it is removed and None is returned.
Sourcepub fn put(&mut self, key: K, value: V) -> Option<V>
pub fn put(&mut self, key: K, value: V) -> Option<V>
Insert or update key -> value (no TTL), evicting the LRU entry if at capacity.
- If
keyalready exists: updates the value and promotes to MRU; no eviction. - If
keyis new and the cache is at capacity: evicts the LRU entry first.
Returns the evicted value (not the replaced value on key updates).
Sourcepub fn put_with_ttl(&mut self, key: K, value: V, ttl: Duration) -> Option<V>
pub fn put_with_ttl(&mut self, key: K, value: V, ttl: Duration) -> Option<V>
Insert or update key -> value with a TTL.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Return the number of entries currently in the cache (including unexpired).
Sourcepub fn contains(&self, key: &K) -> bool
pub fn contains(&self, key: &K) -> bool
Return true if key is present and not expired (without promoting).
Sourcepub fn peek(&self, key: &K) -> Option<&V>
pub fn peek(&self, key: &K) -> Option<&V>
Read the value for key without promoting it to MRU.
Returns None if the key is not present or has expired.
Note: this method takes &self, so it cannot remove the expired entry;
the removal happens lazily on the next mutable access.
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 and not expired.
Sourcepub fn resize(&mut self, new_cap: usize)
pub fn resize(&mut self, new_cap: usize)
Dynamically resize the cache capacity.
If new_cap is smaller than the current length, LRU entries are
evicted until the length fits.
Sourcepub fn iter(&self) -> impl Iterator<Item = (&K, &V)>
pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>
Return an iterator over (&K, &V) pairs in LRU-to-MRU order
(front to back, oldest to newest).
Expired entries are included in the iterator; callers should check expiry if needed.
Sourcepub fn entry(&mut self, key: K) -> Entry<'_, K, V>where
K: Clone,
pub fn entry(&mut self, key: K) -> Entry<'_, K, V>where
K: Clone,
Return an entry handle for key.
Entry::Occupiedif the key is present (and not expired).Entry::Vacantotherwise.
The entry API mirrors HashMap::entry, enabling efficient
insert-or-modify patterns without double hash lookups in most cases.
Trait Implementations§
Source§impl<K, V> Cache<K, V> for LruCache<K, V>
impl<K, V> Cache<K, V> for LruCache<K, V>
Source§fn get(&mut self, key: &K) -> Option<&V>
fn get(&mut self, key: &K) -> Option<&V>
key, returning a reference to the value if present. Read moreSource§fn 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>
Source§fn remove(&mut self, key: &K) -> Option<V>
fn remove(&mut self, key: &K) -> Option<V>
key, returning its value if present. Read moreSource§fn peek(&self, key: &K) -> Option<&V>
fn peek(&self, key: &K) -> Option<&V>
key without updating access metadata (no promotion). Read more