Expand description
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());The cache can also be limited by an arbitrary metric calculated from its key-value pairs, see
LruCache::with_meter for more information. If the heapsize feature is enabled,
this crate provides one such alternate metric—HeapSize. Custom metrics can be written by
implementing the Meter trait.
Structs§
- Count
- Size limit based on a simple count of cache items.
- Into
Iter - An iterator over a cache’s key-value pairs in least- to most-recently-used order.
- Iter
- An iterator over a cache’s key-value pairs in least- to most-recently-used order.
- IterMut
- An iterator over a cache’s key-value pairs in least- to most-recently-used order with mutable references to the values.
- LruCache
- An LRU cache.
Traits§
- Countable
Meter - A trait to allow the default
Countmeasurement to not store an extraneous counter. - Countable
Meter With Measure - Meter
- A trait for measuring the size of a cache entry.