pub struct LfuCache<K, V, S = DefaultHashBuilder> { /* private fields */ }Expand description
An implementation of a Least Frequently Used (LFU) cache.
The cache tracks the frequency of access for each item and evicts the least frequently used items when the cache reaches capacity. In case of a tie in frequency, the least recently used item among those with the same frequency is evicted.
§Examples
use cache_rs::lfu::LfuCache;
use cache_rs::config::LfuCacheConfig;
use core::num::NonZeroUsize;
// Create an LFU cache with capacity 3
let config = LfuCacheConfig {
capacity: NonZeroUsize::new(3).unwrap(),
max_size: u64::MAX,
};
let mut cache = LfuCache::init(config, None);
// Add some items
cache.put("a", 1, 1);
cache.put("b", 2, 1);
cache.put("c", 3, 1);
// Access "a" multiple times to increase its frequency
assert_eq!(cache.get(&"a"), Some(&1));
assert_eq!(cache.get(&"a"), Some(&1));
// Add a new item, which will evict the least frequently used item
cache.put("d", 4, 1);
assert_eq!(cache.get(&"b"), None); // "b" was evicted as it had frequency 0Implementations§
Source§impl<K: Hash + Eq, V: Clone, S: BuildHasher> LfuCache<K, V, S>
impl<K: Hash + Eq, V: Clone, S: BuildHasher> LfuCache<K, V, S>
Sourcepub fn cap(&self) -> NonZeroUsize
pub fn cap(&self) -> NonZeroUsize
Returns the maximum number of key-value pairs the cache can hold.
Sourcepub fn current_size(&self) -> u64
pub fn current_size(&self) -> u64
Returns the current total size of cached content.
Sourcepub fn put(&mut self, key: K, value: V, size: u64) -> Option<Vec<(K, V)>>where
K: Clone,
pub fn put(&mut self, key: K, value: V, size: u64) -> Option<Vec<(K, V)>>where
K: Clone,
Inserts a key-value pair into the cache.
If the key already exists, it is replaced. If at capacity, the least frequently used item is evicted. Ties are broken by evicting the least recently used item among those with the same frequency.
New items are inserted with a frequency of 1.
§Arguments
key- The key to insertvalue- The value to cachesize- Size of this entry for capacity tracking. UseSIZE_UNIT(1) for count-based caching.
§Returns
Some(vec)containing evicted entries (not replaced entries)Noneif no entries were evicted (zero allocation)
Sourcepub fn record_miss(&mut self, object_size: u64)
pub fn record_miss(&mut self, object_size: u64)
Records a cache miss for metrics tracking (to be called by simulation system)
Sourcepub fn contains<Q>(&self, key: &Q) -> bool
pub fn contains<Q>(&self, key: &Q) -> bool
Check if key exists without updating its frequency.
Unlike get(), this method does NOT update the entry’s frequency
or access metadata. Useful for existence checks without affecting
cache eviction order.
§Example
use cache_rs::LfuCache;
use cache_rs::config::LfuCacheConfig;
use core::num::NonZeroUsize;
let config = LfuCacheConfig {
capacity: NonZeroUsize::new(2).unwrap(),
max_size: u64::MAX,
};
let mut cache = LfuCache::init(config, None);
cache.put("a", 1, 1);
cache.put("b", 2, 1);
// contains() does NOT update frequency
assert!(cache.contains(&"a"));Sourcepub fn peek<Q>(&self, key: &Q) -> Option<&V>
pub fn peek<Q>(&self, key: &Q) -> Option<&V>
Returns a reference to the value without updating frequency or access metadata.
Unlike get(), this does NOT increment the entry’s frequency
or change its position in any frequency list.
§Example
use cache_rs::LfuCache;
use cache_rs::config::LfuCacheConfig;
use core::num::NonZeroUsize;
let config = LfuCacheConfig {
capacity: NonZeroUsize::new(3).unwrap(),
max_size: u64::MAX,
};
let mut cache = LfuCache::init(config, None);
cache.put("a", 1, 1);
// peek does not change frequency
assert_eq!(cache.peek(&"a"), Some(&1));
assert_eq!(cache.peek(&"missing"), None);Source§impl<K: Hash + Eq, V> LfuCache<K, V>where
V: Clone,
impl<K: Hash + Eq, V> LfuCache<K, V>where
V: Clone,
Sourcepub fn init(
config: LfuCacheConfig,
hasher: Option<DefaultHashBuilder>,
) -> LfuCache<K, V, DefaultHashBuilder>
pub fn init( config: LfuCacheConfig, hasher: Option<DefaultHashBuilder>, ) -> LfuCache<K, V, DefaultHashBuilder>
Creates a new LFU cache from a configuration.
This is the recommended way to create an LFU cache. All configuration
is specified through the LfuCacheConfig struct.
§Arguments
config- Configuration specifying capacity and optional size limithasher- Optional custom hash builder (uses default ifNone)
§Example
use cache_rs::LfuCache;
use cache_rs::config::LfuCacheConfig;
use core::num::NonZeroUsize;
// Simple capacity-only cache
let config = LfuCacheConfig {
capacity: NonZeroUsize::new(100).unwrap(),
max_size: u64::MAX,
};
let mut cache: LfuCache<&str, i32> = LfuCache::init(config, None);
cache.put("key", 42, 1);
// Cache with size limit
let config = LfuCacheConfig {
capacity: NonZeroUsize::new(1000).unwrap(),
max_size: 10 * 1024 * 1024, // 10MB
};
let cache: LfuCache<String, Vec<u8>> = LfuCache::init(config, None);