hashlru 0.5.1

HashLru is an experimental LRU cache.
Documentation

HashLru

HashLru is an experimental LRU cache implemented in Rust.

It tries to follow the API exposed by a standard Rust HashMap while enforcing a limited memory footprint by limiting the number of keys using the LRU strategy, which is a quite common cache replacement policy.

HashLru icon

Status

For now this is a toy project, clearly NOT suitable for production use.

There are many other libraries you could use instead:

  • lru is faster, and has support for mutable iterators, among other things. See doc and source.
  • cached comes with batteries included, has support for many other features than just LRU. See doc and source.

The latest implementation uses ideas from this nice doubly-linked-list tutorial. It is 100% safe Rust, though it does have some runtime checks.

Here is a quick bench done on a 100k items map:

$ cargo bench
    Finished bench [optimized] target(s) in 0.02s
     Running unittests src/lib.rs (target/release/deps/benches-dd7cea60774e4207)

running 6 tests
test tests::bench_read_usize_hashlru  ... bench:          65 ns/iter (+/- 13)
test tests::bench_read_usize_hashmap  ... bench:          13 ns/iter (+/- 0)
test tests::bench_read_usize_lru      ... bench:          10 ns/iter (+/- 0)
test tests::bench_write_usize_hashlru ... bench:         109 ns/iter (+/- 21)
test tests::bench_write_usize_hashmap ... bench:          66 ns/iter (+/- 13)
test tests::bench_write_usize_lru     ... bench:          24 ns/iter (+/- 4)

test result: ok. 0 passed; 0 failed; 0 ignored; 6 measured; 0 filtered out; finished in 22.51s

Those results are not super reliable, just a one-chost test ran on a laptop. However there is a tendency: hashlru is the slowest, lru performs best (probably because it keeps the number of items below 100k) and standard hashmap is in between.

Proof this is a toy project.

Build Status

Usage

use hashlru::Cache;

let mut lru = Cache::new(4);
lru.insert("key1", 10);
lru.insert("key2", 20);
lru.insert("key3", 30);
lru.insert("key4", 40);
lru.insert("key5", 50);
// key1 has been dropped, size is limited to 4
assert_eq!("key2", lru.lru().unwrap());
assert_eq!(Some(&20), lru.get(&"key2"));
// getting key2 has made key3 the least recently used item
assert_eq!("key3", lru.lru().unwrap());
assert_eq!(Some(&40), lru.get(&"key4"));
// getting key4 makes it the most recently used item
assert_eq!("[key3: 30, key5: 50, key2: 20, key4: 40]", format!("{}", lru));

License

HashLru is licensed under the MIT license.