Struct caches::WTinyLFUCache

source ·
pub struct WTinyLFUCache<K: Hash, V, KH = DefaultKeyHasher<K>, FH = DefaultHashBuilder, RH = DefaultHashBuilder, WH = DefaultHashBuilder> { /* private fields */ }
Expand description

WTinyLFUCache implements the W-TinyLFU, based on the paper TinyLFU: A Highly Efficient Cache Admission Policy

Example

use caches::{WTinyLFUCache, PutResult, Cache};
 
let mut cache = WTinyLFUCache::with_sizes(1, 2, 2, 5).unwrap();
assert_eq!(cache.cap(), 5);
assert_eq!(cache.window_cache_cap(), 1);
assert_eq!(cache.main_cache_cap(), 4);

assert_eq!(cache.put(1, 1), PutResult::Put);
assert!(cache.contains(&1));
assert_eq!(cache.put(2, 2), PutResult::Put);
assert!(cache.contains(&2));
assert_eq!(cache.put(3, 3), PutResult::Put);
assert!(cache.contains(&3));

// current state
// window cache:        (MRU) [3] (LRU)
// probationary cache:  (MRU) [2, 1] (LRU)
// protected cache:     (MRU) [] (LRU)
assert_eq!(cache.window_cache_len(), 1);
assert_eq!(cache.main_cache_len(), 2);

assert_eq!(cache.put(2, 22), PutResult::Update(2));
assert_eq!(cache.put(1, 11), PutResult::Update(1));

// current state
// window cache:        (MRU) [3] (LRU)
// probationary cache:  (MRU) [] (LRU)
// protected cache:     (MRU) [1, 2] (LRU)
assert_eq!(cache.window_cache_len(), 1);

assert_eq!(cache.put(3, 33), PutResult::Update(3));

// current state
// window cache:        (MRU) [2] (LRU)
// probationary cache:  (MRU) [] (LRU)
// protected cache:     (MRU) [3, 1] (LRU)
assert_eq!(cache.window_cache_len(), 1);

assert_eq!(cache.put(4, 4), PutResult::Put);
assert_eq!(cache.put(5, 5), PutResult::Put);

// current state
// window cache:        (MRU) [5] (LRU)
// probationary cache:  (MRU) [4, 2] (LRU)
// protected cache:     (MRU) [3, 1] (LRU)

assert_eq!(cache.get(&4), Some(&4));
assert_eq!(cache.get_mut(&5), Some(&mut 5));

// current state
// window cache:        (MRU) [5] (LRU)
// probationary cache:  (MRU) [1, 2] (LRU)
// protected cache:     (MRU) [4, 3] (LRU)
assert_eq!(cache.peek(&3), Some(&33));
assert_eq!(cache.peek_mut(&2), Some(&mut 22));

assert_eq!(cache.remove(&3), Some(33));
assert_eq!(cache.remove(&2), Some(22));

Implementations

Returns a WTinyLFUCache based on the size and samples

NOTE: the size is not the actual cache size, the actual size is calculated base on the size param.

Returns a WTinyLFUCache based on the related cache sizes and samples

Note

According to the TinyLFU: A Highly Efficient Cache Admission Policy

Returns a WTinyLFUCacheBuilder with default configurations.

Creates a WTinyLFUCache according to WTinyLFUCacheBuilder

Trait Implementations

Puts a key-value pair into cache, returns a PutResult.

Example
use caches::{Cache, WTinyLFUCache};
use caches::PutResult;
let mut cache = WTinyLFUCache::with_sizes(1, 2, 2, 5).unwrap();

assert_eq!(PutResult::Put, cache.put(1, "a"));
assert_eq!(PutResult::Put, cache.put(2, "b"));
assert_eq!(PutResult::Update("b"), cache.put(2, "beta"));
assert_eq!(PutResult::Put, cache.put(3, "c"));

assert_eq!(cache.get(&1), Some(&"a"));
assert_eq!(cache.get(&2), Some(&"beta"));

Returns a reference to the value of the key in the cache or None.

Example
use caches::{Cache, WTinyLFUCache};
let mut cache = WTinyLFUCache::with_sizes(1, 2, 2, 5).unwrap();

cache.put("apple", 8);
cache.put("banana", 4);
cache.put("banana", 6);
cache.put("pear", 2);

assert_eq!(cache.get(&"banana"), Some(&6));

Returns a mutable reference to the value of the key in the cache or None.

Example
use caches::{Cache, WTinyLFUCache};
let mut cache = WTinyLFUCache::with_sizes(1, 2, 2, 5).unwrap();

cache.put("apple", 8);
cache.put("banana", 4);
cache.put("banana", 6);
cache.put("pear", 2);

assert_eq!(cache.get_mut(&"banana"), Some(&mut 6));

Returns a reference to the value corresponding to the key in the cache or None if it is not present in the cache. Unlike get, peek does not update the LRU list so the key’s position will be unchanged.

Example
use caches::{Cache, WTinyLFUCache};

let mut cache = WTinyLFUCache::with_sizes(1, 2, 2, 5).unwrap();

cache.put(1, "a");
cache.put(2, "b");

assert_eq!(cache.peek(&1), Some(&"a"));
assert_eq!(cache.peek(&2), Some(&"b"));

Returns a mutable reference to the value corresponding to the key in the cache or None if it is not present in the cache. Unlike get_mut, peek_mut does not update the LRU list so the key’s position will be unchanged.

Example
use caches::{Cache, WTinyLFUCache};

let mut cache = WTinyLFUCache::with_sizes(1, 2, 2, 5).unwrap();

cache.put(1, "a");
cache.put(2, "b");

assert_eq!(cache.peek_mut(&1), Some(&mut "a"));
assert_eq!(cache.peek_mut(&2), Some(&mut "b"));
Returns a bool indicating whether the given key is in the cache. Does not update the cache. Read more
Removes and returns the value corresponding to the key from the cache or None if it does not exist. Read more
Clears the contents of the cache.
Returns the number of key-value pairs that are currently in the the cache.
Returns the maximum number of key-value pairs the cache can hold.
Returns a bool indicating whether the cache is empty or not.

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 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.