12 releases

0.1.11 Dec 11, 2018
0.1.10 Nov 7, 2018
0.1.9 Oct 30, 2018
0.1.8 Aug 19, 2018
0.1.1 Dec 31, 2016

#5 in Caching

Download history 228/week @ 2018-10-25 479/week @ 2018-11-01 629/week @ 2018-11-08 578/week @ 2018-11-15 481/week @ 2018-11-22 494/week @ 2018-11-29 824/week @ 2018-12-06 715/week @ 2018-12-13 754/week @ 2018-12-20 765/week @ 2018-12-27 409/week @ 2019-01-03 424/week @ 2019-01-10 622/week @ 2019-01-17

2,311 downloads per month
Used in 5 crates (4 directly)

MIT license

36KB
656 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