pub struct SyncLruCache<K, V> { /* private fields */ }
Expand description
A wrapper around LruCache
. This struct is thread safe, doesn’t return any references to any
elements inside.
Implementations§
Source§impl<K, V> SyncLruCache<K, V>
impl<K, V> SyncLruCache<K, V>
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of key-value pairs that are currently in the cache.
Sourcepub fn contains(&self, key: &K) -> bool
pub fn contains(&self, key: &K) -> bool
Returns true if the cache contains the key and false otherwise.
Sourcepub fn push(&self, key: K, value: V) -> Option<(K, V)>
pub fn push(&self, key: K, value: V) -> Option<(K, V)>
Pushes a key-value pair into the cache. If an entry with key k
already exists in
the cache or another cache entry is removed (due to the lru’s capacity),
then it returns the old entry’s key-value pair. Otherwise, returns None
.
Sourcepub fn get_or_put<F>(&self, key: K, f: F) -> V
pub fn get_or_put<F>(&self, key: K, f: F) -> V
Return the value of they key in the cache otherwise computes the value and inserts it into the cache. If the key is already in the cache, they get moved to the head of the LRU list.
Sourcepub fn get_or_try_put<F, E>(&self, key: K, f: F) -> Result<V, E>
pub fn get_or_try_put<F, E>(&self, key: K, f: F) -> Result<V, E>
Returns the value of they key in the cache if present, otherwise computes the value using the provided closure.
If the key is already in the cache, it gets moved to the head of the LRU list.
If the provided closure fails, the error is returned and the cache is not updated.
Sourcepub fn put(&self, key: K, value: V)
pub fn put(&self, key: K, value: V)
Puts a key-value pair into cache. If the key already exists in the cache, then it updates the key’s value.
Sourcepub fn get(&self, key: &K) -> Option<V>
pub fn get(&self, key: &K) -> Option<V>
Returns 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.
Sourcepub fn lock(&self) -> MutexGuard<'_, LruCache<K, V>>
pub fn lock(&self) -> MutexGuard<'_, LruCache<K, V>>
Returns the lock over underlying LRU cache.