24 releases

0.4.3 Dec 11, 2019
0.4.1 Nov 26, 2019
0.1.16 Jul 25, 2019
0.1.13 Mar 12, 2019
0.1.1 Dec 31, 2016

#3 in Caching

Download history 2021/week @ 2019-10-04 1850/week @ 2019-10-11 2123/week @ 2019-10-18 2580/week @ 2019-10-25 2605/week @ 2019-11-01 2708/week @ 2019-11-08 2898/week @ 2019-11-15 3617/week @ 2019-11-22 4239/week @ 2019-11-29 6700/week @ 2019-12-06 5096/week @ 2019-12-13 2288/week @ 2019-12-20 2994/week @ 2019-12-27 7278/week @ 2020-01-03 6130/week @ 2020-01-10

13,877 downloads per month
Used in 29 crates (24 directly)

MIT license

46KB
882 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.

The MSRV for this crate is 1.36.0.

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

~390KB