pub struct Lru<Key, Value> { /* private fields */ }Expand description
A least-recently-used (LRU) cache.
The cache maintains at most capacity entries. When inserting into a full
cache, the least recently used entry is automatically evicted. All
operations that access values (get, insert, get_or_insert_with) update the
entry’s position in the LRU order. Operations that only read without
accessing (peek, contains_key, oldest) do not affect LRU ordering.
§Time Complexity
- Insert/Get/Remove: O(log n) average, O(n) worst case
- Peek/Contains: O(1) average, O(n) worst case
- Pop (oldest): O(log n)
- Clear: O(1)
§Space Complexity
- O(
capacity) memory usage - Pre-allocates space for
capacityentries
§Examples
use std::num::NonZeroUsize;
use evictor::Lru;
let mut cache = Lru::<i32, String>::new(NonZeroUsize::new(3).unwrap());
cache.insert(1, "one".to_string());
cache.insert(2, "two".to_string());
cache.insert(3, "three".to_string());
assert_eq!(cache.len(), 3);
cache.get(&1);
cache.insert(4, "four".to_string());
assert!(!cache.contains_key(&2));
cache.peek(&3);
let (oldest_key, _) = cache.oldest().unwrap();
assert_eq!(oldest_key, &3);Implementations§
Source§impl<Key: Hash + Eq, Value> Lru<Key, Value>
impl<Key: Hash + Eq, Value> Lru<Key, Value>
Sourcepub fn new(capacity: NonZeroUsize) -> Self
pub fn new(capacity: NonZeroUsize) -> Self
Creates a new, empty LRU cache with the specified capacity.
Sourcepub fn peek(&self, key: &Key) -> Option<&Value>
pub fn peek(&self, key: &Key) -> Option<&Value>
Returns a reference to the value without updating its position in the cache.
Sourcepub fn oldest(&self) -> Option<(&Key, &Value)>
pub fn oldest(&self) -> Option<(&Key, &Value)>
Returns a reference to the oldest (least recently used) entry. This does not update the entry’s position in the cache.
Sourcepub fn contains_key(&self, key: &Key) -> bool
pub fn contains_key(&self, key: &Key) -> bool
Returns true if the cache contains the given key. This does not update the entry’s position in the cache.
Sourcepub fn get_or_insert_with(
&mut self,
key: Key,
or_insert: impl FnOnce(&Key) -> Value,
) -> &Value
pub fn get_or_insert_with( &mut self, key: Key, or_insert: impl FnOnce(&Key) -> Value, ) -> &Value
Gets the value for a key, or inserts it using the provided function. Returns an immutable reference to existing value (if found) or the newly inserted value. If the cache is full, the least recently used entry is removed on insertion.
Sourcepub fn get_or_insert_with_mut(
&mut self,
key: Key,
or_insert: impl FnOnce(&Key) -> Value,
) -> &mut Value
pub fn get_or_insert_with_mut( &mut self, key: Key, or_insert: impl FnOnce(&Key) -> Value, ) -> &mut Value
Gets the value for a key, or inserts it using the provided function. Returns a mutable reference to the existing value (if found) or the newly inserted value. If the cache is full, the least recently used entry is removed on insertion.
Sourcepub fn insert(&mut self, key: Key, value: Value) -> &Value
pub fn insert(&mut self, key: Key, value: Value) -> &Value
Inserts a key-value pair into the cache. Returns an immutable reference to the inserted value. If the cache is full, the least recently used entry is removed.
Sourcepub fn insert_mut(&mut self, key: Key, value: Value) -> &mut Value
pub fn insert_mut(&mut self, key: Key, value: Value) -> &mut Value
Inserts a key-value pair into the cache. Returns a mutable reference to the inserted value. If the cache is full, the least recently used entry is removed.
Sourcepub fn get(&mut self, key: &Key) -> Option<&Value>
pub fn get(&mut self, key: &Key) -> Option<&Value>
Gets a value from the cache, marking it as recently used. Returns an immutable reference to the value if found.
Sourcepub fn get_mut(&mut self, key: &Key) -> Option<&mut Value>
pub fn get_mut(&mut self, key: &Key) -> Option<&mut Value>
Gets a value from the cache, marking it as recently used. Returns a mutable reference to the value if found.
Sourcepub fn pop(&mut self) -> Option<(Key, Value)>
pub fn pop(&mut self) -> Option<(Key, Value)>
Removes and returns the oldest (least recently used) entry.
Sourcepub fn remove(&mut self, key: &Key) -> Option<Value>
pub fn remove(&mut self, key: &Key) -> Option<Value>
Removes a specific entry from the cache. Returns the value if the key was present.
Sourcepub fn retain(&mut self, f: impl FnMut(&Key, &mut Value) -> bool)
pub fn retain(&mut self, f: impl FnMut(&Key, &mut Value) -> bool)
Retains only the entries for which the predicate returns true.
Sourcepub fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = (Key, Value)>,
pub fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = (Key, Value)>,
Extends the cache with key-value pairs from an iterator.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the internal storage to fit the current number of entries.