Struct retainer::cache::Cache [−][src]
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
impl<K, V> Cache<K, V> where
K: Ord + Clone,
[src]
K: Ord + Clone,
pub fn new() -> Self
[src]
Construct a new Cache
.
pub fn with_label(mut self: Self, s: &str) -> Self
[src]
Sets the label inside this cache for logging purposes.
pub async fn clear(&self)
[src]
Remove all entries from the cache.
pub async fn expired(&self) -> usize
[src]
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.
pub async fn get(&self, k: &K) -> Option<CacheEntryReadGuard<'_, V>>
[src]
Retrieve a reference to a value inside the cache.
The returned reference is bound inside a RwLockReadGuard
.
pub async fn len(&self) -> usize
[src]
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.
pub async fn insert<E>(&self, k: K, v: V, e: E) -> Option<CacheEntry<V>> where
E: Into<CacheExpiration>,
[src]
E: Into<CacheExpiration>,
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, see insert_untracked
.
pub async fn insert_untracked(&self, k: K, v: V) -> Option<CacheEntry<V>>
[src]
Insert a key/value pair into the cache with no associated expiration.
pub async fn is_empty(&self) -> bool
[src]
Check whether the cache is empty.
pub async fn monitor(&self, sample: usize, threshold: f64, frequency: Duration)
[src]
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.
pub async fn purge(&self, sample: usize, threshold: f64)
[src]
Cleanses the cache of expired entries.
Keys are expired using the same logic as the popular caching system Redis:
- Wait until the next tick of
frequency
. - Take a sample of
sample
keys from the cache. - Remove any expired keys from the sample.
- Based on
threshold
percentage: 4a. If more thanthreshold
were expired, goto #2. 4b. If less thanthreshold
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.
pub async fn remove(&self, k: &K) -> Option<CacheEntry<V>>
[src]
Remove an entry from the cache and return any stored value.
pub async fn unexpired(&self) -> usize
[src]
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.
pub async fn update<F>(&self, k: &K, f: F) where
F: FnOnce(&mut V),
[src]
F: FnOnce(&mut V),
Updates an entry in the cache without changing the expiration.
Trait Implementations
Auto Trait Implementations
impl<K, V> !RefUnwindSafe for Cache<K, V>
[src]
impl<K, V> Send for Cache<K, V> where
K: Send,
V: Send,
[src]
K: Send,
V: Send,
impl<K, V> Sync for Cache<K, V> where
K: Send + Sync,
V: Send + Sync,
[src]
K: Send + Sync,
V: Send + Sync,
impl<K, V> Unpin for Cache<K, V>
[src]
impl<K, V> UnwindSafe for Cache<K, V> where
K: RefUnwindSafe,
V: RefUnwindSafe,
[src]
K: RefUnwindSafe,
V: RefUnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,