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>
impl<K, V> Cache<K, V>
Sourcepub fn with_capacity_and_ttl(capacity: usize, ttl: Duration) -> Self
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>
impl<K, V> Cache<K, V>
Sourcepub fn put(&mut self, id: K, message: V) -> bool
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.
Sourcepub fn contains_key(&self, id: &K) -> bool
pub fn contains_key(&self, id: &K) -> bool
Returns true if the cache contains a non-expired message with the given ID.
Sourcepub fn get(&self, id: &K) -> Option<&V>
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.
Sourcepub fn remove(&mut self, id: &K) -> Option<V>
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.
Sourcepub fn clear_expired_entries(&mut self)
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.