pub struct SlruCache<K, V, S = DefaultHashBuilder> { /* private fields */ }
Expand description
An implementation of a Segmented Least Recently Used (SLRU) cache.
The cache is divided into two segments:
- Probationary segment: Where new entries are initially placed
- Protected segment: Where frequently accessed entries are promoted to
When the cache reaches capacity, the least recently used entry from the probationary segment is evicted. If the probationary segment is empty, entries from the protected segment may be demoted back to probationary.
§Examples
use cache_rs::slru::SlruCache;
use core::num::NonZeroUsize;
// Create an SLRU cache with a total capacity of 4,
// with a protected capacity of 2 (half protected, half probationary)
let mut cache = SlruCache::new(
NonZeroUsize::new(4).unwrap(),
NonZeroUsize::new(2).unwrap()
);
// Add some items
cache.put("a", 1);
cache.put("b", 2);
cache.put("c", 3);
cache.put("d", 4);
// Access "a" to promote it to the protected segment
assert_eq!(cache.get(&"a"), Some(&1));
// Add a new item, which will evict the least recently used item
// from the probationary segment (likely "b")
cache.put("e", 5);
assert_eq!(cache.get(&"b"), None);
Implementations§
Source§impl<K: Hash + Eq, V: Clone, S: BuildHasher> SlruCache<K, V, S>
impl<K: Hash + Eq, V: Clone, S: BuildHasher> SlruCache<K, V, S>
Sourcepub fn with_hasher(
total_cap: NonZeroUsize,
protected_cap: NonZeroUsize,
hash_builder: S,
) -> Self
pub fn with_hasher( total_cap: NonZeroUsize, protected_cap: NonZeroUsize, hash_builder: S, ) -> Self
Creates a new SLRU cache with the specified capacity and hash builder.
§Parameters
total_cap
: The total capacity of the cache. Must be a non-zero value.protected_cap
: The maximum capacity of the protected segment. Must be a non-zero value and less than or equal tototal_cap
.hash_builder
: The hash builder to use for the underlying hash map.
§Panics
Panics if protected_cap
is greater than total_cap
.
Sourcepub fn cap(&self) -> NonZeroUsize
pub fn cap(&self) -> NonZeroUsize
Returns the maximum number of key-value pairs the cache can hold.
Sourcepub fn protected_max_size(&self) -> NonZeroUsize
pub fn protected_max_size(&self) -> NonZeroUsize
Returns the maximum size of the protected segment.
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 least recently used item from the probationary segment will be evicted. If the probationary segment is empty, the least recently used item from the protected segment will be demoted to the probationary segment.
The inserted key-value pair is always placed in the probationary segment.
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)
Source§impl<K: Hash + Eq, V> SlruCache<K, V>where
V: Clone,
impl<K: Hash + Eq, V> SlruCache<K, V>where
V: Clone,
Sourcepub fn new(
capacity: NonZeroUsize,
protected_capacity: NonZeroUsize,
) -> SlruCache<K, V, DefaultHashBuilder>
pub fn new( capacity: NonZeroUsize, protected_capacity: NonZeroUsize, ) -> SlruCache<K, V, DefaultHashBuilder>
Creates a new SLRU cache with the specified capacity and protected capacity.
The total capacity must be greater than the protected capacity.
§Panics
Panics if protected_capacity
is greater than capacity
.