Struct caches::SegmentedCache

source ·
pub struct SegmentedCache<K, V, FH = DefaultHashBuilder, RH = DefaultHashBuilder> { /* private fields */ }
Expand description

SegmentedCache is a fixed size Segmented LRU Cache.

Example

use caches::{Cache, SegmentedCache};

let mut cache = SegmentedCache::new(2, 2).unwrap();

cache.put(1, 1);
cache.put(2, 2);

assert_eq!(cache.probationary_len(), 2);
assert_eq!(cache.protected_len(), 0);

assert_eq!(cache.get(&1), Some(&1));
*cache.get_mut(&2).unwrap() = 22;

assert_eq!(cache.probationary_len(), 0);
assert_eq!(cache.protected_len(), 2);

cache.put(3, 3);
cache.put(4, 4);

assert_eq!(cache.probationary_len(), 2);
assert_eq!(cache.protected_len(), 2);

assert_eq!(cache.peek(&3), Some(&3));
assert_eq!(cache.peek_mut(&4), Some(&mut 4));

assert_eq!(cache.probationary_len(), 2);
assert_eq!(cache.protected_len(), 2);

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

cache.purge();
assert_eq!(cache.len(), 0);

Implementations

Create a SegmentedCache with size and default configurations.

Returns a SegmentedCacheBuilder to help build a SegmentedCache.

Example
use caches::{Cache, SegmentedCacheBuilder, SegmentedCache};
use rustc_hash::FxHasher;
use std::hash::BuildHasherDefault;

let mut cache = SegmentedCache::<u64, u64>::builder(3, 3)
    .set_probationary_hasher(BuildHasherDefault::<FxHasher>::default())
    .set_protected_hasher(BuildHasherDefault::<FxHasher>::default())
    .finalize()
    .unwrap();

cache.put(1, 1);

Create a [AdaptiveCache] from SegmentedCacheBuilder.

Example
use caches::{Cache, SegmentedCache, SegmentedCacheBuilder};
use rustc_hash::FxHasher;
use std::hash::BuildHasherDefault;

let builder = SegmentedCacheBuilder::new(5, 5);

let mut cache = SegmentedCache::from_builder(builder).unwrap();
cache.put(1, 1);

put_protected will force to put an entry in protected LRU

Returns the value corresponding to the least recently used item or None if the cache is empty. Like peek, peek_lru_from_probationary does not update the probationary LRU list so the item’s position will be unchanged.

Returns the mutable value corresponding to the least recently used item or None if the cache is empty. Like peek, peek_lru_mut_from_probationary does not update the probationary LRU list so the item’s position will be unchanged.

Returns the value corresponding to the most recently used item or None if the cache is empty. Like peek, peek_mru_from_probationary does not update the probationary LRU list so the item’s position will be unchanged.

Returns the mutable value corresponding to the most recently used item or None if the cache is empty. Like peek, peek_mru_mut_from_probationary does not update the probationary LRU list so the item’s position will be unchanged.

Returns the value corresponding to the least recently used item or None if the cache is empty. Like peek, peek_lru_from_protected does not update the protected LRU list so the item’s position will be unchanged.

Returns the mutable value corresponding to the least recently used item or None if the cache is empty. Like peek, peek_lru_mut_from_protected does not update the protected LRU list so the item’s position will be unchanged.

Returns the value corresponding to the most recently used item or None if the cache is empty. Like peek, peek_mru_from_protected does not update the protected LRU list so the item’s position will be unchanged.

Returns the mutable value corresponding to the most recently used item or None if the cache is empty. Like peek, peek_mru_mut_from_protected does not update the protected LRU list so the item’s position will be unchanged.

Removes and returns the key and value corresponding to the least recently used item or None if the probationary cache is empty.

Removes and returns the key and value corresponding to the least recently used item or None if the protected cache is empty.

Returns the number of key-value pairs that are currently in the protected LRU.

Returns the number of key-value pairs that are currently in the probationary LRU.

Returns the capacity of probationary LRU.

Returns the capacity of protected LRU.

Trait Implementations

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

Example
use caches::{Cache, SegmentedCache};
use caches::PutResult;
let mut cache = SegmentedCache::new(2, 2).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 if it is not present in the cache. Moves the key to the head of the protected segment LRU list if it exists.

Example
use caches::{Cache, SegmentedCache};
let mut cache = SegmentedCache::new(2, 2).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 if it is not present in the cache. Moves the key to the head of the protected segment LRU list if it exists.

Example
use caches::{Cache, SegmentedCache};
let mut cache = SegmentedCache::new(2, 2).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, SegmentedCache};
let mut cache = SegmentedCache::new(2, 2).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, peek does not update the LRU list so the key’s position will be unchanged.

Example
use caches::{Cache, SegmentedCache};
let mut cache = SegmentedCache::new(2, 2).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.

Example
use caches::{Cache, SegmentedCache};
let mut cache = SegmentedCache::new(2, 2).unwrap();

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

assert!(!cache.contains(&1));
assert!(cache.contains(&2));
assert!(cache.contains(&3));

Removes and returns the value corresponding to the key from the cache or None if it does not exist.

Example
use caches::{Cache, SegmentedCache};

let mut cache = SegmentedCache::new(2, 2).unwrap();

cache.put(2, "a");

assert_eq!(cache.remove(&1), None);
assert_eq!(cache.remove(&2), Some("a"));
assert_eq!(cache.remove(&2), None);
assert_eq!(cache.len(), 0);

Clears the contents of the cache.

Example
use caches::{SegmentedCache, Cache};

let mut cache: SegmentedCache<isize, &str> = SegmentedCache::new(2, 2).unwrap();
assert_eq!(cache.len(), 0);

cache.put(1, "a");
assert_eq!(cache.len(), 1);

cache.put(2, "b");
assert_eq!(cache.len(), 2);

cache.purge();
assert_eq!(cache.len(), 0);

Returns the number of key-value pairs that are currently in the the cache.

Example
use caches::lru::SegmentedCache;
use caches::Cache;
let mut cache = SegmentedCache::new(2, 2).unwrap();
assert_eq!(cache.len(), 0);

cache.put(1, "a");
assert_eq!(cache.len(), 1);

cache.put(2, "b");
assert_eq!(cache.len(), 2);

// because no entry is in protected LRU, so this operation will evict
cache.put(3, "c");
assert_eq!(cache.len(), 2);

// now 3 is put in protected LRU
cache.put(3, "cc");
assert_eq!(cache.len(), 2);

// we can put 4-"d" in probationary LRU, and the size of cache is 3
cache.put(4, "d");
assert_eq!(cache.len(), 3);

Returns the maximum number of key-value pairs the cache can hold.

Example
use caches::lru::SegmentedCache;
use caches::Cache;
let mut cache: SegmentedCache<isize, &str> = SegmentedCache::new(2, 2).unwrap();
assert_eq!(cache.cap(), 4);
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.