generational-cache

Generational Arena based cache impls. in 100% safe, [no_std] compatible Rust.

Usage

generational-cache is a library crate. You may include it in your Cargo.toml as follows:

toml [dependencies] generational-cache = "0.1.0"

Refer to latest git API Documentation or Crate Documentation for more details.

Examples

#1: Generational arena based LRU cache implementation

```rust

[no_std]

use generational_cache::prelude::*;

const CAPACITY: usize = 3;

// users can choose between different map and vector implementations let mut cache = LRUCache::<_, i32, u64, AllocBTreeMap<_, _>>::withbackingvector(Array::<_, CAPACITY>::new());

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

asserteq!(cache.leastrecent().unwrap(), (&-1, &1)); asserteq!(cache.mostrecent().unwrap(), (&-3, &3));

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

asserteq!(cache.leastrecent().unwrap(), (&-2, &2)); asserteq!(cache.mostrecent().unwrap(), (&-4, &4));

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

asserteq!(cache.leastrecent().unwrap(), (&-3, &3)); asserteq!(cache.mostrecent().unwrap(), (&-2, &42));

match cache.remove(&-42) { Err(LRUCacheError::CacheMiss) => {}, _ => unreachable!("Wrong error on cache miss"), };

match cache.query(&-42) { Err(LRUCacheError::CacheMiss) => {}, _ => unreachable!("Wrong error on cache miss"), };

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

asserteq!(cache.leastrecent().unwrap(), (&-4, &4)); asserteq!(cache.mostrecent().unwrap(), (&-3, &3));

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

match cache.query(&-2) { Err(LRUCacheError::CacheMiss) => {}, _ => unreachable!("Wrong error on cache miss"), };

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

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

```

(… we plan on adding more cache implementations in the future).

License

This repository is licensed under the MIT License. See LICENSE for more details.