Skip to main content

LruCache

Struct LruCache 

Source
pub struct LruCache<K, V> { /* private fields */ }
Available on crate feature std only.
Expand description

A bounded, thread-safe LRU cache.

On insert overflow the least-recently-accessed entry is evicted. Both get and insert count as accesses and promote the affected entry to most-recently-used.

§Implementation

Sharded into up to 16 independent arenas keyed by hash of K. Each shard owns its own doubly-linked list, free-list, and HashMap, with its own Mutex<Inner>. Contention on the lock is bounded by the number of threads routing into the same shard, not by total cache traffic.

Eviction is approximate. Once the cache uses more than one shard, insert overflow evicts the local-to-shard least-recently-used entry, not the global one. Caches with fewer than 32 entries automatically use a single shard and retain strict global LRU ordering — this keeps small caches and test fixtures deterministic.

§Example

use cache_mod::{Cache, LruCache};

let cache: LruCache<u32, &'static str> = LruCache::new(2).expect("capacity > 0");

cache.insert(1, "one");
cache.insert(2, "two");
assert_eq!(cache.get(&1), Some("one")); // 1 is now MRU, 2 is LRU

cache.insert(3, "three"); // evicts 2 (single shard at this size)
assert_eq!(cache.get(&2), None);
assert_eq!(cache.get(&1), Some("one"));
assert_eq!(cache.get(&3), Some("three"));

Implementations§

Source§

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

Source

pub fn new(capacity: usize) -> Result<Self, CacheError>

Creates a cache with the given capacity.

Returns CacheError::InvalidCapacity if capacity == 0.

§Example
use cache_mod::LruCache;

let cache: LruCache<String, u32> = LruCache::new(128).expect("capacity > 0");
Source

pub fn with_capacity(capacity: NonZeroUsize) -> Self

Creates a cache with the given non-zero capacity. Infallible.

§Example
use std::num::NonZeroUsize;
use cache_mod::LruCache;

let cap = NonZeroUsize::new(64).expect("64 != 0");
let cache: LruCache<String, u32> = LruCache::with_capacity(cap);

Trait Implementations§

Source§

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

Source§

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

Returns the value associated with key, if any, and counts as an access for the purposes of the eviction policy.
Source§

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

Inserts value under key. Returns the previously-stored value if key was already present. Read more
Source§

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

Removes the entry for key and returns the value if present.
Source§

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

Returns true if the cache currently holds an entry for key. Read more
Source§

fn len(&self) -> usize

Number of entries currently stored. Read more
Source§

fn clear(&self)

Removes every entry. Capacity is preserved. Read more
Source§

fn capacity(&self) -> usize

Configured capacity bound. Read more
Source§

fn is_empty(&self) -> bool

Returns true when the cache holds no entries.

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: Send, V: Send,

§

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