pub struct MemoryBudgetCache { /* private fields */ }Expand description
Memory budget cache with LRU eviction
Tracks loaded partitions and their memory footprint, evicting least-recently-used partitions when the memory budget is exceeded.
Thread-safe: All methods take &self and use interior mutability
via parking_lot::Mutex for concurrent access.
Implementations§
Source§impl MemoryBudgetCache
impl MemoryBudgetCache
Sourcepub fn with_default_budget() -> Self
pub fn with_default_budget() -> Self
Create a cache with default memory budget (512 MB)
Sourcepub fn with_min_partitions(self, min: usize) -> Self
pub fn with_min_partitions(self, min: usize) -> Self
Set the minimum number of partitions to keep
Sourcepub fn max_memory_bytes(&self) -> usize
pub fn max_memory_bytes(&self) -> usize
Get the memory budget in bytes
Sourcepub fn current_memory_bytes(&self) -> usize
pub fn current_memory_bytes(&self) -> usize
Get current memory usage in bytes
Sourcepub fn memory_usage_ratio(&self) -> f64
pub fn memory_usage_ratio(&self) -> f64
Get memory usage as a percentage (0.0 - 1.0)
Sourcepub fn partition_count(&self) -> usize
pub fn partition_count(&self) -> usize
Get the number of partitions currently tracked
Sourcepub fn metrics(&self) -> CacheMetrics
pub fn metrics(&self) -> CacheMetrics
Get a snapshot of cache metrics
Sourcepub fn reset_metrics(&self)
pub fn reset_metrics(&self)
Reset cache metrics
Sourcepub fn touch(&self, partition_id: &str) -> bool
pub fn touch(&self, partition_id: &str) -> bool
Mark a partition as accessed (updates LRU order)
Returns true if the partition was found, false otherwise.
Sourcepub fn record_loaded(&self, partition_id: String, stats: PartitionStats)
pub fn record_loaded(&self, partition_id: String, stats: PartitionStats)
Record that a partition has been loaded
Adds the partition to the cache and updates memory tracking.
Sourcepub fn get_stats(&self, partition_id: &str) -> Option<PartitionStats>
pub fn get_stats(&self, partition_id: &str) -> Option<PartitionStats>
Get stats for a partition (if tracked)
Returns a clone of the stats to avoid holding the lock.
Sourcepub fn remove(&self, partition_id: &str) -> Option<PartitionStats>
pub fn remove(&self, partition_id: &str) -> Option<PartitionStats>
Remove a partition from the cache
Returns the stats if the partition was tracked, None otherwise.
Sourcepub fn is_over_budget(&self) -> bool
pub fn is_over_budget(&self) -> bool
Check if memory budget is exceeded
Sourcepub fn get_eviction_candidates(&self) -> Vec<String>
pub fn get_eviction_candidates(&self) -> Vec<String>
Get partition IDs that should be evicted to make room
Returns partition IDs in LRU order (least recently used first).
Respects min_partitions to avoid thrashing.
Sourcepub fn memory_needed_for(&self, additional_bytes: usize) -> usize
pub fn memory_needed_for(&self, additional_bytes: usize) -> usize
Get the amount of memory needed to be freed to accommodate a new load
Sourcepub fn get_eviction_candidates_for(&self, needed_bytes: usize) -> Vec<String>
pub fn get_eviction_candidates_for(&self, needed_bytes: usize) -> Vec<String>
Get partitions to evict to make room for a specific amount of memory
Returns partition IDs in LRU order.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for MemoryBudgetCache
impl !RefUnwindSafe for MemoryBudgetCache
impl Send for MemoryBudgetCache
impl Sync for MemoryBudgetCache
impl Unpin for MemoryBudgetCache
impl UnwindSafe for MemoryBudgetCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more