pub struct TinyLfuCache<K, V> { /* private fields */ }std only.Expand description
A bounded, thread-safe cache with admission control.
TinyLfuCache tracks the access frequency of every key it observes —
including keys that aren’t (yet) in the cache — using a Count-Min Sketch.
On capacity overflow, an incoming key is admitted only if its
estimated frequency exceeds the LRU victim’s. One-hit-wonders are
rejected at the door instead of evicting hot entries.
A successful insert call does not guarantee the
value is in the cache. The admission filter may reject it. Callers that
need strict insertion guarantees should use LruCache or LfuCache.
§Implementation
Sharded into up to 16 independent arenas keyed by hash of K. Each
shard owns its own Count-Min Sketch — the frequency signal is
per-shard, not global. This is a deliberate trade-off: a global sketch
would force every access to lock a shared structure, defeating the
point of sharding. Per-shard sketches still capture the local frequency
signal accurately, which is what the local admission decision needs.
Eviction is approximate (per-shard LRU). Tiny caches (< 32 entries) use a single shard and retain strict global semantics.
§Example
use cache_mod::{Cache, TinyLfuCache};
let cache: TinyLfuCache<&'static str, u32> =
TinyLfuCache::new(4).expect("capacity > 0");
for _ in 0..16 {
let _ = cache.get(&"hot");
let _ = cache.insert("hot", 1);
}
assert_eq!(cache.get(&"hot"), Some(1));Implementations§
Source§impl<K, V> TinyLfuCache<K, V>
impl<K, V> TinyLfuCache<K, V>
Sourcepub fn new(capacity: usize) -> Result<Self, CacheError>
pub fn new(capacity: usize) -> Result<Self, CacheError>
Creates a cache with the given entry-count capacity.
Returns CacheError::InvalidCapacity if capacity == 0.
§Example
use cache_mod::TinyLfuCache;
let cache: TinyLfuCache<String, u32> =
TinyLfuCache::new(256).expect("capacity > 0");Sourcepub fn with_capacity(capacity: NonZeroUsize) -> Self
pub fn with_capacity(capacity: NonZeroUsize) -> Self
Creates a cache with the given non-zero capacity. Infallible.
§Example
use std::num::NonZeroUsize;
use cache_mod::TinyLfuCache;
let cap = NonZeroUsize::new(256).expect("256 != 0");
let cache: TinyLfuCache<String, u32> = TinyLfuCache::with_capacity(cap);Trait Implementations§
Source§impl<K, V> Cache<K, V> for TinyLfuCache<K, V>
impl<K, V> Cache<K, V> for TinyLfuCache<K, V>
Source§fn get(&self, key: &K) -> Option<V>
fn get(&self, key: &K) -> Option<V>
key, if any, and counts as an
access for the purposes of the eviction policy.Source§fn remove(&self, key: &K) -> Option<V>
fn remove(&self, key: &K) -> Option<V>
key and returns the value if present.