#lru #cache #hashmap


An LRU cache implementation with constant time operations

4 releases (2 breaking)

0.3.0 Nov 3, 2020
0.2.1 Nov 2, 2020
0.2.0 Nov 2, 2020
0.1.0 Oct 27, 2020

#30 in Caching

Used in cosmwasm-vm

MIT license



Actions Crate Docs License

An LRU cache implementation with constant time operations.

The cache is backed by a HashMap and thus offers a O(1) time complexity (amortized average) for common operations:

  • get / get_mut
  • put / pop
  • peek / peek_mut


Most of the API, documentation, examples and tests have been heavily inspired by the lru crate. I want to thank jeromefroe for his work without which this crate would have probably never has been released.

Differences with lru

The main differences are:

  • Smaller amount of unsafe code. Unsafe code is not bad in itself as long as it is thoroughly reviewed and understood but can be surprisingly hard to get right. Reducing the amount of unsafe code should hopefully reduce bugs or undefined behaviors.
  • API closer to the standard HashMap collection which allows to lookup with Borrow-ed version of the key.


Below is a simple example of how to instantiate and use this LRU cache.

use clru::CLruCache;

fn main() {
    let mut cache = CLruCache::new(2);
    cache.put("apple".to_string(), 3);
    cache.put("banana".to_string(), 2);

    assert_eq!(cache.get("apple"), Some(&3));
    assert_eq!(cache.get("banana"), Some(&2));

    assert_eq!(cache.put("banana", 4), Some(2));
    assert_eq!(cache.put("pear", 5), None);

    assert_eq!(cache.get("pear"), Some(&5));
    assert_eq!(cache.get("banana"), Some(&4));

        let v = cache.get_mut("banana").unwrap();
        *v = 6;

    assert_eq!(cache.get("banana"), Some(&6));


Each contribution is tested with regular compiler, miri, and 4 flavors of sanitizer (address, memory, thread and leak). This should help catch bugs sooner than later.


  • improve documentation and add examples
  • figure out Send/Sync traits support

No runtime deps