pub struct LruCache<K, V> { /* private fields */ }Expand description
An LRU (least-recently-used) cache.
Implemented by maintaining a doubly-linked list of cache entries. On access, entries are moved to the head of list. Once the cache reaches capacity, entries at the back of the list will be evicted first to make room for newer entries.
Implementations§
Source§impl<K, V> LruCache<K, V>
impl<K, V> LruCache<K, V>
Sourcepub fn put(&mut self, key: K, value: V) -> Option<V>
pub fn put(&mut self, key: K, value: V) -> Option<V>
Puts a key-value pair into the cache.
If the key already exists in the cache, it is updated and the old value is returned.
Otherwise, None is returned.
Sourcepub fn push(&mut self, key: K, value: V) -> Option<(K, V)>
pub fn push(&mut self, key: K, value: V) -> Option<(K, V)>
Pushes a key-value pair into the cache.
If the key already exists in the cache or another entry is removed (due to capacity),
then the old key-value pair is returned. Otherwise, returns None.
Sourcepub fn contains_key<'a, L>(&'a self, key: &L) -> bool
pub fn contains_key<'a, L>(&'a self, key: &L) -> bool
Checks if the given key is contained in the cache.
Sourcepub fn get<'a, L>(&'a mut self, key: &L) -> Option<&'a V>
pub fn get<'a, L>(&'a mut self, key: &L) -> Option<&'a V>
Returns a reference to the value associated with the given key.
Moves the key to the head of the LRU list if it exists. Otherwise, returns None.
Sourcepub fn get_mut<'a, L>(&'a mut self, key: &L) -> Option<&'a mut V>
pub fn get_mut<'a, L>(&'a mut self, key: &L) -> Option<&'a mut V>
Returns a mutable reference to the value associated with the given key.
Moves the key to the head of the LRU list if it exists. Otherwise, returns None.
Trait Implementations§
Source§impl<'a, K, V> IntoIterator for &'a LruCache<K, V>
impl<'a, K, V> IntoIterator for &'a LruCache<K, V>
Source§impl<'a, K, V> IntoIterator for &'a mut LruCache<K, V>
impl<'a, K, V> IntoIterator for &'a mut LruCache<K, V>
impl<K: Send, V: Send> Send for LruCache<K, V>
impl<K: Sync, V: Sync> Sync for LruCache<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for LruCache<K, V>
impl<K, V> RefUnwindSafe for LruCache<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Unpin for LruCache<K, V>
impl<K, V> UnwindSafe for LruCache<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more