#key-value #lru-cache #lru #pair #numbers #limited #hold

lru-cache-map

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

1 unstable release

0.2.0 Oct 28, 2024

#293 in Caching

Download history 963/week @ 2024-10-30 962/week @ 2024-11-06 1037/week @ 2024-11-13 1393/week @ 2024-11-20 857/week @ 2024-11-27 790/week @ 2024-12-04 620/week @ 2024-12-11 1052/week @ 2024-12-18 816/week @ 2024-12-25 1091/week @ 2025-01-01 870/week @ 2025-01-08 562/week @ 2025-01-15 325/week @ 2025-01-22 130/week @ 2025-01-29 716/week @ 2025-02-05 562/week @ 2025-02-12

1,836 downloads per month
Used in rotbl

MIT/Apache

37KB
700 lines

lru-cache-map

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

This is a mirror of mozilla/sccache/lru-cache, which is derived from contain-rs/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

#
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());

The cache can also be limited by an arbitrary metric calculated from its key-value pairs, see LruCache::with_meter for more information. Custom metrics can be written by implementing the Meter trait.

Dependencies

~2MB
~30K SLoC