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]
pub fn new(capacity: usize) -> Self
[src]
Creates an empty cache that can hold at most capacity
items.
Examples
use std::time::Duration; use ttl_cache::TtlCache; let mut cache: TtlCache<i32, &str> = TtlCache::new(10);
impl<K: Eq + Hash, V, S: BuildHasher> TtlCache<K, V, S>
[src]
pub fn with_hasher(capacity: usize, hash_builder: S) -> Self
[src]
Creates an empty cache that can hold at most capacity
items
that expire after duration
with the given hash builder.
pub fn contains_key<Q: ?Sized>(&mut self, key: &Q) -> bool where
K: Borrow<Q>,
Q: Hash + Eq,
[src]
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(10); cache.insert(1,"a", Duration::from_secs(30)); assert_eq!(cache.contains_key(&1), true);
pub fn insert(&mut self, k: K, v: V, ttl: Duration) -> Option<V>
[src]
Inserts a key-value pair into the cache with an individual ttl for the key. 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(2); cache.insert(1, "a", Duration::from_secs(20)); cache.insert(2, "b", Duration::from_secs(60)); assert_eq!(cache.get_mut(&1), Some(&mut "a")); assert_eq!(cache.get_mut(&2), Some(&mut "b"));
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: Hash + Eq,
[src]
K: Borrow<Q>,
Q: Hash + Eq,
Returns a 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(2); let duration = Duration::from_secs(30); cache.insert(1, "a", duration); cache.insert(2, "b", duration); cache.insert(2, "c", duration); cache.insert(3, "d", duration); assert_eq!(cache.get_mut(&1), None); assert_eq!(cache.get_mut(&2), Some(&mut "c"));
pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Hash + Eq,
[src]
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(2); let duration = Duration::from_secs(30); cache.insert(1, "a", duration); cache.insert(2, "b", duration); cache.insert(2, "c", duration); cache.insert(3, "d", duration); assert_eq!(cache.get_mut(&1), None); assert_eq!(cache.get_mut(&2), Some(&mut "c"));
pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> where
K: Borrow<Q>,
Q: Hash + Eq,
[src]
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(2); cache.insert(2, "a", Duration::from_secs(30)); assert_eq!(cache.remove(&1), None); assert_eq!(cache.remove(&2), Some("a")); assert_eq!(cache.remove(&2), None);
pub fn capacity(&self) -> usize
[src]
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(2); assert_eq!(cache.capacity(), 2);
pub fn set_capacity(&mut self, capacity: usize)
[src]
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(2); let duration = Duration::from_secs(30); cache.insert(1, "a", duration); cache.insert(2, "b", duration); cache.insert(3, "c", duration); 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", duration); cache.insert(2, "b", duration); 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);
pub fn clear(&mut self)
[src]
Clears all values out of the cache
ⓘImportant traits for Iter<'a, K, V>pub fn iter(&mut self) -> Iter<K, V>
[src]
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(2); let duration = Duration::from_secs(30); cache.insert(1, 10, duration); cache.insert(2, 20, duration); cache.insert(3, 30, duration); let kvs: Vec<_> = cache.iter().collect(); assert_eq!(kvs, [(&2, &20), (&3, &30)]);
ⓘImportant traits for IterMut<'a, K, V>pub fn iter_mut(&mut self) -> IterMut<K, V>
[src]
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(2); let duration = Duration::from_secs(30); cache.insert(1, 10, duration); cache.insert(2, 20, duration); cache.insert(3, 30, duration); 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));
pub fn reset_stats_counter(&mut self)
[src]
The cache will keep track of some basic stats during its usage that can be helpful for performance tuning or monitoring. This method will reset these counters.
Examples
use std::thread::sleep; use std::time::Duration; use ttl_cache::TtlCache; let mut cache = TtlCache::new(2); cache.insert(1, "a", Duration::from_secs(20)); cache.insert(2, "b", Duration::from_millis(1)); sleep(Duration::from_millis(10)); let _ = cache.get_mut(&1); let _ = cache.get_mut(&2); let _ = cache.get_mut(&3); assert_eq!(cache.miss_count(), 2); cache.reset_stats_counter(); assert_eq!(cache.miss_count(), 0);
pub fn hit_count(&self) -> u32
[src]
Returns the number of unexpired cache hits since the last time the counters were reset.
Examples
use std::thread::sleep; use std::time::Duration; use ttl_cache::TtlCache; let mut cache = TtlCache::new(2); cache.insert(1, "a", Duration::from_secs(20)); cache.insert(2, "b", Duration::from_millis(1)); sleep(Duration::from_millis(10)); assert!(cache.get_mut(&1).is_some()); assert!(cache.get_mut(&2).is_none()); assert!(cache.get_mut(&3).is_none()); assert_eq!(cache.hit_count(), 1);
pub fn miss_count(&self) -> u32
[src]
Returns the number of cache misses since the last time the counters were reset. Entries that have expired count as a miss.
Examples
use std::thread::sleep; use std::time::Duration; use ttl_cache::TtlCache; let mut cache = TtlCache::new(2); cache.insert(1, "a", Duration::from_secs(20)); cache.insert(2, "b", Duration::from_millis(1)); sleep(Duration::from_millis(10)); let _ = cache.get_mut(&1); let _ = cache.get_mut(&2); let _ = cache.get_mut(&3); assert_eq!(cache.miss_count(), 2);
pub fn stats_since(&self) -> Instant
[src]
Returns the Instant when we started gathering stats. This is either when the cache was created or when it was last reset, whichever happened most recently.