Struct SyncLruCache

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

Source

pub fn new(cap: usize) -> Self

Creates a new LRU cache that holds at most cap items.

Source

pub fn len(&self) -> usize

Returns the number of key-value pairs that are currently in the cache.

Source

pub fn is_empty(&self) -> bool

Returns true if the cache is empty and false otherwise.

Source

pub fn contains(&self, key: &K) -> bool

Returns true if the cache contains the key and false otherwise.

Source

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.

Source

pub fn get_or_put<F>(&self, key: K, f: F) -> V
where V: Clone, F: FnOnce(&K) -> 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.

Source

pub fn get_or_try_put<F, E>(&self, key: K, f: F) -> Result<V, E>
where V: Clone, F: FnOnce(&K) -> 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.

Source

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.

Source

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.

Source

pub fn lock(&self) -> MutexGuard<'_, LruCache<K, V>>

Returns the lock over underlying LRU cache.

Auto Trait Implementations§

§

impl<K, V> !Freeze for SyncLruCache<K, V>

§

impl<K, V> RefUnwindSafe for SyncLruCache<K, V>

§

impl<K, V> Send for SyncLruCache<K, V>
where K: Send, V: Send,

§

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

§

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

§

impl<K, V> UnwindSafe for SyncLruCache<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, 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.