SegmentedCache

Struct 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§

Source§

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

Source

pub fn new( probationary_size: usize, protected_size: usize, ) -> Result<Self, CacheError>

Create a SegmentedCache with size and default configurations.

Source

pub fn builder( probationary_size: usize, protected_size: usize, ) -> SegmentedCacheBuilder

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

impl<K: Hash + Eq, V, FH: BuildHasher, RH: BuildHasher> SegmentedCache<K, V, FH, RH>

Source

pub fn from_builder( builder: SegmentedCacheBuilder<FH, RH>, ) -> Result<Self, CacheError>

Create a SegmentedCache 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);
Source

pub fn put_protected(&mut self, k: K, v: V) -> PutResult<K, V>

put_protected will force to put an entry in protected LRU

Source

pub fn peek_lru_from_probationary(&mut self) -> Option<(&K, &V)>

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.

Source

pub fn peek_lru_mut_from_probationary(&mut self) -> Option<(&K, &mut V)>

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.

Source

pub fn peek_mru_from_probationary(&mut self) -> Option<(&K, &V)>

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.

Source

pub fn peek_mru_mut_from_probationary(&mut self) -> Option<(&K, &mut V)>

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.

Source

pub fn peek_lru_from_protected(&self) -> Option<(&K, &V)>

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.

Source

pub fn peek_lru_mut_from_protected(&mut self) -> Option<(&K, &mut V)>

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.

Source

pub fn peek_mru_from_protected(&self) -> Option<(&K, &V)>

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.

Source

pub fn peek_mru_mut_from_protected(&mut self) -> Option<(&K, &mut V)>

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.

Source

pub fn remove_lru_from_probationary(&mut self) -> Option<(K, V)>

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

Source

pub fn remove_lru_from_protected(&mut self) -> Option<(K, V)>

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

Source

pub fn protected_len(&self) -> usize

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

Source

pub fn probationary_len(&self) -> usize

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

Source

pub fn probationary_cap(&self) -> usize

Returns the capacity of probationary LRU.

Source

pub fn protected_cap(&self) -> usize

Returns the capacity of protected LRU.

Trait Implementations§

Source§

impl<K: Hash + Eq, V, FH: BuildHasher, RH: BuildHasher> Cache<K, V> for SegmentedCache<K, V, FH, RH>

Source§

fn put(&mut self, k: K, v: V) -> PutResult<K, V>

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

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

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

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

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

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

fn peek_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 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"));
Source§

fn contains<Q>(&self, k: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

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

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

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

fn purge(&mut self)

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

fn len(&self) -> usize

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

fn cap(&self) -> usize

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

fn is_empty(&self) -> bool

Returns a bool indicating whether the cache is empty or not.
Source§

impl<K: Hash + Eq + Clone, V: Clone, FH: BuildHasher + Clone, RH: BuildHasher + Clone> Clone for SegmentedCache<K, V, FH, RH>

Source§

fn clone(&self) -> Self

Returns a duplicate 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, FH, RH> Freeze for SegmentedCache<K, V, FH, RH>
where RH: Freeze, FH: Freeze,

§

impl<K, V, FH, RH> RefUnwindSafe for SegmentedCache<K, V, FH, RH>

§

impl<K, V, FH, RH> Send for SegmentedCache<K, V, FH, RH>
where K: Send, V: Send, RH: Send, FH: Send,

§

impl<K, V, FH, RH> Sync for SegmentedCache<K, V, FH, RH>
where K: Sync, V: Sync, RH: Sync, FH: Sync,

§

impl<K, V, FH, RH> Unpin for SegmentedCache<K, V, FH, RH>
where RH: Unpin, FH: Unpin,

§

impl<K, V, FH, RH> UnwindSafe for SegmentedCache<K, V, FH, RH>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V