Skip to main content

SecondChanceLru

Struct SecondChanceLru 

Source
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()); // evicted

Implementations§

Source§

impl<K: Hash + Eq + Clone, V> SecondChanceLru<K, V>

Source

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.

Source

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.

Source

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.

Source

pub fn contains_key(&self, key: &K) -> bool

Checks if the cache contains the given key.

This does NOT mark the entry as accessed.

Source

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.

Source

pub fn remove(&mut self, key: &K) -> Option<V>

Removes a specific key from the cache.

Returns the value if it was present.

Source

pub fn len(&self) -> usize

Returns the number of entries in the cache.

Source

pub fn is_empty(&self) -> bool

Returns true if the cache is empty.

Source

pub fn clear(&mut self)

Clears all entries from the cache.

Source

pub fn capacity(&self) -> usize

Returns the maximum capacity of the cache.

Source

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.

Source

pub fn values(&self) -> impl Iterator<Item = &V>

Returns an iterator over the values in the cache.

The order is unspecified. Values are not marked as accessed.

Source

pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>

Returns an iterator over key-value pairs in the cache.

The order is unspecified. Values are not marked as accessed.

Trait Implementations§

Source§

impl<K: Hash + Eq + Clone, V: Clone> Clone for SecondChanceLru<K, V>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Hash + Eq + Clone, V> Default for SecondChanceLru<K, V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<K, V> Freeze for SecondChanceLru<K, V>

§

impl<K, V> RefUnwindSafe for SecondChanceLru<K, V>

§

impl<K, V> Send for SecondChanceLru<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for SecondChanceLru<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for SecondChanceLru<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for SecondChanceLru<K, V>
where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.