Struct clru::CLruCache

source ·
pub struct CLruCache<K, V, S = RandomState, W: WeightScale<K, V> = ZeroWeightScale> { /* private fields */ }
Expand description

A weighted LRU cache with mostly¹ constant time operations.

Each key-value pair in the cache can have a weight that is retrieved using the provided WeightScale implementation. The default scale is ZeroWeightScale and always return 0. The number of elements that can be stored in the cache is conditioned by the sum of CLruCache::len and CLruCache::weight:

CLruCache::len + CLruCache::weight <= CLruCache::capacity

Using the default ZeroWeightScale scale unlocks some useful APIs that can currently only be implemented for this scale. The most interesting ones are probably:

But more generally, using ZeroWeightScale unlocks all methods that return a mutable reference to the value of an element. This is because modifying the value of an element can lead to a modification of its weight and therefore would put the cache into an incoherent state. For the same reason, it is a logic error for a value to change weight while being stored in the cache.

The cache requires the keys to be clonable because it will store 2 instances of each key in different internal data structures. If cloning a key can be expensive, you might want to consider using an Rc or an Arc.

Note 1: See CLruCache::put_with_weight

Implementations§

Creates a new LRU cache that holds at most capacity elements.

Creates a new LRU cache that holds at most capacity elements and pre-allocates memory in order to hold at least reserve elements without reallocating.

Creates a new LRU cache that holds at most capacity elements and uses the provided hash builder to hash keys.

Creates a new LRU cache that holds at most capacity elements and uses the provided scale to retrieve value’s weight.

Creates a new LRU cache using the provided configuration.

Returns the number of key-value pairs that are currently in the cache.

Returns the capacity of the cache. It serves as a limit for

  • the number of elements that the cache can hold.
  • the total weight of the elements in the cache.

Returns the total weight of the elements in the cache.

Returns a bool indicating whether the cache is empty or not.

Returns a bool indicating whether the cache is full or not.

Returns the value corresponding to the most recently used item or None if the cache is empty. Like peek, front does not update the LRU list so the item’s position will be unchanged.

Returns the value corresponding to the least recently used item or None if the cache is empty. Like peek, back does not update the LRU list so the item’s position will be unchanged.

Puts a key-value pair into cache taking it’s weight into account. If the weight of the new element is greater than what the cache can hold, it returns the provided key-value pair as an error. Otherwise, it removes enough elements for the new element to be inserted, thus making it a non constant time operation. If the key already exists in the cache, then it updates the key’s value and returns the old value. Otherwise, None is returned.

Returns a reference to the value of the key in the cache or None if it is not present in the cache. Moves the key to the head of the LRU list if it exists.

Removes and returns the value corresponding to the key from the cache or None if it does not exist.

Removes and returns the key and value corresponding to the most recently used item or None if the cache is empty.

Removes and returns the key and value corresponding to the least recently used item or None if the cache is empty.

Returns a reference to the value corresponding to the key in the cache or None if it is not present in the cache. Unlike get, peek does not update the LRU list so the key’s position will be unchanged.

Returns a bool indicating whether the given key is in the cache. Does not update the LRU list.

Clears the contents of the cache.

Resizes the cache. If the new capacity is smaller than the size of the current cache any entries past the new capacity are discarded.

Retains only the elements specified by the predicate. In other words, remove all pairs (k, v) such that f(&k, &v) returns false.

Returns an iterator visiting all entries in order. The iterator element type is (&'a K, &'a V).

Puts a key-value pair into cache. If the key already exists in the cache, then it updates the key’s value and returns the old value. Otherwise, None is returned.

Puts a new key-value pair or modify an already existing value. If the key already exists in the cache, then modify_op supplied function is called. Otherwise, put_op supplied function is called. Returns a mutable reference to the value.

The additional data argument can be used to pass extra information to the supplied functions. This can useful when both functions need to access the same variable.

Puts a new key-value pair or modify an already existing value. If the key already exists in the cache, then modify_op supplied function is called. Otherwise, put_op supplied function is called. Returns a mutable reference to the value or an error.

The additional data argument can be used to pass extra information to the supplied functions. This can useful when both functions need to access the same variable.

This is the fallible version of CLruCache::put_or_modify.

Returns the value corresponding to the most recently used item or None if the cache is empty. Like peek, font does not update the LRU list so the item’s position will be unchanged.

Returns the value corresponding to the least recently used item or None if the cache is empty. Like peek, back does not update the LRU list so the item’s position will be unchanged.

Returns a mutable reference to the value of the key in the cache or None if it is not present in the cache. Moves the key to the head of the LRU list if it exists.

Returns a mutable reference to the value corresponding to the key in the cache or None if it is not present in the cache. Unlike get_mut, peek_mut does not update the LRU list so the key’s position will be unchanged.

Retains only the elements specified by the predicate. In other words, remove all pairs (k, v) such that f(&k, &mut v) returns false.

Returns an iterator visiting all entries in order, giving a mutable reference on V. The iterator element type is (&'a K, &'a mut V).

Trait Implementations§

Formats the value using the given formatter. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Creates a value from an iterator. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more

Consumes the cache into an iterator yielding elements by value.

The type of the elements being iterated over.
Which kind of iterator are we turning this into?

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.