pub struct LfudaCache<K, V, S = DefaultHashBuilder> { /* private fields */ }Expand description
An implementation of a Least Frequently Used with Dynamic Aging (LFUDA) cache.
LFUDA improves upon LFU by adding an aging mechanism that prevents old frequently-used items from blocking new items indefinitely. Each item’s effective priority is calculated as (access_frequency + age_at_insertion), where age_at_insertion is the global age value when the item was first inserted.
When an item is evicted, the global age is set to the evicted item’s effective priority, ensuring that new items start with a competitive priority.
§Examples
use cache_rs::lfuda::LfudaCache;
use cache_rs::config::LfudaCacheConfig;
use core::num::NonZeroUsize;
// Create an LFUDA cache with capacity 3
let config = LfudaCacheConfig {
capacity: NonZeroUsize::new(3).unwrap(),
initial_age: 0,
max_size: u64::MAX,
};
let mut cache = LfudaCache::init(config, None);
// Add some items
cache.put("a", 1);
cache.put("b", 2);
cache.put("c", 3);
// Access "a" multiple times to increase its frequency
assert_eq!(cache.get(&"a"), Some(&1));
assert_eq!(cache.get(&"a"), Some(&1));
// Add more items to trigger aging
cache.put("d", 4); // This will evict an item and increase global age
cache.put("e", 5); // New items benefit from the increased ageImplementations§
Source§impl<K: Hash + Eq, V: Clone, S: BuildHasher> LfudaCache<K, V, S>
impl<K: Hash + Eq, V: Clone, S: BuildHasher> LfudaCache<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 global_age(&self) -> u64
pub fn global_age(&self) -> u64
Returns the current global age value.
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 put(&mut self, key: K, value: V) -> Option<(K, V)>where
K: Clone,
pub fn put(&mut self, key: K, value: V) -> Option<(K, V)>where
K: Clone,
Inserts a key-value pair into the cache.
If the cache already contained this key, the old value is replaced and returned. Otherwise, if the cache is at capacity, the item with the lowest effective priority is evicted. The global age is updated to the evicted item’s effective priority.
New items are inserted with a frequency of 1 and age_at_insertion set to the current global_age.
Sourcepub fn put_with_size(&mut self, key: K, value: V, size: u64) -> Option<(K, V)>where
K: Clone,
pub fn put_with_size(&mut self, key: K, value: V, size: u64) -> Option<(K, V)>where
K: Clone,
Insert a key-value pair with explicit size tracking.
The size parameter specifies how much of max_size this entry consumes.
Use size=1 for count-based caches.
Source§impl<K: Hash + Eq, V> LfudaCache<K, V>where
V: Clone,
impl<K: Hash + Eq, V> LfudaCache<K, V>where
V: Clone,
Sourcepub fn init(
config: LfudaCacheConfig,
hasher: Option<DefaultHashBuilder>,
) -> LfudaCache<K, V, DefaultHashBuilder>
pub fn init( config: LfudaCacheConfig, hasher: Option<DefaultHashBuilder>, ) -> LfudaCache<K, V, DefaultHashBuilder>
Creates a new LFUDA cache from a configuration.
This is the recommended way to create an LFUDA cache. All configuration
is specified through the LfudaCacheConfig struct, which uses a builder
pattern for optional parameters.
§Arguments
config- Configuration specifying capacity and optional size limit/initial age
§Example
use cache_rs::LfudaCache;
use cache_rs::config::LfudaCacheConfig;
use core::num::NonZeroUsize;
// Simple capacity-only cache
let config = LfudaCacheConfig {
capacity: NonZeroUsize::new(100).unwrap(),
initial_age: 0,
max_size: u64::MAX,
};
let mut cache: LfudaCache<&str, i32> = LfudaCache::init(config, None);
cache.put("key", 42);
// Cache with size limit
let config = LfudaCacheConfig {
capacity: NonZeroUsize::new(1000).unwrap(),
initial_age: 100,
max_size: 10 * 1024 * 1024, // 10MB
};
let cache: LfudaCache<String, Vec<u8>> = LfudaCache::init(config, None);