pub struct SemanticCacheLayer {
pub config: CacheConfig,
pub entries: Vec<CacheEntry>,
pub next_id: usize,
pub total_hits: u64,
pub total_misses: u64,
pub total_insertions: u64,
}Expand description
A vector-similarity-based cache.
See the module-level documentation for a usage example.
Fields§
§config: CacheConfigRuntime configuration.
entries: Vec<CacheEntry>The stored entries.
next_id: usizeMonotonically increasing counter used to assign stable entry IDs.
total_hits: u64Total cache hits since creation.
total_misses: u64Total cache misses since creation.
total_insertions: u64Total entries ever inserted.
Implementations§
Source§impl SemanticCacheLayer
impl SemanticCacheLayer
Sourcepub fn new(config: CacheConfig) -> Self
pub fn new(config: CacheConfig) -> Self
Create a new cache with the given configuration.
Sourcepub fn cosine_similarity(a: &[f64], b: &[f64]) -> f64
pub fn cosine_similarity(a: &[f64], b: &[f64]) -> f64
Compute the cosine similarity between two f64 slices.
Returns 0.0 if the vectors have different lengths, if either vector is
all-zeros, or if the denominator underflows to zero.
Sourcepub fn lookup(&mut self, query_embedding: &[f64], now: u64) -> CacheLookupResult
pub fn lookup(&mut self, query_embedding: &[f64], now: u64) -> CacheLookupResult
Look up the cache for a semantically similar previous query.
Scans all non-expired entries and finds the one with the highest cosine
similarity to query_embedding. If that similarity is ≥
config.similarity_threshold the entry’s hit_count and last_accessed
fields are updated and a CacheLookupResult::Hit is returned; otherwise
CacheLookupResult::Miss is returned.
The global total_hits / total_misses counters are always updated.
§Arguments
query_embedding— embedding vector for the incoming query.now— current timestamp in milliseconds (caller-supplied for testability).
Sourcepub fn insert(&mut self, key: CacheKey, result: String, now: u64)
pub fn insert(&mut self, key: CacheKey, result: String, now: u64)
Insert a new entry into the cache.
If the cache has reached config.max_entries, one entry is evicted first
according to config.eviction_policy. The config.ttl_ms value is
applied to the new entry’s ttl_ms field.
§Arguments
key— the query key (text + embedding).result— the result to cache.now— current timestamp in milliseconds.
Sourcepub fn evict_one(&mut self, now: u64)
pub fn evict_one(&mut self, now: u64)
Evict a single entry according to the configured CacheEvictionPolicy.
- LRU — remove the entry with the smallest
last_accessedtimestamp. - LFU — remove the entry with the smallest
hit_count. - TTLFirst — among entries that have a
ttl_ms, remove the one that will expire soonest (inserted_at + ttl_msis smallest); if no entry has a TTL, fall back to LRU.
Does nothing if the cache is empty.
Sourcepub fn evict_expired(&mut self, now: u64) -> usize
pub fn evict_expired(&mut self, now: u64) -> usize
Remove all expired entries from the cache.
Returns the number of entries that were removed.
Sourcepub fn invalidate_by_text(&mut self, text: &str) -> usize
pub fn invalidate_by_text(&mut self, text: &str) -> usize
Remove all entries whose key.query_text exactly matches text.
Returns the number of entries removed.
Sourcepub fn entry_count(&self) -> usize
pub fn entry_count(&self) -> usize
Return the current number of entries in the cache.
Sourcepub fn stats(&self) -> ScCacheStats
pub fn stats(&self) -> ScCacheStats
Return aggregate statistics for this cache instance.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SemanticCacheLayer
impl RefUnwindSafe for SemanticCacheLayer
impl Send for SemanticCacheLayer
impl Sync for SemanticCacheLayer
impl Unpin for SemanticCacheLayer
impl UnsafeUnpin for SemanticCacheLayer
impl UnwindSafe for SemanticCacheLayer
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.