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: StringHuman-readable name for this partition.
max_bytes: usizeMaximum byte budget for this partition.
Implementations§
Source§impl CachePartition
impl CachePartition
Sourcepub fn new(name: impl Into<String>, max_bytes: usize) -> Self
pub fn new(name: impl Into<String>, max_bytes: usize) -> Self
Create a new partition with the given name and byte capacity.
Sourcepub fn used_bytes(&self) -> usize
pub fn used_bytes(&self) -> usize
Return currently used bytes.
Sourcepub fn stats(&self) -> PartitionStats
pub fn stats(&self) -> PartitionStats
Return partition statistics.
Sourcepub fn get(&mut self, key: &str) -> Option<&CacheEntry>
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).
Sourcepub fn peek(&self, key: &str) -> Option<&CacheEntry>
pub fn peek(&self, key: &str) -> Option<&CacheEntry>
Peek at an entry without updating LRU order or access statistics.
Sourcepub fn put(&mut self, key: String, entry: CacheEntry)
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.
Sourcepub fn remove(&mut self, key: &str) -> bool
pub fn remove(&mut self, key: &str) -> bool
Remove key from this partition. Returns true if it was present.
Sourcepub fn evict_one_lru(&mut self) -> Option<String>
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.
Sourcepub fn evict_bytes(&mut self, bytes_to_free: usize) -> usize
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.
Sourcepub fn purge_expired(&mut self) -> usize
pub fn purge_expired(&mut self) -> usize
Purge all expired entries. Returns the count of entries removed.