pub struct TimedMap<K, V, TS = Instant> { /* private fields */ }Expand description
Provides a hash map with expiring key-value pairs.
§Basic Example
use timedmap::TimedMap;
use std::time::Duration;
let tm = TimedMap::new();
tm.insert("foo", "bar", Duration::from_secs(10));
assert_eq!(tm.get(&"foo"), Some("bar"));Implementations§
Source§impl<K, V> TimedMap<K, V>
impl<K, V> TimedMap<K, V>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new instance of TimedMap with the default
TimeSource implementation Instant.
Source§impl<K, V, TS> TimedMap<K, V, TS>
impl<K, V, TS> TimedMap<K, V, TS>
Sourcepub fn new_with_timesource() -> Self
pub fn new_with_timesource() -> Self
Create a new instance of TimedMap with a custom
TimeSource implementation.
Source§impl<K, V, TS> TimedMap<K, V, TS>
impl<K, V, TS> TimedMap<K, V, TS>
Sourcepub fn insert(&self, key: K, value: V, lifetime: Duration)
pub fn insert(&self, key: K, value: V, lifetime: Duration)
Add a new key-value pair to the map with the given lifetime.
When the lifetime has passed, the key-value pair will be no more accessible.
§Example
use timedmap::TimedMap;
use std::time::Duration;
let tm = TimedMap::new();
tm.insert("foo", "bar", Duration::from_millis(10));
assert_eq!(tm.get(&"foo"), Some("bar"));
std::thread::sleep(Duration::from_millis(20));
assert_eq!(tm.get(&"foo"), None);Sourcepub fn contains(&self, key: &K) -> bool
pub fn contains(&self, key: &K) -> bool
Returns true when the map contains a non-expired
value for the given key.
§Behavior
Because this method is basically a shorthand for get(key).is_some(), it behaves the same on retrival of expired pairs.
Sourcepub fn remove<Q>(&self, key: &Q) -> Option<V>
pub fn remove<Q>(&self, key: &Q) -> Option<V>
Removes the given key-value pair from the map and returns the value if it was previously in the map and is not expired.
Sourcepub fn refresh(&self, key: &K, new_lifetime: Duration) -> bool
pub fn refresh(&self, key: &K, new_lifetime: Duration) -> bool
Sets the lifetime of the value coresponding to the given key to the new lifetime from now.
Returns true if a non-expired value exists for the
given key.
Sourcepub fn extend(&self, key: &K, added_lifetime: Duration) -> bool
pub fn extend(&self, key: &K, added_lifetime: Duration) -> bool
Extends the lifetime of the value coresponding to the given key to the new lifetime from now.
Returns true if a non-expired value exists for the
given key.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of key-value pairs in the map which have not been expired.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true when the map does not contain any
non-expired key-value pair.
Sourcepub fn snapshot<B: FromIterator<(K, V)>>(&self) -> B
pub fn snapshot<B: FromIterator<(K, V)>>(&self) -> B
Create a snapshot of the current state of the maps key-value entries.
It does only contain all non-expired key-value pairs.