Skip to main content

ContentAwareCache

Struct ContentAwareCache 

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

A media-content-aware cache that scores eviction candidates by a recency × priority × size metric rather than pure LRU order.

Internally, it delegates storage to an LruCache<String, CacheEntry> for O(1) operations and maintains a live count of bytes.

Implementations§

Source§

impl ContentAwareCache

Source

pub fn new(capacity: usize) -> Self

Create a new ContentAwareCache with an entry-count capacity.

Source

pub fn with_max_bytes(self, max_bytes: usize) -> Self

Set an optional byte-level capacity in addition to the entry count cap.

Source

pub fn insert_media( &mut self, key: String, data: Vec<u8>, content_type: MediaContentType, )

Insert a media entry.

If the cache is at capacity, the entry with the highest eviction score is removed first. If the new entry has a higher priority than the current worst entry, it displaces it; otherwise the LRU entry is used as the fallback.

Source

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

Look up key and return an immutable reference to its CacheEntry if present (updating last-accessed time).

Source

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

Peek at key without updating access metadata or LRU order.

Source

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

Explicitly remove an entry by key.

Returns true if the entry was present.

Source

pub fn evict_expired(&mut self) -> usize

Scan all entries and remove those whose TTL has elapsed.

Returns the number of entries evicted.

This is an O(n) operation; callers should invoke it periodically rather than on every access.

Source

pub fn len(&self) -> usize

Return the number of entries currently in the cache.

Source

pub fn is_empty(&self) -> bool

Return true when the cache has no entries.

Source

pub fn total_bytes(&self) -> usize

Return the total number of bytes currently stored.

Source

pub fn capacity(&self) -> usize

Return the entry-count capacity.

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.