Skip to main content

TtlCache

Struct TtlCache 

Source
pub struct TtlCache<K, V> { /* private fields */ }
Expand description

A concurrent cache with per-entry TTL eviction.

Entries are automatically considered expired after their TTL elapses. Expired entries are lazily evicted on access.

Implementations§

Source§

impl<K: Eq + Hash + Clone, V: Clone> TtlCache<K, V>

Source

pub fn new(ttl: Duration) -> Self

Create a new cache with the given TTL for all entries.

Source

pub fn get(&self, key: &K) -> Option<V>

Get a value if it exists and has not expired.

Source

pub fn insert(&self, key: K, value: V)

Insert a value with the configured TTL.

Source

pub fn remove(&self, key: &K) -> Option<V>

Remove an entry.

Source

pub fn take_fresh(&self, key: &K) -> Option<V>

Remove an entry only if it has not expired, returning its value.

Unlike TtlCache::remove (which returns the value even when expired), this is the single-use-consume primitive: an expired entry is dropped and reported as absent — exactly the semantics a CSRF state store or one-time token cache needs.

Source

pub fn cleanup(&self)

Remove all expired entries.

Source

pub fn len(&self) -> usize

Return the number of entries (including expired ones).

Source

pub fn is_empty(&self) -> bool

Return true if the cache is empty.

Trait Implementations§

Source§

impl<K: Eq + Hash + Clone, V: Clone> Default for TtlCache<K, V>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<K, V> !RefUnwindSafe for TtlCache<K, V>

§

impl<K, V> Freeze for TtlCache<K, V>
where DashMap<K, CacheEntry<V>>: Freeze,

§

impl<K, V> Send for TtlCache<K, V>
where DashMap<K, CacheEntry<V>>: Send,

§

impl<K, V> Sync for TtlCache<K, V>
where DashMap<K, CacheEntry<V>>: Sync,

§

impl<K, V> Unpin for TtlCache<K, V>
where DashMap<K, CacheEntry<V>>: Unpin,

§

impl<K, V> UnsafeUnpin for TtlCache<K, V>
where DashMap<K, CacheEntry<V>>: UnsafeUnpin,

§

impl<K, V> UnwindSafe for TtlCache<K, V>
where DashMap<K, CacheEntry<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 = !

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.