9 releases

0.1.8 Aug 19, 2018
0.1.7 Jan 23, 2018
0.1.4 Feb 20, 2017
0.1.3 Jan 2, 2017
0.1.1 Dec 31, 2016

#4 in Caching

Download history 25/week @ 2018-05-27 146/week @ 2018-06-03 306/week @ 2018-06-10 136/week @ 2018-06-17 268/week @ 2018-06-24 244/week @ 2018-07-01 251/week @ 2018-07-08 276/week @ 2018-07-15 113/week @ 2018-07-22 239/week @ 2018-07-29 341/week @ 2018-08-05 360/week @ 2018-08-12 176/week @ 2018-08-19

960 downloads per month

MIT license

28KB
467 lines

LRU Cache

Build Status codecov crates.io docs.rs License

Documentation

An implementation of a LRU cache. The cache supports put, get, get_mut and pop operations, all of which are O(1). This crate was heavily influenced by the LRU Cache implementation in an earlier version of Rust's std::collections crate.

Example

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

extern crate lru;

use lru::LruCache;

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

        assert_eq!(*cache.get(&"apple").unwrap(), 3);
        assert_eq!(*cache.get(&"banana").unwrap(), 2);
        assert!(cache.get(&"pear").is_none());

        cache.put("pear", 4);

        assert_eq!(*cache.get(&"pear").unwrap(), 4);
        assert_eq!(*cache.get(&"banana").unwrap(), 2);
        assert!(cache.get(&"apple").is_none());

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

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

No runtime deps