pub struct ByteLruCache<K, V> { /* private fields */ }Expand description
A thread-safe LRU cache governed by a caller-supplied byte budget.
Values are stored behind Arc so callers can hold a value without
copying it. Each entry carries a strictly positive weight. The cache
evicts least-recently-used entries until the sum of weights is at most
max_bytes.
The weight is an accounting value supplied by the caller. It should be a
conservative estimate of the memory retained by the value; the cache
cannot measure an arbitrary V’s heap allocation itself.
Implementations§
Source§impl<K, V> ByteLruCache<K, V>
impl<K, V> ByteLruCache<K, V>
Sourcepub fn new(max_bytes: u64, label: impl Into<String>) -> Self
pub fn new(max_bytes: u64, label: impl Into<String>) -> Self
Creates an empty cache with the supplied byte ceiling and label.
A zero ceiling is valid and causes every inserted entry to be immediately evicted. This is useful when a caller disables a cache without changing its construction path.
Sourcepub fn get(&self, key: &K) -> Option<Arc<V>>
pub fn get(&self, key: &K) -> Option<Arc<V>>
Looks up a key and promotes a hit to the most-recently-used position.
§Panics
Panics if another thread poisoned the cache lock.
Sourcepub fn insert(&self, key: K, value: Arc<V>, weight_bytes: NonZeroU64)
pub fn insert(&self, key: K, value: Arc<V>, weight_bytes: NonZeroU64)
Inserts or replaces an entry and evicts least-recently-used entries until the byte ceiling is satisfied.
The weight_bytes argument is non-zero by construction, so the
cache’s entry count cannot grow without consuming the configured
byte budget.
§Panics
Panics if another thread poisoned the cache lock.
Sourcepub fn current_bytes(&self) -> u64
pub fn current_bytes(&self) -> u64
Returns the sum of the weights currently retained by the cache.
§Panics
Panics if another thread poisoned the cache lock.
Sourcepub fn set_max_bytes(&self, max_bytes: u64)
pub fn set_max_bytes(&self, max_bytes: u64)
Changes the byte ceiling and immediately evicts entries if necessary.
§Panics
Panics if another thread poisoned the cache lock.
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Returns a point-in-time copy of the cache counters.