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

Construct a new Cache.

Sets the label inside this cache for logging purposes.

Remove all entries from the cache.

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.

Retrieve a reference to a value inside the cache.

The returned reference is bound inside a RwLockReadGuard.

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.

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().

Check whether the cache is empty.

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.

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.

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

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.

Updates an entry in the cache without changing the expiration.

Trait Implementations

Default implementation.

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.