generational-lru

ci codecov rustdoc Crates.io

Crate providing a 100% safe, generational arena based LRU cache implementation.

```rust use generational_lru::lrucache::{LRUCache, CacheError};

let capacity = 5;

let mut lrucache = LRUCache::::withcapacity(capacity); asserteq!(lrucache.query(&0), Err(CacheError::CacheMiss));

for ele in 0..capacity { let x = ele as i32; assert!(lrucache.insert(x, x).isok()); }

for ele in 0..capacity { let x = ele as i32; asserteq!(lrucache.query(&x), Ok(&x)); }

let x = capacity as i32; assert!(lrucache.insert(x, x).isok());

asserteq!(lrucache.query(&x), Ok(&x));

asserteq!(lrucache.query(&0), Err(CacheError::CacheMiss));

let x = capacity as i32 / 2; asserteq!(lrucache.remove(&x), Ok(x));

asserteq!(lrucache.query(&x), Err(CacheError::CacheMiss)); asserteq!(lrucache.remove(&x), Err(CacheError::CacheMiss));

// zero capacity LRUCache is unusable let mut lrucache = LRUCache::::withcapacity(0);

assert!(matches!( lrucache.insert(0, 0), Err(CacheError::CacheBroken()) ));

```

Refer to API documentation for more details.

Usage

This is a library crate. You may include it in your Cargo.toml as follows: toml [dependencies] generational-lru = "0.1"

License

This repository is licensed under the MIT License. See LICENSE for the full license text.