pub struct SecondChanceLru<K, V> { /* private fields */ }Expand description
LRU cache with second-chance eviction algorithm.
Access marking is lock-free (atomic bool), reducing contention on the hot read path. Only eviction requires exclusive access.
§Example
use grafeo_core::cache::SecondChanceLru;
let mut cache = SecondChanceLru::new(3);
cache.insert("a", 1);
cache.insert("b", 2);
cache.insert("c", 3);
// Access "a" to give it a second chance
let _ = cache.get(&"a");
// Insert "d" - should evict "b" (not accessed), not "a"
cache.insert("d", 4);
assert!(cache.get(&"a").is_some());
assert!(cache.get(&"b").is_none()); // evictedImplementations§
Source§impl<K: Hash + Eq + Clone, V> SecondChanceLru<K, V>
impl<K: Hash + Eq + Clone, V> SecondChanceLru<K, V>
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Creates a new cache with the given capacity.
The cache will hold at most capacity entries. When full, the
second-chance algorithm determines which entry to evict.
Sourcepub fn get(&self, key: &K) -> Option<&V>
pub fn get(&self, key: &K) -> Option<&V>
Gets a value, marking it as accessed (lock-free flag set).
Returns None if the key is not in the cache.
Sourcepub fn get_mut(&mut self, key: &K) -> Option<&mut V>
pub fn get_mut(&mut self, key: &K) -> Option<&mut V>
Gets a mutable reference to a value, marking it as accessed.
Returns None if the key is not in the cache.
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Checks if the cache contains the given key.
This does NOT mark the entry as accessed.
Sourcepub fn insert(&mut self, key: K, value: V)
pub fn insert(&mut self, key: K, value: V)
Inserts a value, evicting if at capacity.
If the key already exists, the value is updated and marked as accessed. If the cache is at capacity and this is a new key, one entry is evicted using the second-chance algorithm.
Sourcepub fn remove(&mut self, key: &K) -> Option<V>
pub fn remove(&mut self, key: &K) -> Option<V>
Removes a specific key from the cache.
Returns the value if it was present.
Sourcepub fn keys(&self) -> impl Iterator<Item = &K>
pub fn keys(&self) -> impl Iterator<Item = &K>
Returns an iterator over the keys in the cache.
The order is unspecified and does not reflect access patterns.
Trait Implementations§
Auto Trait Implementations§
impl<K, V> Freeze for SecondChanceLru<K, V>
impl<K, V> RefUnwindSafe for SecondChanceLru<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for SecondChanceLru<K, V>
impl<K, V> Sync for SecondChanceLru<K, V>
impl<K, V> Unpin for SecondChanceLru<K, V>
impl<K, V> UnwindSafe for SecondChanceLru<K, V>where
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more