pub struct LfuCache<Key: Hash + Eq, Value> { /* private fields */ }Expand description
A collection that if limited to a certain capacity will evict based on the least recently used value.
Implementations§
Source§impl<Key: Hash + Eq, Value> LfuCache<Key, Value>
impl<Key: Hash + Eq, Value> LfuCache<Key, Value>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a LFU cache with at least the specified capacity.
When the capacity is reached, then the least frequently used item is evicted. If there are multiple least frequently used items in this collection, the most recently added item is evicted.
let mut cache = LfuCache::with_capacity(2);
// Fill up the cache.
cache.insert("foo", 3);
cache.insert("bar", 4);
// Insert returns the evicted value, if a value was evicted.
let maybe_evicted = cache.insert("baz", 5);
// In the case of a tie, the most recently added value is evicted.
assert!(cache.get(&"bar").is_none());
assert_eq!(maybe_evicted, Some(4));
cache.get(&"baz");
// Otherwise, the least frequently value is evicted.
assert_eq!(cache.pop_lfu(), Some(3));Sourcepub fn unbounded() -> Self
pub fn unbounded() -> Self
Creates a LFU cache with no bound.
This effectively turns the LFU cache into a very expensive HashMap
if the least frequently used item is never removed. This is useful if
you want to have fine-grain control over when the LFU cache should
evict. If a LFU cache was constructed using this function, users should
call pop_lfu to remove the least frequently used item.
Construction of this cache will not heap allocate.
Sourcepub fn set_capacity(&mut self, new_capacity: usize)
pub fn set_capacity(&mut self, new_capacity: usize)
Sets the new capacity.
If the provided capacity is zero, then this will turn the cache into an unbounded cache. If the new capacity is less than the current capacity, the least frequently used items are evicted until the number of items is equal to the capacity.
If the cache becomes unbounded, then users must manually call
pop_lfu to remove the least frequently used item.
If the cache was previously unbounded and is provided a non-zero capacity, then the cache now is bounded and will automatically remove the least frequently used item when the capacity is reached.
Sourcepub fn insert(&mut self, key: Key, value: Value) -> Option<Value>
pub fn insert(&mut self, key: Key, value: Value) -> Option<Value>
Inserts a key-value pair into the cache.
If the key already exists, the value is updated without updating the key and the previous value is returned. The frequency of this item is reset.
If the key did not already exist, then what is returned depends on the capacity of the cache. If an entry was evicted, the old value is returned. If the cache did not need to evict an entry or was unbounded, this returns None.
Sourcepub fn get(&mut self, key: &Key) -> Option<&Value>
pub fn get(&mut self, key: &Key) -> Option<&Value>
Gets a value and increments the internal frequency counter of that value, if it exists.
Sourcepub fn get_mut(&mut self, key: &Key) -> Option<&mut Value>
pub fn get_mut(&mut self, key: &Key) -> Option<&mut Value>
Gets a mutable value and increments the internal frequency counter of that value, if it exists.
Sourcepub fn remove(&mut self, key: &Key) -> Option<Value>
pub fn remove(&mut self, key: &Key) -> Option<Value>
Removes a value from the cache by key, if it exists.
Sourcepub fn entry(&mut self, key: Key) -> Entry<'_, Key, Value>
pub fn entry(&mut self, key: Key) -> Entry<'_, Key, Value>
Gets the given key’s corresponding entry in the LFU cache for in-place manipulation. If the key refers to an occupied entry, that entry then is immediately considered accessed, even if no reading or writing is performed. This behavior is a limitation of the Entry API.
Sourcepub fn pop_lfu(&mut self) -> Option<Value>
pub fn pop_lfu(&mut self) -> Option<Value>
Evicts the least frequently used value and returns it. If the cache is empty, then this returns None. If there are multiple items that have an equal access count, then the most recently added value is evicted.
Sourcepub fn pop_lfu_key_value(&mut self) -> Option<(Key, Value)>
pub fn pop_lfu_key_value(&mut self) -> Option<(Key, Value)>
Evicts the least frequently used key-value pair and returns it. If the cache is empty, then this returns None. If there are multiple items that have an equal access count, then the most recently added key-value pair is evicted.
Sourcepub fn pop_lfu_key_value_frequency(&mut self) -> Option<(Key, Value, usize)>
pub fn pop_lfu_key_value_frequency(&mut self) -> Option<(Key, Value, usize)>
Evicts the least frequently used value and returns it, the key it was inserted under, and the frequency it had. If the cache is empty, then this returns None. If there are multiple items that have an equal access count, then the most recently added key-value pair is evicted.
Sourcepub fn clear(&mut self) -> LfuCacheIter<Key, Value> ⓘ
pub fn clear(&mut self) -> LfuCacheIter<Key, Value> ⓘ
Clears the cache, returning the iterator of the previous cached values.
Sourcepub fn peek_lfu(&self) -> Option<&Value>
pub fn peek_lfu(&self) -> Option<&Value>
Peeks at the next value to be evicted, if there is one. This will not increment the access counter for that value.
Sourcepub fn peek_lfu_key(&self) -> Option<&Key>
pub fn peek_lfu_key(&self) -> Option<&Key>
Peeks at the key for the next value to be evicted, if there is one. This will not increment the access counter for that value.
Sourcepub const fn capacity(&self) -> Option<NonZeroUsize>
pub const fn capacity(&self) -> Option<NonZeroUsize>
Returns the current capacity of the cache.
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the current number of items in the cache. This is a constant time operation.
Sourcepub const fn is_unbounded(&self) -> bool
pub const fn is_unbounded(&self) -> bool
Returns if the cache is unbounded.
Sourcepub fn frequencies(&self) -> Vec<usize>
pub fn frequencies(&self) -> Vec<usize>
Returns the frequencies that this cache has. This is a linear time operation.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Sets the capacity to the amount of objects currently in the cache. If no items were in the cache, the cache becomes unbounded.
Sourcepub fn keys(&self) -> impl Iterator<Item = &Key> + FusedIterator + '_
pub fn keys(&self) -> impl Iterator<Item = &Key> + FusedIterator + '_
Returns an iterator over the keys of the LFU cache in any order.
Sourcepub fn peek_values(&self) -> impl Iterator<Item = &Value> + FusedIterator + '_
pub fn peek_values(&self) -> impl Iterator<Item = &Value> + FusedIterator + '_
Returns an iterator over the values of the LFU cache in any order. Note that this does not increment the count for any of the values.
Trait Implementations§
Source§impl<Key: Hash + Eq + Debug, Value> Debug for LfuCache<Key, Value>
Available on non-tarpaulin_include only.
impl<Key: Hash + Eq + Debug, Value> Debug for LfuCache<Key, Value>
tarpaulin_include only.Source§impl<Key: Hash + Eq, Value> Extend<(Key, Value)> for LfuCache<Key, Value>
impl<Key: Hash + Eq, Value> Extend<(Key, Value)> for LfuCache<Key, Value>
Source§fn extend<T: IntoIterator<Item = (Key, Value)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (Key, Value)>>(&mut self, iter: T)
Inserts the items from the iterator into the cache. Note that this may evict items if the number of elements in the iterator plus the number of current items in the cache exceeds the capacity of the cache.
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<Key: Hash + Eq, Value> FromIterator<(Key, Value)> for LfuCache<Key, Value>
impl<Key: Hash + Eq, Value> FromIterator<(Key, Value)> for LfuCache<Key, Value>
Source§fn from_iter<T: IntoIterator<Item = (Key, Value)>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = (Key, Value)>>(iter: T) -> Self
Constructs a LFU cache with the capacity equal to the number of elements in the iterator.