pub struct UnifiedCache<K, V> { /* private fields */ }Expand description
Unified cache backend with configurable eviction policies
Uses interior mutability via RwLock to allow &self methods,
enabling easier use in concurrent contexts without borrow checker conflicts.
Implementations§
Source§impl<K, V> UnifiedCache<K, V>where
K: CacheKey,
V: CacheValue,
impl<K, V> UnifiedCache<K, V>where
K: CacheKey,
V: CacheValue,
pub fn new( max_size: usize, ttl: Duration, eviction_policy: EvictionPolicy, ) -> Self
Sourcepub fn get(&self, key: &K) -> Option<Arc<V>>
pub fn get(&self, key: &K) -> Option<Arc<V>>
Get value from cache with zero-copy access by default.
Uses a read-first fast path and only attempts a non-blocking write lock for best-effort metadata/stat updates.
Sourcepub fn insert(&self, key: K, value: V, size_bytes: u64)
pub fn insert(&self, key: K, value: V, size_bytes: u64)
Insert value into cache with automatic eviction
Sourcepub fn insert_arc(&self, key: K, value: Arc<V>, size_bytes: u64)
pub fn insert_arc(&self, key: K, value: Arc<V>, size_bytes: u64)
Insert an already shared value without cloning its payload.
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Get cache statistics (returns owned clone)
pub fn is_empty(&self) -> bool
Sourcepub fn invalidate_prefix(&self, prefix: &str)
pub fn invalidate_prefix(&self, prefix: &str)
Sourcepub fn invalidate_path(&self, path: &str)
pub fn invalidate_path(&self, path: &str)
Sourcepub fn invalidate_suffix(&self, suffix: &str)
pub fn invalidate_suffix(&self, suffix: &str)
Sourcepub fn invalidate_containing(&self, substring: &str)
pub fn invalidate_containing(&self, substring: &str)
Sourcepub fn remove_where<F>(&self, predicate: F) -> usize
pub fn remove_where<F>(&self, predicate: F) -> usize
Remove all entries that satisfy a predicate.
Returns the number of removed entries without cloning keys or buffering matches.
Sourcepub fn total_memory_bytes(&self) -> u64
pub fn total_memory_bytes(&self) -> u64
Get total memory used by cache in bytes
Sourcepub fn estimate_entry_cost(entry: &CacheEntry<V>) -> u64
pub fn estimate_entry_cost(entry: &CacheEntry<V>) -> u64
Estimate entry cost in bytes (for memory-aware decisions) This is a heuristic based on entry metadata
Sourcepub fn reduce_ttl(&self, factor: f64) -> Duration
pub fn reduce_ttl(&self, factor: f64) -> Duration
Reduce TTL for all entries in cache (for pressure-based tuning) Returns the new TTL that was set
Sourcepub fn evict_under_pressure(&self, target_reduction_percent: u32) -> usize
pub fn evict_under_pressure(&self, target_reduction_percent: u32) -> usize
Evict entries under memory pressure (aggressive cleanup)
When memory pressure increases:
- Remove all expired entries first
- Evict according to the configured policy until the live entry count
reaches
floor(live_count * (100 - percent) / 100).
Percentages are clamped to 100. The return count includes expired entries; expiration does not increment the eviction statistic.
Sourcepub fn clear_least_used(&self, percent_to_clear: u32) -> usize
pub fn clear_least_used(&self, percent_to_clear: u32) -> usize
Evict floor(entry_count * percent / 100) entries using the configured policy.
Percentages are clamped to 100. Returns the number of entries removed.
Sourcepub fn entries_by_usefulness(&self) -> Vec<(K, CacheEntry<V>)>
pub fn entries_by_usefulness(&self) -> Vec<(K, CacheEntry<V>)>
Get entries sorted by “usefulness” (access count and recency) Higher score = more useful (keep these)
Auto Trait Implementations§
impl<K, V> !Freeze for UnifiedCache<K, V>
impl<K, V> Forget for UnifiedCache<K, V>
impl<K, V> Move for UnifiedCache<K, V>
impl<K, V> RefUnwindSafe for UnifiedCache<K, V>where
RwLock<UnifiedCacheInner<K, V>>: RefUnwindSafe,
impl<K, V> Send for UnifiedCache<K, V>
impl<K, V> Sync for UnifiedCache<K, V>
impl<K, V> Unpin for UnifiedCache<K, V>
impl<K, V> UnsafeUnpin for UnifiedCache<K, V>where
RwLock<UnifiedCacheInner<K, V>>: UnsafeUnpin,
impl<K, V> UnwindSafe for UnifiedCache<K, V>where
RwLock<UnifiedCacheInner<K, V>>: UnwindSafe,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for 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> 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