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.
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.
Usage
use Cache;
let mut lru = new;
lru.insert;
lru.insert;
lru.insert;
lru.insert;
lru.insert;
// key1 has been dropped, size is limited to 4
assert_eq!;
assert_eq!;
// getting key2 has made key3 the least recently used item
assert_eq!;
assert_eq!;
// getting key4 makes it the most recently used item
assert_eq!;
License
HashLru is licensed under the MIT license.