Skip to main content

TtlCache

Struct TtlCache 

Source
pub struct TtlCache<K, V> { /* private fields */ }
Available on crate feature std only.
Expand description

A bounded, thread-safe cache with per-entry time-to-live.

Each entry is stamped with a deadline at insert time. On every access (get, contains_key, len), expired entries are removed lazily. On overflow, the entry with the soonest expiration is evicted — already-expired entries are naturally preferred over live ones.

Both insert and insert_with_ttl reset the deadline on the affected entry — writes always re-arm the timer.

§Implementation

Sharded into up to 16 independent stores keyed by hash of K. Each shard owns its own HashMap and applies expiry / overflow eviction locally. Tiny caches (< 32 entries) use a single shard.

Eviction is per-shard approximate — overflow inside one shard evicts the soonest-expiring entry within that shard, not necessarily the soonest-expiring entry globally. Lazy expiry remains exact within the operating shard.

§Example

use std::time::Duration;
use cache_mod::{Cache, TtlCache};

let cache: TtlCache<&'static str, u32> =
    TtlCache::new(4, Duration::from_secs(60)).expect("capacity > 0");

cache.insert("session", 42);
assert_eq!(cache.get(&"session"), Some(42));

Implementations§

Source§

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

Source

pub fn new(capacity: usize, ttl: Duration) -> Result<Self, CacheError>

Creates a cache with the given capacity and default time-to-live.

ttl is applied to every insert that does not specify its own. Returns CacheError::InvalidCapacity if capacity == 0.

§Example
use std::time::Duration;
use cache_mod::TtlCache;

let cache: TtlCache<String, u32> =
    TtlCache::new(128, Duration::from_secs(300)).expect("capacity > 0");
Source

pub fn with_capacity(capacity: NonZeroUsize, ttl: Duration) -> Self

Creates a cache with the given non-zero capacity and default TTL. Infallible.

§Example
use std::num::NonZeroUsize;
use std::time::Duration;
use cache_mod::TtlCache;

let cap = NonZeroUsize::new(64).expect("64 != 0");
let cache: TtlCache<String, u32> =
    TtlCache::with_capacity(cap, Duration::from_secs(60));
Source

pub fn insert_with_ttl(&self, key: K, value: V, ttl: Duration) -> Option<V>

Inserts value under key with a per-call TTL that overrides the cache default. The deadline is now + ttl.

Returns the previously-stored live value if key was already present and not yet expired. An expired-but-not-yet-cleaned entry is treated as absent: the call returns None and replaces it.

§Example
use std::time::Duration;
use cache_mod::{Cache, TtlCache};

let cache: TtlCache<u32, u32> =
    TtlCache::new(4, Duration::from_secs(60)).expect("capacity > 0");

cache.insert_with_ttl(1, 10, Duration::from_secs(5));
assert_eq!(cache.get(&1), Some(10));

Trait Implementations§

Source§

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

Source§

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

Returns the value associated with key, if any, and counts as an access for the purposes of the eviction policy.
Source§

fn insert(&self, key: K, value: V) -> Option<V>

Inserts value under key. Returns the previously-stored value if key was already present. Read more
Source§

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

Removes the entry for key and returns the value if present.
Source§

fn contains_key(&self, key: &K) -> bool

Returns true if the cache currently holds an entry for key. Read more
Source§

fn len(&self) -> usize

Number of entries currently stored. Read more
Source§

fn clear(&self)

Removes every entry. Capacity is preserved. Read more
Source§

fn capacity(&self) -> usize

Configured capacity bound. Read more
Source§

fn is_empty(&self) -> bool

Returns true when the cache holds no 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: Send, V: Send,

§

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

§

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

§

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

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.