[−][src]Struct waitcache::WaitCache
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
impl<K: Eq + Hash, V> WaitCache<K, V, RandomState>
[src]
#[must_use]pub fn new() -> Self
[src]
Constructs a new WaitCache
with the default hashing algorithm and an
initial capacity of 0.
#[must_use]pub fn with_capacity(capacity: usize) -> Self
[src]
Constructs a new WaitCache
with the default hashing algorithm and the
specified initial capacity.
impl<K: Eq + Hash, V, S: BuildHasher + Clone> WaitCache<K, V, S>
[src]
#[must_use]pub fn with_hasher(hasher: S) -> Self
[src]
Constructs a new WaitCache
with the specified hasher and an initial
capacity of 0.
#[must_use]pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self
[src]
Constructs a new WaitCache
with the specified hasher and initial
capacity.
#[must_use]pub fn is_empty(&self) -> bool
[src]
Returns true
if the cache currently contains no items and false
otherwise.
#[must_use]pub fn len(&self) -> usize
[src]
Returns the number of items currently in the cache.
#[must_use]pub fn capacity(&self) -> usize
[src]
Returns the currently provisioned capacity of the cache.
pub fn shrink_to_fit(&self)
[src]
Shrinks the provisioned capacity to match the current number of items in the cache.
#[must_use]pub fn hasher(&self) -> &S
[src]
Returns the hasher used to construct this cache.
pub fn clear(&mut self)
[src]
Empties the cache of all items.
#[must_use]pub fn get<'a, 'b, Q: Eq + Hash + ?Sized>(&'a self, key: &'b Q) -> Option<&'a V> where
K: Borrow<Q>,
[src]
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.
pub fn resolve<F: FnOnce() -> V>(&self, key: K, init: F) -> &V
[src]
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.
#[must_use]pub fn iter(&self) -> Iter<'_, K, V, S>ⓘ
[src]
Produces an iterator of immutable references to the entries contained in the cache.
The values are exposed as immutable references to a WaitCell
containing the value. The value may still be resolving.
Concurrency
This iterator may prevent concurrent updates to certain portions of the cache.
#[must_use]pub fn keys(&self) -> Keys<'_, K, V, S>ⓘ
[src]
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.
#[must_use]pub fn values(&self) -> Values<'_, K, V, S>ⓘ
[src]
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.
#[must_use]pub fn from_raw_iter_with_hasher<T: IntoIterator<Item = (K, Box<WaitCell<V>>)>>(
iter: T,
hasher: S
) -> Self
[src]
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.
#[must_use]pub fn from_iter_with_hasher<T: IntoIterator<Item = (K, V)>>(
iter: T,
hasher: S
) -> Self
[src]
iter: T,
hasher: S
) -> Self
Constructs a WaitCache
from the specified iterator and hasher.
Trait Implementations
impl<K: Debug + Eq + Hash, V: Debug, S: BuildHasher + Clone> Debug for WaitCache<K, V, S>
[src]
impl<K: Eq + Hash, V, S: BuildHasher + Clone + Default> Default for WaitCache<K, V, S>
[src]
impl<K: Eq + Hash, V> FromIterator<(K, V)> for WaitCache<K, V, RandomState>
[src]
pub fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self
[src]
impl<K: Eq + Hash, V, S: BuildHasher + Clone> IntoIterator for WaitCache<K, V, S>
[src]
Auto Trait Implementations
impl<K, V, S = RandomState> !RefUnwindSafe for WaitCache<K, V, S>
[src]
impl<K, V, S> Send for WaitCache<K, V, S> where
K: Send,
S: Send,
V: Send,
[src]
K: Send,
S: Send,
V: Send,
impl<K, V, S> Sync for WaitCache<K, V, S> where
K: Send + Sync,
S: Send + Sync,
V: Send + Sync,
[src]
K: Send + Sync,
S: Send + Sync,
V: Send + Sync,
impl<K, V, S> Unpin for WaitCache<K, V, S> where
S: Unpin,
[src]
S: Unpin,
impl<K, V, S> UnwindSafe for WaitCache<K, V, S> where
K: UnwindSafe,
S: UnwindSafe,
V: UnwindSafe,
[src]
K: UnwindSafe,
S: UnwindSafe,
V: UnwindSafe,
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>,