Struct ttl_cache::TtlCache
[−]
[src]
pub struct TtlCache<K: Eq + Hash, V, S: BuildHasher = RandomState> { /* fields omitted */ }
A time sensitive cache.
Methods
impl<K: Eq + Hash, V> TtlCache<K, V>
[src]
impl<K: Eq + Hash, V, S: BuildHasher> TtlCache<K, V, S>
[src]
fn with_hasher(duration: Duration, capacity: usize, hash_builder: S) -> Self
Creates an empty cache that can hold at most capacity
items
that expire after duration
with the given hash builder.
fn contains_key<Q: ?Sized>(&mut self, key: &Q) -> bool where K: Borrow<Q>, Q: Hash + Eq
Check if the cache contains the given key.
Examples
use std::time::Duration; use ttl_cache::TtlCache; let mut cache = TtlCache::new(Duration::from_secs(30), 10); cache.insert(1,"a"); assert_eq!(cache.contains_key(&1), true);Run
fn insert(&mut self, k: K, v: V) -> Option<V>
Inserts a key-value pair into the cache. If the key already existed and hasn't expired, the old value is returned.
Examples
use std::time::Duration; use ttl_cache::TtlCache; let mut cache = TtlCache::new(Duration::from_secs(30), 2); cache.insert(1, "a"); cache.insert(2, "b"); assert_eq!(cache.get_mut(&1), Some(&mut "a")); assert_eq!(cache.get_mut(&2), Some(&mut "b"));Run
fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> where K: Borrow<Q>, Q: Hash + Eq
Returns a mutable reference to the value corresponding to the given key in the cache, if it contains an unexpired entry.
Examples
use std::time::Duration; use ttl_cache::TtlCache; let mut cache = TtlCache::new(Duration::from_secs(30), 2); cache.insert(1, "a"); cache.insert(2, "b"); cache.insert(2, "c"); cache.insert(3, "d"); assert_eq!(cache.get_mut(&1), None); assert_eq!(cache.get_mut(&2), Some(&mut "c"));Run
fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> where K: Borrow<Q>, Q: Hash + Eq
Removes the given key from the cache and returns its corresponding value.
Examples
use std::time::Duration; use ttl_cache::TtlCache; let mut cache = TtlCache::new(Duration::from_secs(30), 2); cache.insert(2, "a"); assert_eq!(cache.remove(&1), None); assert_eq!(cache.remove(&2), Some("a")); assert_eq!(cache.remove(&2), None);Run
fn capacity(&self) -> usize
Returns the maximum number of key-value pairs the cache can hold.
Examples
use std::time::Duration; use ttl_cache::TtlCache; let mut cache: TtlCache<i32, &str> = TtlCache::new(Duration::from_secs(30), 2); assert_eq!(cache.capacity(), 2);Run
fn set_capacity(&mut self, capacity: usize)
Sets the number of key-value pairs the cache can hold. Removes oldest key-value pairs if necessary.
Examples
use std::time::Duration; use ttl_cache::TtlCache; let mut cache = TtlCache::new(Duration::from_secs(30), 2); cache.insert(1, "a"); cache.insert(2, "b"); cache.insert(3, "c"); assert_eq!(cache.get_mut(&1), None); assert_eq!(cache.get_mut(&2), Some(&mut "b")); assert_eq!(cache.get_mut(&3), Some(&mut "c")); cache.set_capacity(3); cache.insert(1, "a"); cache.insert(2, "b"); assert_eq!(cache.get_mut(&1), Some(&mut "a")); assert_eq!(cache.get_mut(&2), Some(&mut "b")); assert_eq!(cache.get_mut(&3), Some(&mut "c")); cache.set_capacity(1); assert_eq!(cache.get_mut(&1), None); assert_eq!(cache.get_mut(&2), Some(&mut "b")); assert_eq!(cache.get_mut(&3), None);Run
fn clear(&mut self)
Clears all values out of the cache
fn iter(&mut self) -> Iter<K, V>
Returns an iterator over the cache's key-value pairs in oldest to youngest order.
Examples
use std::time::Duration; use ttl_cache::TtlCache; let mut cache = TtlCache::new(Duration::from_secs(30), 2); cache.insert(1, 10); cache.insert(2, 20); cache.insert(3, 30); let kvs: Vec<_> = cache.iter().collect(); assert_eq!(kvs, [(&2, &20), (&3, &30)]);Run
fn iter_mut(&mut self) -> IterMut<K, V>
Returns an iterator over the cache's key-value pairs in oldest to youngest order with mutable references to the values.
Examples
use std::time::Duration; use ttl_cache::TtlCache; let mut cache = TtlCache::new(Duration::from_secs(30), 2); cache.insert(1, 10); cache.insert(2, 20); cache.insert(3, 30); let mut n = 2; for (k, v) in cache.iter_mut() { assert_eq!(*k, n); assert_eq!(*v, n * 10); *v *= 10; n += 1; } assert_eq!(n, 4); assert_eq!(cache.get_mut(&2), Some(&mut 200)); assert_eq!(cache.get_mut(&3), Some(&mut 300));Run
Trait Implementations
impl<K: Clone + Eq + Hash, V: Clone, S: Clone + BuildHasher> Clone for TtlCache<K, V, S>
[src]
fn clone(&self) -> TtlCache<K, V, S>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<K: Eq + Hash, V, S: BuildHasher> Extend<(K, V)> for TtlCache<K, V, S>
[src]
fn extend<I: IntoIterator<Item=(K, V)>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more