#lru-cache #stack #safe #100

fast-lru

A fast, 100% safe, stack based LRU cache

3 releases

0.1.2 Jun 28, 2022
0.1.1 Jun 28, 2022
0.1.0 Jun 28, 2022

#214 in Caching

Download history 7/week @ 2024-01-08 4/week @ 2024-01-15 18/week @ 2024-01-22 67/week @ 2024-01-29 42/week @ 2024-02-05 64/week @ 2024-02-12 136/week @ 2024-02-19 103/week @ 2024-02-26 29/week @ 2024-03-04 25/week @ 2024-03-11 13/week @ 2024-03-18 101/week @ 2024-03-25 120/week @ 2024-04-01

263 downloads per month

MIT/Apache

19KB
329 lines

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);
}

Dependencies

~66KB