9 releases

Uses old Rust 2015

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

#27 in Caching

Download history 457259/week @ 2024-10-04 442097/week @ 2024-10-11 484794/week @ 2024-10-18 477757/week @ 2024-10-25 432256/week @ 2024-11-01 460805/week @ 2024-11-08 477414/week @ 2024-11-15 402437/week @ 2024-11-22 387585/week @ 2024-11-29 494483/week @ 2024-12-06 460569/week @ 2024-12-13 180791/week @ 2024-12-20 174789/week @ 2024-12-27 397363/week @ 2025-01-03 507215/week @ 2025-01-10 328272/week @ 2025-01-17

1,428,641 downloads per month
Used in 1,716 crates (73 directly)

MIT/Apache

20KB
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

~220KB