pub struct Cache<K, V> {
data: DashMap<K, Arc<SetOnce<V>>>,
pub num_hits: AtomicUsize,
pub num_misses: AtomicUsize,
}Expand description
Cache for asynchronous operations. Each operation is associated with a key, and operations are cached, deduplicated and mutually exclusive with other operations on the same key, including in-progress operations.
Fields§
§data: DashMap<K, Arc<SetOnce<V>>>Internal map of keys to set-once values.
num_hits: AtomicUsizeNumber of cache hits (including hits to in-progress values).
num_misses: AtomicUsizeNumber of cache misses.
Implementations§
Source§impl<K, V> Cache<K, V>
impl<K, V> Cache<K, V>
Sourcepub fn lock_entry(&self, key: &K) -> Result<CacheSetter<V>, CacheFut<V>>where
K: Clone,
pub fn lock_entry(&self, key: &K) -> Result<CacheSetter<V>, CacheFut<V>>where
K: Clone,
Locks the cache entry with the given key, adding it to the cache if it does not already exist. This function returns values which can be used to write into or read from the cache.
If this is the first task to lock this entry, Ok of CacheSetter
is returned so the call can compute and store the value. If the value is
already cached or another task is currently computing the value, Err
of CacheFut is returned which can be used to wait and retrieve the value
from the cache.
The given key will only be cloned if the cache does not currently have an entry for this key.
§Errors
An Err means the cache key is already completed or in-progress, as
described above.
Sourcepub fn into_completed_entries(self) -> impl Iterator<Item = (K, V)>
pub fn into_completed_entries(self) -> impl Iterator<Item = (K, V)>
Consumes the cache and returns an iterator over the completed key and value pairs.
§Panics
This function will panic if there are leftover CacheFut or CacheSetter
references pointing to the current cache.
Trait Implementations§
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>
impl<K, V> Sync for Cache<K, V>
impl<K, V> Unpin for Cache<K, V>
impl<K, V> UnsafeUnpin for Cache<K, V>
impl<K, V> !UnwindSafe for Cache<K, V>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more