Skip to main content

ExpiringCacheBuilder

Struct ExpiringCacheBuilder 

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

Source

pub fn new() -> Self

Create a builder with default settings. Equivalent to ExpiringCache::builder.

Source§

impl<K, V, S> ExpiringCacheBuilder<K, V, S>

Source

pub fn initial_capacity(self, capacity: usize) -> Self

Set the initial allocation capacity (optional).

Source

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.

Source

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());
Source

pub fn build(self) -> Result<ExpiringCache<K, V, S>, BuildError>
where K: Hash + Eq, S: BuildHasher,

Build the cache.

ExpiringCache has no required fields and this call never fails.

§Errors

This method currently never returns an error.

Trait Implementations§

Source§

impl<K, V> Default for ExpiringCacheBuilder<K, V, DefaultHashBuilder>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

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

§

impl<K, V, S = RandomState> !UnwindSafe for ExpiringCacheBuilder<K, V, S>

§

impl<K, V, S> Freeze for ExpiringCacheBuilder<K, V, S>
where Option<Arc<dyn Fn(&K, &V) + Send + Sync>>: Freeze, S: Freeze,

§

impl<K, V, S> Send for ExpiringCacheBuilder<K, V, S>
where Option<Arc<dyn Fn(&K, &V) + Send + Sync>>: Send, S: Send,

§

impl<K, V, S> Sync for ExpiringCacheBuilder<K, V, S>
where Option<Arc<dyn Fn(&K, &V) + Send + Sync>>: Sync, S: Sync,

§

impl<K, V, S> Unpin for ExpiringCacheBuilder<K, V, S>
where Option<Arc<dyn Fn(&K, &V) + Send + Sync>>: Unpin, S: Unpin,

§

impl<K, V, S> UnsafeUnpin for ExpiringCacheBuilder<K, V, S>
where Option<Arc<dyn Fn(&K, &V) + Send + Sync>>: UnsafeUnpin, S: UnsafeUnpin,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.