Skip to main content

TwoQueueCache

Struct TwoQueueCache 

Source
pub struct TwoQueueCache<K: Eq + Hash + Clone, V> { /* private fields */ }
Expand description

A 2Q cache with configurable total capacity and A1in/A1out sizing.

§Type parameters

  • K — key type (must be Eq + Hash + Clone).
  • V — value type.

Implementations§

Source§

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

Source

pub fn new(capacity: usize) -> Self

Create a new 2Q cache with the given capacity.

A1in is sized to ~25% of capacity and A1out to ~50%.

Source

pub fn with_queue_sizes( capacity: usize, a1in_capacity: usize, a1out_capacity: usize, ) -> Self

Create a 2Q cache with explicit A1in and A1out sizing.

Source

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

Look up key. On a hit in Am, promote to MRU. On a hit in A1in, the entry stays in place (FIFO semantics).

Source

pub fn insert(&mut self, key: K, value: V)

Insert (key, value) into the cache.

  • If key is already in Am or A1in, its value is updated.
  • If key is in A1out (ghost hit), it is admitted to Am.
  • Otherwise it enters A1in.
Source

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

Remove key from the cache entirely (including ghost list).

Source

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

Returns true if the cache contains key (not counting ghosts).

Source

pub fn len(&self) -> usize

Total number of live entries.

Source

pub fn is_empty(&self) -> bool

Returns true when the cache has no live entries.

Source

pub fn stats(&self) -> TwoQueueStats

Return a statistics snapshot.

Source

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

Peek at key without updating access metadata or LRU order.

Source

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

Return a mutable reference to the value for key without changing queue positions.

Source

pub fn clear(&mut self)

Remove all entries, ghost list included.

Source

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

Return true if key is in the ghost list (A1out).

Source

pub fn a1in_len(&self) -> usize

Return the number of entries in A1in.

Source

pub fn am_len(&self) -> usize

Return the number of entries in Am.

Source

pub fn a1out_len(&self) -> usize

Return the number of ghost entries in A1out.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<K, V> UnsafeUnpin for TwoQueueCache<K, V>

§

impl<K, V> UnwindSafe for TwoQueueCache<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> 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, 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.