Skip to main content

Cache

Struct Cache 

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

Cache of messages that we have already seen.

This is used to avoid sending the same message multiple times.

Implementations§

Source§

impl<K, V> Cache<K, V>

Source

pub fn with_capacity_and_ttl(capacity: usize, ttl: Duration) -> Self

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

Source§

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

Source

pub fn put(&mut self, id: K, message: V) -> bool

Inserts a message in the cache.

Returns true if the message was not already in the cache. Returns false if the message was already in the cache.

If the source is None, then the message is assumed to have been sent by us.

Source

pub fn len(&self) -> usize

Returns the number of non-expired messages in the cache.

Source

pub fn is_empty(&self) -> bool

Returns if the cache is empty.

Source

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

Returns true if the cache contains a non-expired message with the given ID.

Source

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

Returns a reference to the message with the given ID, if it exists in the cache and has not expired.

Source

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

Removes the message with the given ID from the cache.

Returns the removed cache entry, if it existed in the cache and had not expired.

Source

pub fn clear_expired_entries(&mut self)

Remove all expired messages from the cache.

An entry is considered expired if the elapsed time since the insertion of the entry is greater than the time-to-live of the cache, then the entry is considered expired.

Trait Implementations§

Source§

impl<K, V> Default for Cache<K, V>

Source§

fn default() -> Self

Creates a new empty cache.

Capacity defaults to 1024 messages and a time-to-live to 5 seconds.

Auto Trait Implementations§

§

impl<K, V> Freeze for Cache<K, V>
where LinkedHashMap<K, CacheEntry<V>>: Freeze,

§

impl<K, V> RefUnwindSafe for Cache<K, V>
where LinkedHashMap<K, CacheEntry<V>>: RefUnwindSafe,

§

impl<K, V> Send for Cache<K, V>
where LinkedHashMap<K, CacheEntry<V>>: Send,

§

impl<K, V> Sync for Cache<K, V>
where LinkedHashMap<K, CacheEntry<V>>: Sync,

§

impl<K, V> Unpin for Cache<K, V>
where LinkedHashMap<K, CacheEntry<V>>: Unpin,

§

impl<K, V> UnsafeUnpin for Cache<K, V>
where LinkedHashMap<K, CacheEntry<V>>: UnsafeUnpin,

§

impl<K, V> UnwindSafe for Cache<K, V>
where LinkedHashMap<K, CacheEntry<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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.