pub struct FifoCacheMap<K, V, const N: usize>{ /* private fields */ }Expand description
A bounded cache that maintains key-value pairs with O(1) lookups.
Uses a VecDeque for FIFO ordering and an AHashMap for fast key-value access.
When capacity is exceeded, the oldest entry is automatically evicted.
§Examples
use nautilus_common::cache::fifo::FifoCacheMap;
let mut cache: FifoCacheMap<u32, String, 3> = FifoCacheMap::new();
cache.insert(1, "one".to_string());
cache.insert(2, "two".to_string());
cache.insert(3, "three".to_string());
assert_eq!(cache.get(&1), Some(&"one".to_string()));
// Adding beyond capacity evicts the oldest
cache.insert(4, "four".to_string());
assert_eq!(cache.get(&1), None);
assert_eq!(cache.get(&4), Some(&"four".to_string()));Zero capacity is a compile-time error:
use nautilus_common::cache::fifo::FifoCacheMap;
// This fails to compile: capacity must be > 0
let cache: FifoCacheMap<u32, String, 0> = FifoCacheMap::new();Implementations§
Source§impl<K, V, const N: usize> FifoCacheMap<K, V, N>
impl<K, V, const N: usize> FifoCacheMap<K, V, N>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty FifoCacheMap with capacity N.
§Panics
Panics if N == 0. The panic occurs at compile time.
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Returns whether the cache contains the given key (O(1) lookup).
Sourcepub fn get(&self, key: &K) -> Option<&V>
pub fn get(&self, key: &K) -> Option<&V>
Returns a reference to the value for the given key (O(1) lookup).
Sourcepub fn get_mut(&mut self, key: &K) -> Option<&mut V>
pub fn get_mut(&mut self, key: &K) -> Option<&mut V>
Returns a mutable reference to the value for the given key (O(1) lookup).
Sourcepub fn iter(&self) -> impl Iterator<Item = (&K, &V)>
pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>
Returns an iterator over the key-value pairs in arbitrary order.
Sourcepub fn insert(&mut self, key: K, value: V)
pub fn insert(&mut self, key: K, value: V)
Inserts a key-value pair into the cache.
If the key already exists, the value is updated (no eviction occurs). If the cache is at capacity and the key is new, the oldest entry is evicted.