WaitCache

Struct WaitCache 

Source
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>

Source

pub fn new() -> Self

Constructs a new WaitCache with the default hashing algorithm and an initial capacity of 0.

Source

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>

Source

pub fn with_hasher(hasher: S) -> Self

Constructs a new WaitCache with the specified hasher and an initial capacity of 0.

Source

pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self

Constructs a new WaitCache with the specified hasher and initial capacity.

Source

pub fn is_empty(&self) -> bool

Returns true if the cache currently contains no items and false otherwise.

Source

pub fn len(&self) -> usize

Returns the number of items currently in the cache.

Source

pub fn capacity(&self) -> usize

Returns the currently provisioned capacity of the cache.

Source

pub fn shrink_to_fit(&self)

Shrinks the provisioned capacity to match the current number of items in the cache.

Source

pub fn hasher(&self) -> &S

Returns the hasher used to construct this cache.

Source

pub fn clear(&mut self)

Empties the cache of all items.

Source

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.
Source

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.

Source

pub fn iter(&self) -> Iter<'_, K, V, S>

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<K: Debug + Eq + Hash, V: Debug, S: BuildHasher + Clone> Debug for WaitCache<K, V, S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: Eq + Hash, V, S: BuildHasher + Clone + Default> Default for WaitCache<K, V, S>

Source§

fn default() -> Self

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

impl<K: Eq + Hash, V> FromIterator<(K, V)> for WaitCache<K, V, RandomState>

Source§

fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<K: Eq + Hash, V, S: BuildHasher + Clone> IntoIterator for WaitCache<K, V, S>

Source§

type IntoIter = Owned<K, V, S>

Which kind of iterator are we turning this into?
Source§

type Item = (K, Option<V>)

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<K, V, S> Freeze for WaitCache<K, V, S>
where S: Freeze,

§

impl<K, V, S = RandomState> !RefUnwindSafe for WaitCache<K, V, S>

§

impl<K, V, S> Send for WaitCache<K, V, S>
where S: Send, K: Send, V: Send,

§

impl<K, V, S> Sync for WaitCache<K, V, S>
where S: Sync + Send, K: Send + Sync, V: Sync + Send,

§

impl<K, V, S> Unpin for WaitCache<K, V, S>
where S: Unpin,

§

impl<K, V, S> UnwindSafe for WaitCache<K, V, S>
where S: UnwindSafe, K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.