Skip to main content

LruCache

Struct LruCache 

Source
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 implement Eq + Hash + Clone.
  • V – value type.

Implementations§

Source§

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

Source

pub fn new(capacity: usize) -> Self

Create a new LruCache with the given entry capacity.

Source

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.

Source

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).

Source

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.

Source

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.

Source

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.

Source

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

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

Source

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

Returns true if the cache contains an entry for key.

Source

pub fn len(&self) -> usize

Returns the number of entries currently resident in the cache.

Source

pub fn is_empty(&self) -> bool

Returns true when the cache has no entries.

Source

pub fn stats(&self) -> CacheStats

Return a snapshot of cache statistics.

Source

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

Return a shared reference to the value for key without updating LRU order or statistics.

Source

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.

Source

pub fn set_default_ttl(&mut self, ttl: Option<Duration>)

Set the default TTL for newly inserted entries.

Source

pub fn purge_expired(&mut self) -> usize

Eagerly purge all expired entries. Returns the number of entries removed.

Source

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.

Source

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.

Source

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

Returns true if the entry for key is pinned.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn pinned_count(&self) -> usize

Return the number of pinned entries.

Source

pub fn unpin_all(&mut self) -> usize

Unpin all currently pinned entries. Returns the number of entries unpinned.

Source

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.

Source

pub fn capacity(&self) -> usize

Return the current capacity (maximum number of entries).

Source

pub fn keys(&self) -> Vec<K>

Return a vector of all keys currently in the cache (in no particular order).

Source

pub fn clear(&mut self)

Remove all entries from the cache, resetting statistics.

Source

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.

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>
where K: Unpin, V: Unpin,

§

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

§

impl<K, V> UnwindSafe for LruCache<K, V>
where K: UnwindSafe, V: 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.