9 releases

0.1.2 Mar 21, 2019
0.1.1 Mar 29, 2017
0.1.0 Oct 7, 2016
0.0.7 Feb 19, 2016
0.0.2 Mar 26, 2015

#4 in #data-structures

Download history 28314/week @ 2019-10-13 26833/week @ 2019-10-20 27443/week @ 2019-10-27 30760/week @ 2019-11-03 29286/week @ 2019-11-10 29640/week @ 2019-11-17 27765/week @ 2019-11-24 32725/week @ 2019-12-01 38568/week @ 2019-12-08 33285/week @ 2019-12-15 17326/week @ 2019-12-22 24838/week @ 2019-12-29 35484/week @ 2020-01-05 34820/week @ 2020-01-12 35186/week @ 2020-01-19

126,034 downloads per month
Used in 513 crates (32 directly)

MIT/Apache

19KB
291 lines

WARNING: THIS PROJECT IS IN MAINTENANCE MODE, DUE TO INSUFFICIENT MAINTAINER RESOURCES

It works fine, but will generally no longer be improved.

We are currently only accepting changes which:

  • keep this compiling with the latest versions of Rust or its dependencies.
  • have minimal review requirements, such as documentation changes (so not totally new APIs).

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

~66KB