Struct Lru

Source
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 capacity entries

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

Source

pub fn new(capacity: NonZeroUsize) -> Self

Creates a new, empty LRU cache with the specified capacity.

Source

pub fn clear(&mut self)

Removes all entries from the cache.

Source

pub fn peek(&self, key: &Key) -> Option<&Value>

Returns a reference to the value without updating its position in the cache.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn pop(&mut self) -> Option<(Key, Value)>

Removes and returns the oldest (least recently used) entry.

Source

pub fn remove(&mut self, key: &Key) -> Option<Value>

Removes a specific entry from the cache. Returns the value if the key was present.

Source

pub fn is_empty(&self) -> bool

Returns true if the cache contains no entries.

Source

pub fn len(&self) -> usize

Returns the number of entries in the cache.

Source

pub fn capacity(&self) -> usize

Returns the maximum number of entries the cache can hold.

Source

pub fn retain(&mut self, f: impl FnMut(&Key, &mut Value) -> bool)

Retains only the entries for which the predicate returns true.

Source

pub fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = (Key, Value)>,

Extends the cache with key-value pairs from an iterator.

Source

pub fn shrink_to_fit(&mut self)

Shrinks the internal storage to fit the current number of entries.

Trait Implementations§

Source§

impl<Key: Debug, Value: Debug> Debug for Lru<Key, Value>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Key: Hash + Eq, Value> FromIterator<(Key, Value)> for Lru<Key, Value>

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = (Key, Value)>,

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<Key, Value> Freeze for Lru<Key, Value>

§

impl<Key, Value> RefUnwindSafe for Lru<Key, Value>
where Key: RefUnwindSafe, Value: RefUnwindSafe,

§

impl<Key, Value> Send for Lru<Key, Value>
where Key: Send, Value: Send,

§

impl<Key, Value> Sync for Lru<Key, Value>
where Key: Sync, Value: Sync,

§

impl<Key, Value> Unpin for Lru<Key, Value>
where Key: Unpin, Value: Unpin,

§

impl<Key, Value> UnwindSafe for Lru<Key, Value>
where Key: UnwindSafe, Value: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.