33 releases

Uses old Rust 2015

0.6.5 Feb 12, 2021
0.6.3 Dec 19, 2020
0.6.1 Nov 2, 2020
0.5.3 Jul 6, 2020
0.1.1 Dec 31, 2016

#2 in Caching

Download history 31848/week @ 2020-11-07 32868/week @ 2020-11-14 31207/week @ 2020-11-21 31323/week @ 2020-11-28 28613/week @ 2020-12-05 29662/week @ 2020-12-12 20374/week @ 2020-12-19 17032/week @ 2020-12-26 31613/week @ 2021-01-02 34144/week @ 2021-01-09 32706/week @ 2021-01-16 33430/week @ 2021-01-23 33200/week @ 2021-01-30 34544/week @ 2021-02-06 36788/week @ 2021-02-13 35348/week @ 2021-02-20

128,886 downloads per month
Used in 218 crates (79 directly)

MIT license

50KB
976 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

~420KB