#LRU #cache

lru

A LRU cache implementation

16 releases

0.1.15 Apr 13, 2019
0.1.13 Mar 12, 2019
0.1.11 Dec 11, 2018
0.1.10 Nov 7, 2018
0.1.1 Dec 31, 2016

#5 in Caching

Download history 691/week @ 2019-01-29 611/week @ 2019-02-05 1072/week @ 2019-02-12 803/week @ 2019-02-19 1348/week @ 2019-02-26 1160/week @ 2019-03-05 1480/week @ 2019-03-12 1665/week @ 2019-03-19 1511/week @ 2019-03-26 679/week @ 2019-04-02 1647/week @ 2019-04-09 1331/week @ 2019-04-16 1377/week @ 2019-04-23 998/week @ 2019-04-30 1157/week @ 2019-05-07

5,729 downloads per month
Used in 15 crates (14 directly)

MIT license

42KB
793 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);
}

Dependencies

~407KB