pub struct WaitCache<K, V, S = RandomState> { /* private fields */ }Expand description
A concurrent, ever-growing hash map with key-level lock granularity.
This map is suitable for use in situations where the resolution of values can take an extraordinarily long time, such as caching the result of disk access, network requests, or extensive computations. Concurrent resolutions block access to only the resolving values, and not to hash-adjacent values.
Unlike some other cache implemetations, the function used to resolve a given key is never executed multiple times. This is important for avoiding duplication of expensive work.
Implementations§
Source§impl<K: Eq + Hash, V> WaitCache<K, V, RandomState>
impl<K: Eq + Hash, V> WaitCache<K, V, RandomState>
Sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a new WaitCache with the default hashing algorithm and an
initial capacity of 0.
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Constructs a new WaitCache with the default hashing algorithm and the
specified initial capacity.
Source§impl<K: Eq + Hash, V, S: BuildHasher + Clone> WaitCache<K, V, S>
impl<K: Eq + Hash, V, S: BuildHasher + Clone> WaitCache<K, V, S>
Sourcepub fn with_hasher(hasher: S) -> Self
pub fn with_hasher(hasher: S) -> Self
Constructs a new WaitCache with the specified hasher and an initial
capacity of 0.
Sourcepub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self
Constructs a new WaitCache with the specified hasher and initial
capacity.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the cache currently contains no items and false
otherwise.
Sourcepub fn shrink_to_fit(&self)
pub fn shrink_to_fit(&self)
Shrinks the provisioned capacity to match the current number of items in the cache.
Sourcepub fn get<'a, 'b, Q: Eq + Hash + ?Sized>(&'a self, key: &'b Q) -> Option<&'a V>where
K: Borrow<Q>,
pub fn get<'a, 'b, Q: Eq + Hash + ?Sized>(&'a self, key: &'b Q) -> Option<&'a V>where
K: Borrow<Q>,
Looks up a value in the cache given a compatible key.
If the key is present but the value is not fully resolved, the current thread will block until resolution completes.
§Returns
Some(value)- The fully-resolved value corresponding to the specified key.None- The specified key was not present.
Sourcepub fn resolve<F: FnOnce() -> V>(&self, key: K, init: F) -> &V
pub fn resolve<F: FnOnce() -> V>(&self, key: K, init: F) -> &V
Retrieves the value with the specified key, or initializes it if it is not present.
If the key is present but the value is not fully resolved, the current
thread will block until resolution completes. If the key is not present,
init is executed to produce a value. In either case, an immutable
reference to the value is returned.
§Notes
The resolution closure, init, does not provide access to the key being
resolved. You may need to provide a copy of this value to the closure.
This is done to allow for maximum concurrency, as it permits the key
to be accessed by other threads during the resolution process.
Sourcepub fn keys(&self) -> Keys<'_, K, V, S> ⓘ
pub fn keys(&self) -> Keys<'_, K, V, S> ⓘ
Produces an iterator of immutable references to the keyes contained in the cache.
§Concurrency
This iterator may prevent concurrent updates to certain portions of the cache.
Sourcepub fn values(&self) -> Values<'_, K, V, S> ⓘ
pub fn values(&self) -> Values<'_, K, V, S> ⓘ
Produces an iterator of immutable references to the values contained in the cache.
The values are exposed as immutable references to a WaitCell
containing the value. The value may still be resolving.
Sourcepub fn from_raw_iter_with_hasher<T: IntoIterator<Item = (K, Box<WaitCell<V>>)>>(
iter: T,
hasher: S,
) -> Self
pub fn from_raw_iter_with_hasher<T: IntoIterator<Item = (K, Box<WaitCell<V>>)>>( iter: T, hasher: S, ) -> Self
Constructs a WaitCache from the specified boxed cell iterator and
hasher.
§Notes
This method is provided for completeness. Prefer using
from_iter_with_hasher instead.
Sourcepub fn from_iter_with_hasher<T: IntoIterator<Item = (K, V)>>(
iter: T,
hasher: S,
) -> Self
pub fn from_iter_with_hasher<T: IntoIterator<Item = (K, V)>>( iter: T, hasher: S, ) -> Self
Constructs a WaitCache from the specified iterator and hasher.