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, S> LruCache<K, V, S>
impl<K, V, S> LruCache<K, V, S>
Source§impl<K: Eq + Hash + PartialEq, V, S: BuildHasher> LruCache<K, V, S>
impl<K: Eq + Hash + PartialEq, V, S: BuildHasher> LruCache<K, V, S>
Sourcepub fn put(&mut self, key: K, value: V) -> Option<V>
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.
Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
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.
Sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
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.
Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
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.
Sourcepub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
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.
Sourcepub fn peek<Q>(&self, key: &Q) -> Option<&V>
pub fn peek<Q>(&self, key: &Q) -> Option<&V>
Returns a reference to the value corresponding to the key.
Unlike get
, peek
does not updates the LRU ordinal value
associated with the entry.
Sourcepub fn peek_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn peek_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
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.
Sourcepub fn peek_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
pub fn peek_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
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.
Sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<V>
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
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.
Sourcepub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
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.