timed_map

Struct TimedMap

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

Associates keys of type K with values of type V. Each entry may optionally expire after a specified duration.

Mutable functions automatically clears expired entries when called.

If no expiration is set, the entry remains constant.

Implementations§

Source§

impl<C, K, V> TimedMap<C, K, V>
where C: Clock, K: GenericKey,

Source

pub fn new() -> Self

Creates an empty map.

Source

pub fn new_with_map_kind(map_kind: MapKind) -> Self

Creates an empty map based on the chosen map implementation specified by MapKind.

Source

pub fn expiration_tick_cap(self, expiration_tick_cap: u16) -> Self

Configures expiration_tick_cap, which sets how often TimedMap::drop_expired_entries is automatically called. The default value is 1.

On each insert (excluding unchecked ones), an internal counter expiration_tick is incremented. When expiration_tick meets or exceeds expiration_tick_cap, TimedMap::drop_expired_entries is triggered to remove expired entries.

Use this to control cleanup frequency and optimize performance. For example, if your workload involves about 100 inserts within couple seconds, setting expiration_tick_cap to 100 can improve the performance significantly.

Source

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

Returns the associated value if present and not expired.

To retrieve the value without checking expiration, use TimedMap::get_unchecked.

Source

pub fn get_mut(&mut self, k: &K) -> Option<&mut V>

Returns a mutable reference to the value corresponding to the key.

To retrieve the value without checking expiration, use TimedMap::get_unchecked.

Source

pub fn get_unchecked(&self, k: &K) -> Option<&V>

Returns the associated value if present, regardless of whether it is expired.

If you only want non-expired entries, use TimedMap::get instead.

Source

pub fn get_unchecked_mut(&mut self, k: &K) -> Option<&mut V>

Returns a mutable reference to the associated value if present, regardless of whether it is expired.

If you only want non-expired entries, use TimedMap::get instead.

Source

pub fn get_remaining_duration(&self, k: &K) -> Option<Duration>

Returns the associated value’s Duration if present and not expired.

Returns None if the entry does not exist or is constant.

Source

pub fn insert_expirable(&mut self, k: K, v: V, duration: Duration) -> Option<V>

Inserts a key-value pair with an expiration duration, and then drops the expired entries.

If a value already exists for the given key, it will be updated and then the old one will be returned.

If you don’t want to the check expired entries, consider using TimedMap::insert_expirable_unchecked instead.

Source

pub fn insert_expirable_unchecked( &mut self, k: K, v: V, duration: Duration, ) -> Option<V>

Inserts a key-value pair with an expiration duration, without checking the expired entries.

If a value already exists for the given key, it will be updated and then the old one will be returned.

If you want to check the expired entries, consider using TimedMap::insert_expirable instead.

Source

pub fn insert_constant(&mut self, k: K, v: V) -> Option<V>

Inserts a key-value pair with that doesn’t expire, and then drops the expired entries.

If a value already exists for the given key, it will be updated and then the old one will be returned.

If you don’t want to check the expired entries, consider using TimedMap::insert_constant_unchecked instead.

Source

pub fn insert_constant_unchecked(&mut self, k: K, v: V) -> Option<V>

Inserts a key-value pair with that doesn’t expire without checking the expired entries.

If a value already exists for the given key, it will be updated and then the old one will be returned.

If you want to check the expired entries, consider using TimedMap::insert_constant instead.

Source

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

Removes a key-value pair from the map and returns the associated value if present and not expired.

If you want to retrieve the entry after removal even if it is expired, consider using TimedMap::remove_unchecked.

Source

pub fn remove_unchecked(&mut self, k: &K) -> Option<V>

Removes a key-value pair from the map and returns the associated value if present, regardless of expiration status.

If you only want the entry when it is not expired, consider using TimedMap::remove.

Source

pub fn clear(&mut self)

Clears the map, removing all elements.

Source

pub fn drop_expired_entries(&mut self)

Clears expired entries from the map.

Call this function when using *_unchecked inserts, as these do not automatically clear expired entries.

Trait Implementations§

Source§

impl<C: Debug, K: Debug, V: Debug> Debug for TimedMap<C, K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<C, K, V> Default for TimedMap<C, K, V>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<C, K, V> Freeze for TimedMap<C, K, V>

§

impl<C, K, V> RefUnwindSafe for TimedMap<C, K, V>

§

impl<C, K, V> Send for TimedMap<C, K, V>
where C: Send, K: Send, V: Send,

§

impl<C, K, V> Sync for TimedMap<C, K, V>
where C: Sync, K: Sync, V: Sync,

§

impl<C, K, V> Unpin for TimedMap<C, K, V>
where C: Unpin, K: Unpin, V: Unpin,

§

impl<C, K, V> UnwindSafe for TimedMap<C, 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.