pub struct VersionedInferenceCache { /* private fields */ }Expand description
Concurrent, version-aware memoization cache for inference results.
Entries are keyed by (goal_hash, kb_id, kb_version). Calling
bump_kb_version atomically increments the version
for a given KB and removes every cached entry that was derived from that
KB, regardless of their recorded version.
§Concurrency
Read operations (get, current_kb_version, entry_count, stats) acquire
a read lock; write operations acquire an exclusive write lock.
Implementations§
Source§impl VersionedInferenceCache
impl VersionedInferenceCache
Sourcepub fn new(max_entries: usize) -> Self
pub fn new(max_entries: usize) -> Self
Create a new cache with the given capacity limit.
If max_entries is 0 the default (10_000) is used.
Sourcepub fn insert(&self, key: CacheKey, entry: CacheEntry) -> Result<(), CacheError>
pub fn insert(&self, key: CacheKey, entry: CacheEntry) -> Result<(), CacheError>
Insert a new entry.
Returns CacheError::CapacityExceeded when the cache is full.
Returns CacheError::StaleVersion when key.kb_version is lower
than the currently tracked version for key.kb_id.
If an entry with the same key already exists it is overwritten (the capacity check is skipped for replacements).
Sourcepub fn get(&self, key: &CacheKey) -> Option<CacheEntry>
pub fn get(&self, key: &CacheKey) -> Option<CacheEntry>
Look up a cached entry by key.
Returns None if the entry is not found or if it has expired.
On a hit the entry’s hit_count is incremented and a clone is returned.
Sourcepub fn current_kb_version(&self, kb_id: &str) -> u64
pub fn current_kb_version(&self, kb_id: &str) -> u64
Return the current version for the given KB, or 0 if unknown.
Sourcepub fn bump_kb_version(&self, kb_id: &str) -> u64
pub fn bump_kb_version(&self, kb_id: &str) -> u64
Increment the version counter for kb_id and atomically invalidate
all cached entries that were derived from that KB.
Returns the new version number.
Sourcepub fn invalidate_kb(&self, kb_id: &str)
pub fn invalidate_kb(&self, kb_id: &str)
Remove all cached entries whose kb_id matches, regardless of
their recorded version.
This is called internally by bump_kb_version
but can also be called directly (e.g., when a KB is deleted).
Sourcepub fn evict_expired(&self) -> usize
pub fn evict_expired(&self) -> usize
Remove all expired entries from the cache.
Returns the number of entries removed.
Sourcepub fn entry_count(&self) -> usize
pub fn entry_count(&self) -> usize
Current number of entries in the cache (including possibly-expired ones).
Sourcepub fn stats(&self) -> CacheStatsSnapshot
pub fn stats(&self) -> CacheStatsSnapshot
Snapshot of the current statistics counters.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for VersionedInferenceCache
impl RefUnwindSafe for VersionedInferenceCache
impl Send for VersionedInferenceCache
impl Sync for VersionedInferenceCache
impl Unpin for VersionedInferenceCache
impl UnsafeUnpin for VersionedInferenceCache
impl UnwindSafe for VersionedInferenceCache
Blanket Implementations§
impl<T> Allocation for T
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> 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