fast-lru 0.1.2

A fast, 100% safe, stack based LRU cache
Documentation
  • Coverage
  • 83.33%
    10 out of 12 items documented7 out of 12 items with examples
  • Size
  • Source code size: 18.1 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 372.5 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 8s Average build duration of successful builds.
  • all releases: 8s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • LeadFreeCandy/lru
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • LeadFreeCandy

fast-lru

A fast, 100% safe, stack based least recently used cache.

fast-lru uses a stack based array to store all the values, in conjunction with a hashmap to store the keys. It gaurentees O(1) time complexity for all operations, including get(), put(), get_mut(), and pop().

Example

This a simple example of creating a cache, adding some values, and then reading them.

use lru::LruCache

fn main() {
    let mut cache: LruCache<_, _, 2> = LruCache::new();

    cache.put("cow", 3);
    cache.put("pig", 2);

    assert_eq!(*cache.get(&"cow").unwrap(), 3);
    assert_eq!(*cache.get(&"pig").unwrap(), 2);
    assert!(cache.get(&"dog").is_none());

    assert_eq!(cache.put("pig", 4), Some(2));
    assert_eq!(cache.put("dog", 5), None);

    assert_eq!(*cache.get(&"dog").unwrap(), 5);
    assert_eq!(*cache.get(&"pig").unwrap(), 4);
    assert!(cache.get(&"cow").is_none());

    {
        let v = cache.get_mut(&"pig").unwrap();
        *v = 6;
    }

    assert_eq!(*cache.get(&"pig").unwrap(), 6);
}