Struct moka::sync::SegmentedCache [−][src]
pub struct SegmentedCache<K, V, S = RandomState> { /* fields omitted */ }
Expand description
A thread-safe concurrent in-memory cache, with multiple internal segments.
SegmentedCache
has multiple internal Cache
instances for
increased concurrent update performance. However, it has little overheads on
retrievals and updates for managing these segments.
For usage examples, see the document of the Cache
.
Implementations
impl<K, V> SegmentedCache<K, V, RandomState> where
K: Hash + Eq + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
impl<K, V> SegmentedCache<K, V, RandomState> where
K: Hash + Eq + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
Constructs a new SegmentedCache<K, V>
that has multiple internal
segments and will store up to the max_capacity
entries.
To adjust various configuration knobs such as initial_capacity
or
time_to_live
, use the CacheBuilder
.
Panics
Panics if num_segments
is 0.
Returns a clone of the value corresponding to the key.
If you want to store values that will be expensive to clone, wrap them by
std::sync::Arc
before storing in a cache. Arc
is a
thread-safe reference-counted pointer and its clone()
method is cheap.
The key may be any borrowed form of the cache’s key type, but Hash
and Eq
on the borrowed form must match those for the key type.
Ensures the value of the key exists by inserting the result of the init closure if not exist, and returns a clone of the value.
This method prevents to evaluate the init closure multiple times on the same key even if the method is concurrently called by many threads; only one of the calls evaluates its closure, and other calls wait for that closure to complete.
Try to ensure the value of the key exists by inserting an Ok
result of the
init closure if not exist, and returns a clone of the value or the Err
returned by the closure.
This method prevents to evaluate the init closure multiple times on the same key even if the method is concurrently called by many threads; only one of the calls evaluates its closure (as long as these closures return the same error type), and other calls wait for that closure to complete.
Inserts a key-value pair into the cache.
If the cache has this key present, the value is updated.
Discards any cached value for the key.
The key may be any borrowed form of the cache’s key type, but Hash
and Eq
on the borrowed form must match those for the key type.
Discards all cached values.
This method returns immediately and a background thread will evict all the
cached values inserted before the time when this method was called. It is
guaranteed that the get
method must not return these invalidated values
even if they have not been evicted.
Like the invalidate
method, this method does not clear the historic
popularity estimator of keys so that it retains the client activities of
trying to retrieve an item.
Discards cached values that satisfy a predicate.
invalidate_entries_if
takes a closure that returns true
or false
. This
method returns immediately and a background thread will apply the closure to
each cached value inserted before the time when invalidate_entries_if
was
called. If the closure returns true
on a value, that value will be evicted
from the cache.
Also the get
method will apply the closure to a value to determine if it
should have been invalidated. Therefore, it is guaranteed that the get
method must not return invalidated values.
Note that you must call
CacheBuilder::support_invalidation_closures
at the cache creation time as the cache needs to maintain additional internal
data structures to support this method. Otherwise, calling this method will
fail with a
PredicateError::InvalidationClosuresDisabled
.
Like the invalidate
method, this method does not clear the historic
popularity estimator of keys so that it retains the client activities of
trying to retrieve an item.
Returns the max_capacity
of this cache.
Returns the time_to_live
of this cache.
Returns the time_to_idle
of this cache.
Returns the number of internal segments of this cache.
Trait Implementations
impl<K, V, S> ConcurrentCacheExt<K, V> for SegmentedCache<K, V, S> where
K: Hash + Eq + Send + Sync + 'static,
V: Send + Sync + 'static,
S: BuildHasher + Clone + Send + Sync + 'static,
impl<K, V, S> ConcurrentCacheExt<K, V> for SegmentedCache<K, V, S> where
K: Hash + Eq + Send + Sync + 'static,
V: Send + Sync + 'static,
S: BuildHasher + Clone + Send + Sync + 'static,
Auto Trait Implementations
impl<K, V, S = RandomState> !RefUnwindSafe for SegmentedCache<K, V, S>
impl<K, V, S> Unpin for SegmentedCache<K, V, S>
impl<K, V, S = RandomState> !UnwindSafe for SegmentedCache<K, V, S>
Blanket Implementations
Mutably borrows from an owned value. Read more