Skip to main content

BlockingLruCache

Struct BlockingLruCache 

Source
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>
where K: Eq + Hash,

Source

pub fn new(capacity: NonZeroUsize) -> Self

Creates a cache with the provided non-zero capacity.

Source

pub fn get_or_insert_with(&self, key: K, value: impl FnOnce() -> V) -> V
where V: Clone,

Returns a clone of the cached value for key, or computes and inserts it.

Source

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.

Source

pub fn try_with_capacity(capacity: usize) -> Option<Self>

Builds a cache if capacity is non-zero, returning None otherwise.

Source

pub fn get<Q>(&self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized, V: Clone,

Returns a clone of the cached value corresponding to key, if present.

Source

pub fn insert(&self, key: K, value: V) -> Option<V>

Inserts value for key, returning the previous entry if it existed.

Source

pub fn remove<Q>(&self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes the entry for key if it exists, returning it.

Source

pub fn clear(&self)

Clears all entries from the cache.

Source

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.

Source

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>
where K: Send, V: Send,

§

impl<K, V> Sync for BlockingLruCache<K, V>
where K: Send, V: Send,

§

impl<K, V> Unpin for BlockingLruCache<K, V>

§

impl<K, V> UnsafeUnpin for BlockingLruCache<K, V>

§

impl<K, V> UnwindSafe for BlockingLruCache<K, V>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.