Struct Cache

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

Basic caching structure with asynchronous locking support.

This structure provides asynchronous access wrapped around a standard BTreeMap to avoid blocking event loops when a writer cannot gain a handle - which is what would happen with standard locking implementations.

Implementations§

Source§

impl<K, V> Cache<K, V>
where K: Ord + Clone,

Source

pub fn new() -> Self

Construct a new Cache.

Source

pub fn with_label(self, s: &str) -> Self

Sets the label inside this cache for logging purposes.

Source

pub async fn clear(&self)

Remove all entries from the cache.

Source

pub async fn expired(&self) -> usize

Retrieve the number of expired entries inside the cache.

Note that this is calculated by walking the set of entries and should therefore not be used in performance sensitive situations.

Source

pub async fn get(&self, k: &K) -> Option<CacheReadGuard<'_, V>>

Retrieve a reference to a value inside the cache.

The returned reference is bound inside a RwLockReadGuard.

Source

pub async fn len(&self) -> usize

Retrieve the number of entries inside the cache.

This does include entries which may be expired but are not yet evicted. In future there may be an API addition to find the unexpired count, but as it’s relatively expensive it has been omitted for the time being.

Source

pub async fn insert<E>(&self, k: K, v: V, e: E) -> Option<V>

Insert a key/value pair into the cache with an associated expiration.

The third argument controls expiration, which can be provided using any type which implements Into<CacheExpiration>. This allows for various different syntax based on your use case. If you do not want expiration, use CacheExpiration::none().

Source

pub async fn is_empty(&self) -> bool

Check whether the cache is empty.

Source

pub async fn monitor(&self, sample: usize, threshold: f64, frequency: Duration)

Retrieve a Future used to monitor expired keys.

This future must be spawned on whatever runtime you are using inside your application; not doing this will result in keys never being expired.

For expiration logic, please see Cache::purge, as this is used under the hood.

Source

pub async fn purge(&self, sample: usize, threshold: f64)

Cleanses the cache of expired entries.

Keys are expired using the same logic as the popular caching system Redis:

  1. Wait until the next tick of frequency.
  2. Take a sample of sample keys from the cache.
  3. Remove any expired keys from the sample.
  4. Based on threshold percentage: 4a. If more than threshold were expired, goto #2. 4b. If less than threshold were expired, goto #1.

This means that at any point you may have up to threshold percent of your cache storing expired entries (assuming the monitor just ran), so make sure to tune your frequency, sample size, and threshold accordingly.

Source

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

Remove an entry from the cache and return any stored value.

Source

pub async fn unexpired(&self) -> usize

Retrieve the number of unexpired entries inside the cache.

Note that this is calculated by walking the set of entries and should therefore not be used in performance sensitive situations.

Source

pub async fn update<F>(&self, k: &K, f: F)
where F: FnOnce(&mut V),

Updates an entry in the cache without changing the expiration.

Trait Implementations§

Source§

impl<K, V> Default for Cache<K, V>
where K: Ord + Clone,

Default implementation.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

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

§

impl<K, V> !RefUnwindSafe for Cache<K, V>

§

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

§

impl<K, V> Sync for Cache<K, V>
where K: Send + Sync, V: Send + Sync,

§

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

§

impl<K, V> UnwindSafe for Cache<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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V