18 releases

0.1.17 Aug 21, 2019
0.1.16 Jul 25, 2019
0.1.15 Apr 13, 2019
0.1.13 Mar 12, 2019
0.1.1 Dec 31, 2016

#3 in Caching

Download history 1181/week @ 2019-05-08 1903/week @ 2019-05-15 1517/week @ 2019-05-22 1138/week @ 2019-05-29 1177/week @ 2019-06-05 1320/week @ 2019-06-12 1546/week @ 2019-06-19 1102/week @ 2019-06-26 824/week @ 2019-07-03 1201/week @ 2019-07-10 941/week @ 2019-07-17 1321/week @ 2019-07-24 1448/week @ 2019-07-31 1149/week @ 2019-08-07 978/week @ 2019-08-14

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

MIT license

44KB
830 lines

LRU Cache

Travis Badge Codecov Badge crates.io Badge docs.rs Badge License Badge

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

    assert_eq!(cache.put("banana", 4), Some(2));
    assert_eq!(cache.put("pear", 5), None);

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

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

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

Dependencies

~295KB