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

A time sensitive cache.

Implementations§

source§

impl<K: Eq + Hash, V> TtlCache<K, V>

source

pub fn new(capacity: usize) -> Self

Creates an empty cache that can hold at most capacity items.

Examples
use ttl_cache::TtlCache;

let mut cache: TtlCache<i32, &str> = TtlCache::new(10);
source§

impl<K: Eq + Hash, V, S: BuildHasher> TtlCache<K, V, S>

source

pub fn with_hasher(capacity: usize, hash_builder: S) -> Self

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

source

pub fn contains_key<Q>(&self, key: &Q) -> boolwhere K: Borrow<Q>, Q: Hash + Eq + ?Sized,

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

pub fn insert(&mut self, k: K, v: V, ttl: Duration) -> Option<V>

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

pub fn get<Q>(&self, k: &Q) -> Option<&V>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

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

pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

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

pub fn remove<Q>(&mut self, k: &Q) -> Option<V>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

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

pub 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(2);
assert_eq!(cache.capacity(), 2);
source

pub 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(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);
source

pub fn clear(&mut self)

Clears all values out of the cache

source

pub fn entry(&mut self, k: K) -> Entry<'_, K, V, S>

source

pub 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(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)]);
source

pub 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(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));
source

pub fn remove_expired(&mut self)

This will evict all expired records in the cache. Typically expired values will remain in cache until they are accessed and then they are cleaned up. Calling remove_expired will have no impact on what values we can access in the cache, it will only hurry up the removal of expired values. One reason why you would want to call this is if the values stored in the cache implement Drop and you want to trigger that behavior (i.e. free up file handles sooner).

Trait Implementations§

source§

impl<K, V> Clone for TtlCache<K, V>where K: Clone + Eq + Hash, V: Clone,

source§

fn clone(&self) -> TtlCache<K, V>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<K, V, S> RefUnwindSafe for TtlCache<K, V, S>where K: RefUnwindSafe, S: RefUnwindSafe, V: RefUnwindSafe,

§

impl<K, V, S> Send for TtlCache<K, V, S>where K: Send, S: Send, V: Send,

§

impl<K, V, S> Sync for TtlCache<K, V, S>where K: Sync, S: Sync, V: Sync,

§

impl<K, V, S> Unpin for TtlCache<K, V, S>where S: Unpin,

§

impl<K, V, S> UnwindSafe for TtlCache<K, V, S>where K: RefUnwindSafe, S: UnwindSafe, V: RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.