Skip to main content

LruCache

Struct LruCache 

Source
pub struct LruCache<K, V>
where K: Clone + Eq + Hash,
{ /* private fields */ }
Expand description

A fixed-capacity LRU cache.

The cache holds at most capacity entries. When a put would exceed the capacity, the least-recently-used entry is evicted. get updates the recency so the just-read entry becomes the most- recently-used.

All operations are O(1) amortized (put, get, remove, contains) except iter, which is O(n).

§Capacity edge cases

  • capacity = 0 - the cache accepts no entries. Both put and get behave as no-ops (well, get still evicts because there’s nothing to evict; put silently drops the entry).
  • capacity = 1 - the cache holds exactly one entry. Every put evicts the previous entry.

§Lombok New derivation

#[derive(New)] generates LruCache::new(capacity) — the map and order fields are skipped with #[new(skip)] so Lombok falls back to <HashMap as Default>::default() and <VecDeque as Default>::default() (which both call new() internally), preserving the canonical single-argument call site.

Implementations§

Source§

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

Source

pub fn len(&self) -> usize

Returns the current number of entries in the cache.

§Returns
  • usize - The number of items in the collection.
Source

pub fn is_empty(&self) -> bool

Returns true if the cache is empty.

§Returns
  • bool - true when the collection is empty.
Source

pub fn is_full(&self) -> bool

Returns true if the cache is at capacity.

§Returns
  • bool - A boolean.
Source

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

Returns true if the cache contains a value for the given key. Does NOT update the recency (use get for that).

§Arguments
  • &K - Shared reference to a K.
§Returns
  • bool - A boolean.
Source

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

Returns the value for the given key, updating the recency so the entry becomes the most-recently- used.

Returns None if the key is not in the cache.

§Arguments
  • &K - Shared reference to a K.
§Returns
  • Option<V> - The current value (or a snapshot thereof).
Source

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

Returns the value for the given key without updating the recency. Useful for “is this cached?” checks that should not affect eviction order.

§Arguments
  • &K - Shared reference to a K.
§Returns
  • Option<V> - Some(...) on success, None otherwise.
Source

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

Inserts a key-value pair into the cache. If the key is already present, the existing value is replaced (and the entry becomes the most- recently-used). If the cache is at capacity and the key is new, the least-recently-used entry is evicted first.

Returns the evicted entry, if any.

§Arguments
  • K: Clone + Eq + Hash - A generic type parameter.
  • V - A V parameter.
§Returns
  • Option<(K, V)> - Some(...) on success, None otherwise.
Source

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

Removes the entry for the given key, returning the removed value if any.

§Arguments
  • &K - Shared reference to a K.
§Returns
  • Option<V> - Some(...) on success, None otherwise.
Source

pub fn clear(&mut self)

Removes every entry from the cache.

Source

pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>

Returns an iterator over the entries in most-recently-used-first order.

§Returns
  • impl Iterator<Item - A impl Iterator<Item value.
Source

pub fn keys(&self) -> impl Iterator<Item = &K>

Returns an iterator over the keys in most-recently-used-first order.

§Returns
  • impl Iterator<Item - A impl Iterator<Item value.
Source

pub fn values(&self) -> impl Iterator<Item = &V>

Returns an iterator over the values in most-recently-used-first order.

§Returns
  • impl Iterator<Item - A impl Iterator<Item value.
Source

pub fn resize(&mut self, new_capacity: usize)

Resizes the cache to a new capacity.

If the new capacity is smaller than the current size, the least-recently-used entries are evicted until the cache fits.

§Arguments
  • usize - A non-negative integer (usize).
Source§

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

Source

pub fn get_mut_capacity(&mut self) -> &mut usize

Source

pub fn set_capacity(&mut self, val: usize) -> &mut Self

Source

pub fn get_map(&self) -> &HashMap<K, V>

Source

pub fn get_mut_map(&mut self) -> &mut HashMap<K, V>

Source

pub fn set_map(&mut self, val: HashMap<K, V>) -> &mut Self

Source

pub fn get_order(&self) -> &VecDeque<K>

Source

pub fn get_mut_order(&mut self) -> &mut VecDeque<K>

Source

pub fn set_order(&mut self, val: VecDeque<K>) -> &mut Self

Source§

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

Source

pub fn new(capacity: usize) -> Self

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> LruCache<K, V>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K, V> Freeze for LruCache<K, V>
where HashMap<K, V>: Freeze, VecDeque<K>: Freeze,

§

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

§

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

§

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

§

impl<K, V> Unpin for LruCache<K, V>
where HashMap<K, V>: Unpin, VecDeque<K>: Unpin,

§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more