Skip to main content

TtlCache

Struct TtlCache 

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

A simple TTL-based cache using a step counter as a clock.

Each entry has a ttl expressed in “steps”. The cache is checked on every get; stale entries are lazily removed.

Implementations§

Source§

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

Source

pub fn new(default_ttl: u64) -> Self

Create a new TTL cache.

Source

pub fn tick(&mut self)

Advance the clock by one step.

Source

pub fn tick_n(&mut self, n: u64)

Advance the clock by n steps.

Source

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

Insert an entry with the default TTL.

Source

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

Insert an entry with a custom TTL.

Source

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

Get an entry if it hasn’t expired.

Source

pub fn purge_expired(&mut self)

Remove expired entries.

Source

pub fn len(&self) -> usize

Number of entries (including possibly stale).

Source

pub fn is_empty(&self) -> bool

Whether the cache is empty.

Source

pub fn current_step(&self) -> u64

Current step counter.

Source

pub fn clear(&mut self)

Clear all 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.