pub struct TtlMap<K, V>{ /* private fields */ }Expand description
A HashMap whose entries expire after ttl.
Implementations§
Source§impl<K, V> TtlMap<K, V>
impl<K, V> TtlMap<K, V>
Sourcepub fn new(ttl: Duration) -> Self
pub fn new(ttl: Duration) -> Self
Creates a new map with the given TTL.
A ttl of zero disables expiry entirely — entries stay until
explicitly removed.
Sourcepub fn with_on_expire<F>(self, cb: F) -> Self
pub fn with_on_expire<F>(self, cb: F) -> Self
Sets a callback invoked whenever an entry is removed due to TTL expiry.
The registry uses this to reject pending request promises with a timeout error.
Sourcepub fn insert(&self, key: K, value: V) -> Option<V>
pub fn insert(&self, key: K, value: V) -> Option<V>
Inserts a value, returning the previous entry if any.
Sourcepub fn remove(&self, key: &K) -> Option<V>
pub fn remove(&self, key: &K) -> Option<V>
Removes and returns the value for key, bypassing expiry.
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Returns whether key is present and unexpired.
Triggers on_expire as a side effect if the entry is stale.
Sourcepub fn get_cloned(&self, key: &K) -> Option<V>where
V: Clone,
pub fn get_cloned(&self, key: &K) -> Option<V>where
V: Clone,
Returns a clone of the value for key if present and unexpired.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the current size of the map (including any stale entries that have not yet been touched).
pub fn is_empty(&self) -> bool
Sourcepub fn sweep_expired(&self)
pub fn sweep_expired(&self)
Removes every entry that has exceeded the TTL, firing
on_expire for each. Callers may invoke this periodically to
bound memory in long-running processes.