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:

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

Build Status

Usage

```rust 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 asserteq!("key2", lru.lru().unwrap()); asserteq!(Some(&20), lru.get(&"key2")); // getting key2 has made key3 the least recently used item asserteq!("key3", lru.lru().unwrap()); asserteq!(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.