weight-cache
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.
Contrary to the lru-cache crate (which
this crate is heavily inspired by!), the capacity is not the number of items
in the cache, but can be given by an arbitrary criterion by implementing
[Weighable
] for the value type V. A straight-forward example of this would
be to use the allocated size of the object, and provide a total capacity
which must not be exceeded by the cache.
Examples
use ;
use NonZeroUsize;
let mut cache = new;
// Can't put too much in!
assert!;
assert!;
cache.put.unwrap;
assert!;
assert_eq!;
cache.put.unwrap;
assert_eq!;
assert_eq!;
cache.put.unwrap;
assert_eq!;
assert!; // 1 has been touched last
assert_eq!;
Feature flags
metrics
: Enables metric gathering on the cache. Register a [prometheus::Registry
] with a call to [WeightCache::register
]; set a custom metric namespace with [WeightCache::new_with_namespace
]
License: MIT OR Apache-2.0