pub struct BlockingLruCache<K, V> { /* private fields */ }Expand description
A minimal LRU cache protected by a Tokio mutex. Calls outside a Tokio runtime are no-ops.
Implementations§
Source§impl<K, V> BlockingLruCache<K, V>
impl<K, V> BlockingLruCache<K, V>
Sourcepub fn new(capacity: NonZeroUsize) -> Self
pub fn new(capacity: NonZeroUsize) -> Self
Creates a cache with the provided non-zero capacity.
Sourcepub fn get_or_insert_with(&self, key: K, value: impl FnOnce() -> V) -> Vwhere
V: Clone,
pub fn get_or_insert_with(&self, key: K, value: impl FnOnce() -> V) -> Vwhere
V: Clone,
Returns a clone of the cached value for key, or computes and inserts it.
Sourcepub fn get_or_try_insert_with<E>(
&self,
key: K,
value: impl FnOnce() -> Result<V, E>,
) -> Result<V, E>where
V: Clone,
pub fn get_or_try_insert_with<E>(
&self,
key: K,
value: impl FnOnce() -> Result<V, E>,
) -> Result<V, E>where
V: Clone,
Like get_or_insert_with, but the value factory may fail.
Sourcepub fn try_with_capacity(capacity: usize) -> Option<Self>
pub fn try_with_capacity(capacity: usize) -> Option<Self>
Builds a cache if capacity is non-zero, returning None otherwise.
Sourcepub fn get<Q>(&self, key: &Q) -> Option<V>
pub fn get<Q>(&self, key: &Q) -> Option<V>
Returns a clone of the cached value corresponding to key, if present.
Sourcepub fn insert(&self, key: K, value: V) -> Option<V>
pub fn insert(&self, key: K, value: V) -> Option<V>
Inserts value for key, returning the previous entry if it existed.
Sourcepub fn remove<Q>(&self, key: &Q) -> Option<V>
pub fn remove<Q>(&self, key: &Q) -> Option<V>
Removes the entry for key if it exists, returning it.
Sourcepub fn with_mut<R>(&self, callback: impl FnOnce(&mut LruCache<K, V>) -> R) -> R
pub fn with_mut<R>(&self, callback: impl FnOnce(&mut LruCache<K, V>) -> R) -> R
Executes callback with a mutable reference to the underlying cache.
Sourcepub fn blocking_lock(&self) -> Option<MutexGuard<'_, LruCache<K, V>>>
pub fn blocking_lock(&self) -> Option<MutexGuard<'_, LruCache<K, V>>>
Provides direct access to the cache guard when a Tokio runtime is available.
Auto Trait Implementations§
impl<K, V> !Freeze for BlockingLruCache<K, V>
impl<K, V> !RefUnwindSafe for BlockingLruCache<K, V>
impl<K, V> Send for BlockingLruCache<K, V>
impl<K, V> Sync for BlockingLruCache<K, V>
impl<K, V> Unpin for BlockingLruCache<K, V>
impl<K, V> UnsafeUnpin for BlockingLruCache<K, V>
impl<K, V> UnwindSafe for BlockingLruCache<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
Mutably borrows from an owned value. Read more