Struct moka::sync::CacheBuilder [−][src]
pub struct CacheBuilder<C> { /* fields omitted */ }
Expand description
Builds a Cache
or SegmentedCache
with various configuration knobs.
Examples
use moka::sync::CacheBuilder;
use std::time::Duration;
let cache = CacheBuilder::new(10_000) // Max 10,000 elements
// Time to live (TTL): 30 minutes
.time_to_live(Duration::from_secs(30 * 60))
// Time to idle (TTI): 5 minutes
.time_to_idle(Duration::from_secs( 5 * 60))
// Create the cache.
.build();
// This entry will expire after 5 minutes (TTI) if there is no get().
cache.insert(0, "zero");
// This get() will extend the entry life for another 5 minutes.
cache.get(&0);
// Even though we keep calling get(), the entry will expire
// after 30 minutes (TTL) from the insert().
Implementations
Construct a new CacheBuilder
that will be used to build a Cache
or
SegmentedCache
holding up to max_capacity
entries.
Sets the number of segments of the cache.
Panics
Panics if num_segments
is less than or equals to 1.
Builds a Cache<K, V>
.
If you want to build a SegmentedCache<K, V>
, call segments
method before
calling this method.
pub fn build_with_hasher<S>(self, hasher: S) -> Cache<K, V, S> where
S: BuildHasher + Clone + Send + Sync + 'static,
pub fn build_with_hasher<S>(self, hasher: S) -> Cache<K, V, S> where
S: BuildHasher + Clone + Send + Sync + 'static,
Builds a Cache<K, V, S>
, with the given hasher
.
If you want to build a SegmentedCache<K, V>
, call segments
method before
calling this method.
impl<K, V> CacheBuilder<SegmentedCache<K, V, RandomState>> where
K: Eq + Hash + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
impl<K, V> CacheBuilder<SegmentedCache<K, V, RandomState>> where
K: Eq + Hash + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
Builds a SegmentedCache<K, V>
.
If you want to build a Cache<K, V>
, do not call segments
method before
calling this method.
pub fn build_with_hasher<S>(self, hasher: S) -> SegmentedCache<K, V, S> where
S: BuildHasher + Clone + Send + Sync + 'static,
pub fn build_with_hasher<S>(self, hasher: S) -> SegmentedCache<K, V, S> where
S: BuildHasher + Clone + Send + Sync + 'static,
Builds a SegmentedCache<K, V, S>
, with the given hasher
.
If you want to build a Cache<K, V>
, do not call segments
method before
calling this method.
Sets the initial capacity of the cache.
Sets the time to live of the cache.
A cached entry will be expired after the specified duration past from
insert
.
Sets the time to idle of the cache.
A cached entry will be expired after the specified duration past from get
or insert
.
Enables support for Cache::invalidate_entries_if method.
The cache will maintain additional internal data structures to support
invalidate_entries_if
method.