Skip to main content

CachePartition

Struct CachePartition 

Source
pub struct CachePartition {
    pub name: String,
    pub max_bytes: usize,
    /* private fields */
}
Expand description

An isolated cache partition with its own byte-level capacity.

Internally uses an insertion-ordered key list (Vec<String>) for LRU tracking, and a HashMap for O(1) value access. Eviction walks from the tail (oldest) towards the head (newest), skipping high-priority items.

Fields§

§name: String

Human-readable name for this partition.

§max_bytes: usize

Maximum byte budget for this partition.

Implementations§

Source§

impl CachePartition

Source

pub fn new(name: impl Into<String>, max_bytes: usize) -> Self

Create a new partition with the given name and byte capacity.

Source

pub fn len(&self) -> usize

Return the number of entries in this partition.

Source

pub fn is_empty(&self) -> bool

Return true when the partition is empty.

Source

pub fn used_bytes(&self) -> usize

Return currently used bytes.

Source

pub fn stats(&self) -> PartitionStats

Return partition statistics.

Source

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

Retrieve the entry for key.

Returns None if the key is absent or its TTL has expired (in which case the entry is lazily removed).

Source

pub fn peek(&self, key: &str) -> Option<&CacheEntry>

Peek at an entry without updating LRU order or access statistics.

Source

pub fn put(&mut self, key: String, entry: CacheEntry)

Insert or update (key, entry) in this partition.

If the entry does not fit even after evicting all lower-priority entries the insert is silently dropped.

Source

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

Remove key from this partition. Returns true if it was present.

Source

pub fn evict_one_lru(&mut self) -> Option<String>

Evict the least-recently-used entry.

Returns the evicted key or None if the partition is empty.

Source

pub fn evict_bytes(&mut self, bytes_to_free: usize) -> usize

Evict entries from this partition until bytes_to_free bytes have been freed or the partition is empty.

Returns the number of bytes actually freed.

Source

pub fn purge_expired(&mut self) -> usize

Purge all expired entries. Returns the count of entries removed.

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.