pub struct TtlCache<K, V>{ /* 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 implementEq + Hash + Clone.V– value type.
Implementations§
Source§impl<K, V> TtlCache<K, V>
impl<K, V> TtlCache<K, V>
Sourcepub fn new(capacity: usize, default_ttl_secs: u64) -> Self
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.
Sourcepub fn insert(&mut self, key: K, value: V, now_secs: u64)
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.
Sourcepub fn insert_with_ttl(
&mut self,
key: K,
value: V,
ttl_secs: u64,
now_secs: u64,
)
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.
Sourcepub fn get(&mut self, key: &K, now_secs: u64) -> Option<&V>
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.
Sourcepub fn remove(&mut self, key: &K) -> bool
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).
Sourcepub fn remove_expired(&mut self, now_secs: u64) -> usize
pub fn remove_expired(&mut self, now_secs: u64) -> usize
Purge all expired entries and return the number removed.
Sourcepub fn is_empty(&self, now_secs: u64) -> bool
pub fn is_empty(&self, now_secs: u64) -> bool
Returns true when there are no non-expired entries.
Sourcepub fn stats(&self, now_secs: u64) -> TtlCacheStats
pub fn stats(&self, now_secs: u64) -> TtlCacheStats
Return a statistics snapshot for the given now_secs.