Struct ttl_cache::TtlCache

source ·
pub struct TtlCache<K: Eq + Hash, V, S: BuildHasher = RandomState> { /* private fields */ }
Expand description

A time sensitive cache.

Implementations

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);

Creates an empty cache that can hold at most capacity items that expire after duration with the given hash builder.

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);

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(&1), Some(&"a"));
assert_eq!(cache.get(&2), Some(&"b"));

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(&1), None);
assert_eq!(cache.get(&2), Some(&"c"));

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"));

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);

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);

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(&1), None);
assert_eq!(cache.get(&2), Some(&"b"));
assert_eq!(cache.get(&3), Some(&"c"));

cache.set_capacity(3);
cache.insert(1, "a", duration);
cache.insert(2, "b", duration);

assert_eq!(cache.get(&1), Some(&"a"));
assert_eq!(cache.get(&2), Some(&"b"));
assert_eq!(cache.get(&3), Some(&"c"));

cache.set_capacity(1);

assert_eq!(cache.get(&1), None);
assert_eq!(cache.get(&2), Some(&"b"));
assert_eq!(cache.get(&3), None);

Clears all values out of the cache

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)]);

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(&2), Some(&200));
assert_eq!(cache.get(&3), Some(&300));

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.