Skip to main content

WeightedCache

Struct WeightedCache 

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

Capacity-bounded cache that uses per-media-type weight factors to choose eviction candidates.

Entries are scored on each eviction pass; the entry with the lowest score (least worth keeping) is removed first.

Implementations§

Source§

impl WeightedCache

Source

pub fn new(capacity: usize, weights: WeightConfig) -> Self

Create a new WeightedCache with the given capacity and weight config.

§Panics

Panics if capacity is 0.

Source

pub fn insert( &mut self, key: impl Into<String>, value: Vec<u8>, media_type: CacheMediaType, priority: u8, )

Insert (key, value) into the cache with the given media_type and priority label (0–255, higher = more important).

If the key already exists it is overwritten. If the cache is at capacity, the lowest-scored entry is evicted first.

Source

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

Look up key and return a reference to its value.

Records a hit or miss and updates last_accessed on hit.

Source

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

Remove the entry for key and return it (if present).

Source

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

Return true when key is present.

Source

pub fn len(&self) -> usize

Number of entries currently stored.

Source

pub fn is_empty(&self) -> bool

Return true when no entries are stored.

Source

pub fn capacity(&self) -> usize

Maximum number of entries the cache will hold.

Source

pub fn hits(&self) -> u64

Total cache hits recorded.

Source

pub fn misses(&self) -> u64

Total cache misses recorded.

Source

pub fn evictions(&self) -> u64

Total evictions performed.

Source

pub fn hit_rate(&self) -> f64

Hit rate as a fraction [0.0, 1.0].

Source

pub fn resize(&mut self, new_capacity: usize)

Resize the cache. If new_capacity is smaller than the current entry count, entries are evicted until the count fits.

Source

pub fn clear(&mut self)

Clear all entries and reset statistics.

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.