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
impl ContentAwareCache
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Create a new ContentAwareCache with an entry-count capacity.
Sourcepub fn with_max_bytes(self, max_bytes: usize) -> Self
pub fn with_max_bytes(self, max_bytes: usize) -> Self
Set an optional byte-level capacity in addition to the entry count cap.
Sourcepub fn with_scoring_weights(self, weights: ScoringWeights) -> Self
pub fn with_scoring_weights(self, weights: ScoringWeights) -> Self
Set custom scoring weights used for eviction candidate selection.
Sourcepub fn set_scoring_weights(&mut self, weights: ScoringWeights)
pub fn set_scoring_weights(&mut self, weights: ScoringWeights)
Update scoring weights at runtime.
Sourcepub fn scoring_weights(&self) -> &ScoringWeights
pub fn scoring_weights(&self) -> &ScoringWeights
Return a reference to the current scoring weights.
Sourcepub fn insert_media(
&mut self,
key: String,
data: Vec<u8>,
content_type: MediaContentType,
)
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.
Sourcepub fn get(&mut self, key: &str) -> Option<&CacheEntry>
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).
Sourcepub fn peek(&self, key: &str) -> Option<&CacheEntry>
pub fn peek(&self, key: &str) -> Option<&CacheEntry>
Peek at key without updating access metadata or LRU order.
Sourcepub fn remove(&mut self, key: &str) -> bool
pub fn remove(&mut self, key: &str) -> bool
Explicitly remove an entry by key.
Returns true if the entry was present.
Sourcepub fn evict_expired(&mut self) -> usize
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.
Sourcepub fn total_bytes(&self) -> usize
pub fn total_bytes(&self) -> usize
Return the total number of bytes currently stored.