Skip to main content

LruCache

Struct LruCache 

Source
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 be Eq + Hash.
  • V: value type.

Implementations§

Source§

impl<K, V> LruCache<K, V>
where K: Eq + Hash,

Source

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.

Source

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.

Source

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 key already exists: updates the value and promotes to MRU; no eviction.
  • If key is new and the cache is at capacity: evicts the LRU entry first.

Returns the evicted value (not the replaced value on key updates).

Source

pub fn put_with_ttl(&mut self, key: K, value: V, ttl: Duration) -> Option<V>

Insert or update key -> value with a TTL.

Source

pub fn len(&self) -> usize

Return the number of entries currently in the cache (including unexpired).

Source

pub fn is_empty(&self) -> bool

Return true if the cache contains no entries.

Source

pub fn cap(&self) -> usize

Return the maximum capacity of the cache.

Source

pub fn contains(&self, key: &K) -> bool

Return true if key is present and not expired (without promoting).

Source

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.

Source

pub fn remove(&mut self, key: &K) -> Option<V>

Remove the entry for key, returning its value if present and not expired.

Source

pub fn clear(&mut self)

Remove all entries from the cache.

Source

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.

Source

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.

Source

pub fn entry(&mut self, key: K) -> Entry<'_, K, V>
where K: Clone,

Return an entry handle for key.

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>
where K: Eq + Hash,

Source§

fn get(&mut self, key: &K) -> Option<&V>

Look up key, returning a reference to the value if present. Read more
Source§

fn put(&mut self, key: K, value: V) -> Option<V>

Insert or update key -> value without a TTL. Read more
Source§

fn put_with_ttl(&mut self, key: K, value: V, ttl: Duration) -> Option<V>

Insert or update key -> value with a time-to-live. Read more
Source§

fn len(&self) -> usize

Return the number of live entries currently in the cache.
Source§

fn cap(&self) -> usize

Return the maximum number of entries the cache can hold.
Source§

fn remove(&mut self, key: &K) -> Option<V>

Remove the entry associated with key, returning its value if present. Read more
Source§

fn clear(&mut self)

Remove all entries from the cache.
Source§

fn peek(&self, key: &K) -> Option<&V>

Look up key without updating access metadata (no promotion). Read more
Source§

fn contains_key(&self, key: &K) -> bool

Return true if key is present in the cache (without promotion). Read more
Source§

fn resize(&mut self, new_cap: usize)

Dynamically resize the cache capacity. Read more
Source§

fn values(&self) -> Vec<&V>

Return all live (non-expired) values currently stored in the cache. Read more
Source§

fn is_empty(&self) -> bool

Return true if the cache holds no entries.
Source§

fn get_or_insert(&mut self, key: K, default: impl FnOnce() -> V) -> &V
where K: Clone,

Return &V for key, inserting default() if the key is absent. Read more
Source§

fn warm(&mut self, iter: impl IntoIterator<Item = (K, V)>)

Pre-populate the cache with (key, value) pairs from an iterator. Read more
Source§

impl<K: Debug, V> Debug for LruCache<K, V>

Source§

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

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

impl<K: Eq + Hash, V> From<Vec<(K, V)>> for LruCache<K, V>

Source§

fn from(pairs: Vec<(K, V)>) -> Self

Build an LruCache from a vector of (key, value) pairs.

The cache capacity is set to pairs.len().max(1) so that all pairs fit without eviction.

Auto Trait Implementations§

§

impl<K, V> Freeze for LruCache<K, V>

§

impl<K, V> RefUnwindSafe for LruCache<K, V>

§

impl<K, V> Send for LruCache<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for LruCache<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for LruCache<K, V>

§

impl<K, V> UnsafeUnpin for LruCache<K, V>

§

impl<K, V> UnwindSafe for LruCache<K, V>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.