Skip to main content

SyncCache

Struct SyncCache 

Source
pub struct SyncCache<K, V, C: Cache<K, V>> { /* private fields */ }
Expand description

A thread-safe wrapper around any Cache implementation.

Wraps C (which implements Cache<K, V>) in a Mutex, providing Send + Sync access from multiple threads. All methods acquire the mutex for their duration.

§Panics

Every method panics if the inner mutex is poisoned, which happens when another thread panicked while holding the lock.

Implementations§

Source§

impl<K, V, C: Cache<K, V>> SyncCache<K, V, C>

Source

pub fn new(cache: C) -> Self

Wrap cache in a SyncCache.

Source

pub fn get(&self, key: &K) -> Option<V>
where V: Clone,

Look up key, cloning and returning the value if present.

This acquires the lock, calls Cache::get (which may update recency), and clones the result so the lock can be released.

Source

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

Insert or update key -> value.

Returns the evicted value (if any) per the underlying cache policy.

Source

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

Remove the entry for key, returning its value if present.

Source

pub fn len(&self) -> usize

Return the number of live entries in the cache.

Source

pub fn is_empty(&self) -> bool

Return true if the cache holds no entries.

Source

pub fn clear(&self)

Remove all entries from the cache.

Auto Trait Implementations§

§

impl<K, V, C> !Freeze for SyncCache<K, V, C>

§

impl<K, V, C> RefUnwindSafe for SyncCache<K, V, C>

§

impl<K, V, C> Send for SyncCache<K, V, C>
where C: Send, K: Send, V: Send,

§

impl<K, V, C> Sync for SyncCache<K, V, C>
where C: Send, K: Sync, V: Sync,

§

impl<K, V, C> Unpin for SyncCache<K, V, C>
where C: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, C> UnsafeUnpin for SyncCache<K, V, C>
where C: UnsafeUnpin,

§

impl<K, V, C> UnwindSafe for SyncCache<K, V, C>
where K: UnwindSafe, V: UnwindSafe,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.