Skip to main content

TtlCache

Struct TtlCache 

Source
pub struct TtlCache<K, V>
where K: Eq + Hash + Clone,
{ /* private fields */ }
Expand description

Capacity-bounded TTL cache.

Entries are evicted in FIFO insertion order when the cache reaches capacity. Expired entries are returned as absent on get and can be bulk-purged via remove_expired.

§Type parameters

  • K – key type; must implement Eq + Hash + Clone.
  • V – value type.

Implementations§

Source§

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

Source

pub fn new(capacity: usize, default_ttl_secs: u64) -> Self

Create a new TtlCache with the given capacity and default_ttl_secs.

A default_ttl_secs of 0 means entries inserted via insert never expire; per-entry TTL overrides via insert_with_ttl still apply.

§Panics

Panics if capacity == 0.

Source

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

Insert a key-value pair using the cache’s default TTL.

If the key already exists it is overwritten. When the cache is at capacity, the oldest entry (by insertion order) is evicted first.

Source

pub fn insert_with_ttl( &mut self, key: K, value: V, ttl_secs: u64, now_secs: u64, )

Insert a key-value pair with an explicit ttl_secs override.

A ttl_secs of 0 means this specific entry never expires.

Source

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

Look up key.

Returns None if the key is absent or if the entry has expired. Expired entries are lazy: they remain in memory until either remove_expired or a new insertion triggers eviction.

Source

pub fn remove(&mut self, key: &K) -> bool

Explicitly remove an entry from the cache regardless of expiry.

Returns true if the key was present (even if expired).

Source

pub fn remove_expired(&mut self, now_secs: u64) -> usize

Purge all expired entries and return the number removed.

Source

pub fn len(&self, now_secs: u64) -> usize

Count of non-expired entries at now_secs.

Source

pub fn is_empty(&self, now_secs: u64) -> bool

Returns true when there are no non-expired entries.

Source

pub fn stats(&self, now_secs: u64) -> TtlCacheStats

Return a statistics snapshot for the given now_secs.

Source

pub fn capacity(&self) -> usize

Configured maximum number of entries.

Auto Trait Implementations§

§

impl<K, V> Freeze for TtlCache<K, V>

§

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

§

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

§

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

§

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

§

impl<K, V> UnsafeUnpin for TtlCache<K, V>

§

impl<K, V> UnwindSafe for TtlCache<K, V>
where K: UnwindSafe, 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.