22 unstable releases (3 breaking)

0.4.0 Oct 28, 2019
0.2.0 Sep 27, 2019
0.1.16 Jul 25, 2019
0.1.13 Mar 12, 2019
0.1.1 Dec 31, 2016

#3 in Caching

Download history 990/week @ 2019-07-15 1198/week @ 2019-07-22 1482/week @ 2019-07-29 1293/week @ 2019-08-05 926/week @ 2019-08-12 1274/week @ 2019-08-19 1340/week @ 2019-08-26 1444/week @ 2019-09-02 1832/week @ 2019-09-09 2645/week @ 2019-09-16 2781/week @ 2019-09-23 1987/week @ 2019-09-30 1960/week @ 2019-10-07 2025/week @ 2019-10-14 2319/week @ 2019-10-21

7,745 downloads per month
Used in 21 crates (20 directly)

MIT license

45KB
874 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

~385KB