Struct LruCache

Source
pub struct LruCache<K, V, S = DefaultHashBuilder> { /* private fields */ }
Expand description

A least-recently-used (LRU) Cache with lazy eviction. Each entry maintains an associated ordinal value representing when the entry was last accessed. The cache is allowed to grow up to 2 times specified capacity with no evictions, at which point, the excess entries are evicted based on LRU policy resulting in an amortized O(1) performance.

Implementations§

Source§

impl<K, V> LruCache<K, V, DefaultHashBuilder>

Source

pub fn new(capacity: usize) -> Self

Source§

impl<K, V, S> LruCache<K, V, S>

Source

pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self

Source

pub fn hasher(&self) -> &S

Source§

impl<K, V, S> LruCache<K, V, S>

Source

pub fn iter(&self) -> Iter<'_, K, V>

An iterator visiting all key-value pairs in arbitrary order. The iterator element type is (&'a K, &'a V).

Source

pub fn iter_mut(&mut self) -> IterMut<'_, K, V>

An iterator visiting all key-value pairs in arbitrary order, with mutable references to the values. The iterator element type is (&'a K, &'a mut V).

Source§

impl<K: Eq + Hash + PartialEq, V, S: BuildHasher> LruCache<K, V, S>

Source

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

Inserts a key-value pair into the cache. If the cache not have this key present, None is returned. If the cache did have this key present, the value is updated, and the old value is returned. The key is not updated, though; this matters for types that can be == without being identical.

Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns true if the cache contains a value for the specified key. Unlike self.get(key).is_some(), this method does not update the LRU ordinal value associated with the entry.

The key may be any borrowed form of the cache’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Source

pub fn get<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a reference to the value corresponding to the key. Updates the LRU ordinal value associated with the entry.

The key may be any borrowed form of the cache’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a mutable reference to the value corresponding to the key. Updates the LRU ordinal value associated with the entry.

The key may be any borrowed form of the cache’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Source

pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns the key-value pair corresponding to the supplied key. Updates the LRU ordinal value associated with the entry.

The supplied key may be any borrowed form of the cache’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Source

pub fn peek<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a reference to the value corresponding to the key. Unlike get, peek does not updates the LRU ordinal value associated with the entry.

Source

pub fn peek_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a mutable reference to the value corresponding to the key. Unlike get_mut, peek_mut does not updates the LRU ordinal value associated with the entry.

The key may be any borrowed form of the cache’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Source

pub fn peek_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns the key-value pair corresponding to the supplied key. Unlike get_key_value, peek_key_value does not updates the ordinal value associated with the entry.

The supplied key may be any borrowed form of the cache’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes a key from the cache, returning the value at the key if the key was previously in the cache.

The key may be any borrowed form of the cache’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Source

pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes a key from the cache, returning the stored key and value if the key was previously in the cache.

The key may be any borrowed form of the cache’s key type, but Hash and Eq on the borrowed form must match those for the key type.

Source

pub fn pop<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Synonym for remove.

Source

pub fn pop_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Synonym for remove_entry.

Source§

impl<K, V, S> LruCache<K, V, S>

Source

pub fn len(&self) -> usize

Returns the number of elements in the cache.

Source

pub fn is_empty(&self) -> bool

Returns true if the cache contains no entries.

Source

pub fn clear(&mut self)

Clears the cache, removing all key-value pairs.

Source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(&K, &mut V) -> bool,

Retains only the elements specified by the predicate. In other words, remove all pairs (k, v) for which f(&k, &mut v) returns false. The elements are visited in unsorted (and unspecified) order.

Source§

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

Source

pub fn clone(&mut self) -> Self

Clones the LruCache.

Note: &mut self is necessary to prevent interior mutation from concurrent access while the cache is cloned.

Trait Implementations§

Source§

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

Source§

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

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

impl<'a, K, V, S> IntoIterator for &'a LruCache<K, V, S>

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Iter<'a, K, V>

Creates an iterator from a value. Read more
Source§

impl<'a, K, V, S> IntoIterator for &'a mut LruCache<K, V, S>

Source§

type Item = (&'a K, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IterMut<'a, K, V>

Creates an iterator from a value. Read more
Source§

impl<K, V, S> IntoIterator for LruCache<K, V, S>

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IntoIter<K, V>

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<K, V, S = RandomState> !Freeze for LruCache<K, V, S>

§

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

§

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

§

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

§

impl<K, V, S> Unpin for LruCache<K, V, S>
where S: Unpin, K: Unpin, V: Unpin,

§

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