Skip to main content

UnifiedCache

Struct UnifiedCache 

Source
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,

Source

pub fn new( max_size: usize, ttl: Duration, eviction_policy: EvictionPolicy, ) -> Self

Source

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.

Source

pub fn get_owned(&self, key: &K) -> Option<V>

Get owned value (explicitly clones when needed)

Source

pub fn insert(&self, key: K, value: V, size_bytes: u64)

Insert value into cache with automatic eviction

Source

pub fn insert_arc(&self, key: K, value: Arc<V>, size_bytes: u64)

Insert an already shared value without cloning its payload.

Source

pub fn stats(&self) -> CacheStats

Get cache statistics (returns owned clone)

Source

pub fn clear(&self)

Clear all entries

Source

pub fn remove(&self, key: &K)

Remove a single cache entry by exact key.

Source

pub fn len(&self) -> usize

Get current size

Source

pub fn is_empty(&self) -> bool

Source

pub fn invalidate_prefix(&self, prefix: &str)

Invalidate cache entries matching a key prefix (selective eviction) This replaces the old “clear entire cache” behavior with granular eviction

§Example
ⓘ
cache.invalidate_prefix("grep_file:/workspace/src/");
// Only removes entries for that specific file, not entire cache
Source

pub fn invalidate_path(&self, path: &str)

Invalidate entries for a specific target path (e.g., file path) This is a convenience wrapper for file-based invalidation

§Example
ⓘ
cache.invalidate_path("/workspace/src/main.rs");
// Removes all cache entries related to this file
Source

pub fn invalidate_suffix(&self, suffix: &str)

Invalidate cache entries matching a key suffix (selective eviction)

§Example
ⓘ
cache.invalidate_suffix(":/workspace/src/main.rs");
// Only removes entries for that specific file
Source

pub fn invalidate_containing(&self, substring: &str)

Invalidate cache entries containing a substring (selective eviction)

§Example
ⓘ
cache.invalidate_containing("/workspace/src/main.rs");
// Removes entries where the cache key contains this path
Source

pub fn remove_where<F>(&self, predicate: F) -> usize
where F: FnMut(&K) -> bool,

Remove all entries that satisfy a predicate.

Returns the number of removed entries without cloning keys or buffering matches.

Source

pub fn total_memory_bytes(&self) -> u64

Get total memory used by cache in bytes

Source

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

Source

pub fn reduce_ttl(&self, factor: f64) -> Duration

Set the effective TTL to factor times the configured TTL.

Idempotent by design: pressure is observed repeatedly (e.g. once per turn), so multiplying the current TTL on every call would shrink it geometrically toward zero and leave no way to recover when pressure subsides. A factor of 1.0 restores the original TTL. Factors are clamped to [0, 1]; a non-finite factor leaves the current TTL unchanged.

Source

pub fn base_ttl(&self) -> Duration

TTL this cache was configured with, before any pressure tuning.

Source

pub fn evict_under_pressure(&self, target_reduction_percent: u32) -> usize

Evict entries under memory pressure (aggressive cleanup)

When memory pressure increases:

  1. Remove all expired entries first
  2. 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.

Source

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.

Source

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>
where RwLock<UnifiedCacheInner<K, V>>: Send,

§

impl<K, V> Sync for UnifiedCache<K, V>
where RwLock<UnifiedCacheInner<K, V>>: Sync,

§

impl<K, V> Unpin for UnifiedCache<K, V>
where RwLock<UnifiedCacheInner<K, V>>: Unpin,

§

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> 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &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
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync> ⓘ

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> PtyHandle for T
where T: Send,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more