Skip to main content

TieredCache

Struct TieredCache 

Source
pub struct TieredCache { /* private fields */ }
Expand description

A multi-tier cache where each tier has its own TierConfig.

Reads check tiers in order (L1 first); a hit in tier i > 0 promotes the entry to tier i-1 when the key’s access frequency in that tier meets or exceeds the tier’s TierConfig::promotion_threshold. Writes always go to L1 only.

Implementations§

Source§

impl TieredCache

Source

pub fn new(tiers: Vec<TierConfig>) -> Self

Construct a TieredCache from a list of tier configurations. The first element is L1 (fastest / smallest), last is the slowest.

Source

pub fn get(&mut self, key: &str) -> Option<Vec<u8>>

Look up key across all tiers in order.

On a hit in tier i > 0, the entry is promoted to tier i-1 if the key’s frequency in that tier meets or exceeds the effective promotion threshold (static or P²-adaptive depending on configuration).

Source

pub fn put(&mut self, key: &str, data: Vec<u8>)

Insert (key, data) into the L1 tier.

Source

pub fn put_at_tier(&mut self, tier_idx: usize, key: &str, data: Vec<u8>)

Insert (key, data) directly into tier tier_idx.

Useful for pre-populating lower tiers (e.g. from a warm-up snapshot).

Source

pub fn evict_tier(&mut self, tier_idx: usize) -> Option<(String, Vec<u8>)>

Evict one entry from tier tier_idx according to that tier’s policy. Returns the evicted (key, data) or None if the tier is empty.

Source

pub fn stats(&self) -> TieredCacheStats

Return an aggregate statistics snapshot.

Source

pub fn warmup(&mut self, entries: &[(String, Vec<u8>)])

Bulk-insert entries into L1 without triggering eviction.

Source

pub fn invalidate(&mut self, key: &str) -> bool

Remove key from every tier. Returns true if it was found in at least one tier.

Source

pub fn tier_count(&self) -> usize

Return the number of tiers.

Source

pub fn tier_promotions(&self, tier_idx: usize) -> u64

Return the number of promotions from tier tier_idx to the tier above.

Source

pub fn tier_hit_count(&self, tier_idx: usize) -> u64

Return the number of hits recorded for tier tier_idx.

Source

pub fn reset_tier_arena(&mut self, tier_idx: usize)

Reset the bump arena of tier tier_idx (reclaims all arena memory).

After a reset, all previously stored arena handles for that tier are invalid; this is intended for use after a bulk eviction sweep.

Auto Trait Implementations§

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.