pub struct ExpiringCacheBuilder<K, V, S = DefaultHashBuilder> { /* private fields */ }Expand description
Builder for ExpiringCache.
Note: there is intentionally no .ttl() setter. An ExpiringCache has no global
expiry duration – each value decides when it is expired via the Expires trait. For a
single global TTL applied to every entry, use TtlCache or
LruTtlCache instead.
Implementations§
Source§impl<K, V> ExpiringCacheBuilder<K, V>
impl<K, V> ExpiringCacheBuilder<K, V>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a builder with default settings. Equivalent to ExpiringCache::builder.
Source§impl<K, V, S> ExpiringCacheBuilder<K, V, S>
impl<K, V, S> ExpiringCacheBuilder<K, V, S>
Sourcepub fn initial_capacity(self, capacity: usize) -> Self
pub fn initial_capacity(self, capacity: usize) -> Self
Set the initial allocation capacity (optional).
Sourcepub fn on_evict(self, on_evict: impl Fn(&K, &V) + Send + Sync + 'static) -> Self
pub fn on_evict(self, on_evict: impl Fn(&K, &V) + Send + Sync + 'static) -> Self
Set a callback to be invoked when an entry is removed from the cache.
The callback fires when an expired value is encountered during cache_get,
cache_get_mut, cache_get_or_set_with_mut, cache_try_get_or_set_with_mut
(the primary implementations), cache_get_or_set_with, cache_try_get_or_set_with
(default-impl wrappers that delegate to the _mut variants),
their async equivalents, an explicit evict() sweep, or an explicit
cache_remove (including when the removed entry was already expired).
It does not fire on cache_clear or cache_reset (consistent with
ExpiringLruCache).
Use cache_clear_with_on_evict
instead of cache_clear to opt into callback
firing and eviction counter increments when clearing all entries.
Sourcepub fn hasher<S2: BuildHasher>(
self,
hasher: S2,
) -> ExpiringCacheBuilder<K, V, S2>
pub fn hasher<S2: BuildHasher>( self, hasher: S2, ) -> ExpiringCacheBuilder<K, V, S2>
Switch to a custom hash builder S2, returning a builder parameterized on S2.
The hasher is used to hash keys in the internal UnboundCache. Calling this method
changes the builder’s type parameter so build() returns an ExpiringCache<K, V, S2>.
§Example
use cached::{Cached, Expires, ExpiringCache};
use std::collections::hash_map::RandomState;
struct Val(bool);
impl Expires for Val { fn is_expired(&self) -> bool { self.0 } }
let mut cache = ExpiringCache::<u32, Val>::builder()
.hasher(RandomState::new())
.build()
.unwrap();
cache.cache_set(1, Val(false));
assert!(cache.cache_get(&1).is_some());Sourcepub fn build(self) -> Result<ExpiringCache<K, V, S>, BuildError>
pub fn build(self) -> Result<ExpiringCache<K, V, S>, BuildError>
Build the cache.
ExpiringCache has no required fields and this call never fails.
§Errors
This method currently never returns an error.