Expand description

Module providing abstractions to represent an LRUCache.

Usage

use generational_cache::prelude::*;

const CAPACITY: usize = 3;

let mut cache = LRUCache::<_, i32, u64, AllocBTreeMap<_, _>>::with_backing_vector(Array::<_, CAPACITY>::new());

cache.insert(-1, 1).unwrap();
cache.insert(-2, 2).unwrap();
cache.insert(-3, 3).unwrap();

assert_eq!(cache.least_recent().unwrap(), (&-1, &1));
assert_eq!(cache.most_recent().unwrap(), (&-3, &3));

assert_eq!(cache.insert(-4, 4).unwrap(), Eviction::Block { key: -1, value: 1});

assert_eq!(cache.least_recent().unwrap(), (&-2, &2));
assert_eq!(cache.most_recent().unwrap(), (&-4, &4));

assert_eq!(cache.insert(-2, 42).unwrap(), Eviction::Value(2));

assert_eq!(cache.least_recent().unwrap(), (&-3, &3));
assert_eq!(cache.most_recent().unwrap(), (&-2, &42));

assert_eq!(cache.remove(&-42).unwrap(), Lookup::Miss);
assert_eq!(cache.query(&-42).unwrap(), Lookup::Miss);

assert_eq!(cache.query(&-3).unwrap(), Lookup::Hit(&3));

assert_eq!(cache.least_recent().unwrap(), (&-4, &4));
assert_eq!(cache.most_recent().unwrap(), (&-3, &3));

assert_eq!(cache.remove(&-2).unwrap(), Lookup::Hit(42));

assert_eq!(cache.query(&-2).unwrap(), Lookup::Miss);

// zero capacity LRUCache is unusable
let mut cache = LRUCache::<_, i32, u64, AllocBTreeMap<_, _>>::with_backing_vector(Array::<_, 0_usize>::new());

match cache.insert(0, 0) {
    Err(LRUCacheError::ListUnderflow) => {}
    _ => unreachable!("Wrong error on list underflow."),
};

Structs

  • A cache block containing a key value pair.
  • A generational Arena backed LRU cache implementation.

Enums

Type Aliases