#data-structures

lru-cache

A cache that holds a limited number of key-value pairs

9 releases

0.1.1 Mar 29, 2017
0.1.0 Oct 7, 2016
0.0.7 Feb 19, 2016
0.0.6 Jan 29, 2016
0.0.2 Mar 26, 2015

#1 in Caching

Download history 7056/week @ 2018-11-18 7175/week @ 2018-11-25 8708/week @ 2018-12-02 8918/week @ 2018-12-09 8034/week @ 2018-12-16 5355/week @ 2018-12-23 6555/week @ 2018-12-30 13724/week @ 2019-01-06 12620/week @ 2019-01-13 11240/week @ 2019-01-20 12136/week @ 2019-01-27 12159/week @ 2019-02-03 13676/week @ 2019-02-10 12730/week @ 2019-02-17 15042/week @ 2019-02-24

17,006 downloads per month
Used in 222 crates (27 directly)

MIT/Apache

18KB
291 lines

A cache that holds a limited number of key-value pairs.

Documentation is available at https://contain-rs.github.io/lru-cache/lru_cache.


lib.rs:

A cache that holds a limited number of key-value pairs. When the capacity of the cache is exceeded, the least-recently-used (where "used" means a look-up or putting the pair into the cache) pair is automatically removed.

Examples

use lru_cache::LruCache;

let mut cache = LruCache::new(2);

cache.insert(1, 10);
cache.insert(2, 20);
cache.insert(3, 30);
assert!(cache.get_mut(&1).is_none());
assert_eq!(*cache.get_mut(&2).unwrap(), 20);
assert_eq!(*cache.get_mut(&3).unwrap(), 30);

cache.insert(2, 22);
assert_eq!(*cache.get_mut(&2).unwrap(), 22);

cache.insert(6, 60);
assert!(cache.get_mut(&3).is_none());

cache.set_capacity(1);
assert!(cache.get_mut(&2).is_none());

Dependencies

~131KB