pub struct SemanticCacheManager { /* private fields */ }Expand description
A semantic-aware cache backed by cosine similarity search.
Entries are stored in a Vec<Slot> for simplicity and full linear-scan
semantic search. For workloads requiring sub-linear semantic lookup, wrap
this with an HNSW layer on top.
Implementations§
Source§impl SemanticCacheManager
impl SemanticCacheManager
Sourcepub fn new(config: ScmCacheConfig) -> Self
pub fn new(config: ScmCacheConfig) -> Self
Create a new SemanticCacheManager with the given configuration.
Sourcepub fn insert(
&mut self,
key: ScmCacheKey,
result: Vec<u8>,
ttl_us: Option<u64>,
) -> Result<u64, ScmCacheError>
pub fn insert( &mut self, key: ScmCacheKey, result: Vec<u8>, ttl_us: Option<u64>, ) -> Result<u64, ScmCacheError>
Insert an entry into the cache.
Assigns a monotonically increasing entry_id and stores the entry.
If the cache is over capacity after insertion, entries are evicted
according to the configured ScmEvictionStrategy.
§Errors
ScmCacheError::EntryTooLargeif the entry alone exceedsmax_bytes.ScmCacheError::DimensionMismatchif the embedding dimension is wrong.ScmCacheError::CacheAtCapacityif eviction fails to free space.
Sourcepub fn insert_at(
&mut self,
key: ScmCacheKey,
result: Vec<u8>,
ttl_us: Option<u64>,
inserted_at: u64,
) -> Result<u64, ScmCacheError>
pub fn insert_at( &mut self, key: ScmCacheKey, result: Vec<u8>, ttl_us: Option<u64>, inserted_at: u64, ) -> Result<u64, ScmCacheError>
Insert with an explicit insertion timestamp (used in tests and production where you track time yourself).
Sourcepub fn lookup(
&mut self,
query_text: &str,
embedding: &[f64],
current_ts: u64,
) -> Result<ScmCacheHit, ScmCacheError>
pub fn lookup( &mut self, query_text: &str, embedding: &[f64], current_ts: u64, ) -> Result<ScmCacheHit, ScmCacheError>
Look up a query in the cache.
- Scan all live entries; lazily expire any TTL-expired entries found.
- Check for an exact-text match (same FNV-1a hash and identical text).
- If no exact match, find the highest-similarity entry above the threshold.
Updates access_count and last_accessed on the winning entry.
§Errors
Returns ScmCacheError::DimensionMismatch when the provided embedding
dimension differs from the inferred dimension.
Sourcepub fn get_entry(&self, entry_id: u64) -> Result<&ScmCacheEntry, ScmCacheError>
pub fn get_entry(&self, entry_id: u64) -> Result<&ScmCacheEntry, ScmCacheError>
Retrieve a reference to an entry by its ID.
§Errors
Returns ScmCacheError::EntryNotFound if no such ID exists.
Sourcepub fn invalidate(&mut self, entry_id: u64) -> Result<(), ScmCacheError>
pub fn invalidate(&mut self, entry_id: u64) -> Result<(), ScmCacheError>
Remove the entry with the given ID.
§Errors
Returns ScmCacheError::EntryNotFound if no such ID exists.
Sourcepub fn invalidate_similar(
&mut self,
embedding: &[f64],
threshold: f64,
) -> Vec<u64>
pub fn invalidate_similar( &mut self, embedding: &[f64], threshold: f64, ) -> Vec<u64>
Remove all entries whose embedding has cosine similarity > threshold
with the given embedding. Returns the IDs of removed entries.
Sourcepub fn expire_ttl(&mut self, current_ts: u64) -> Vec<u64>
pub fn expire_ttl(&mut self, current_ts: u64) -> Vec<u64>
Remove all TTL-expired entries as of current_ts. Returns the IDs removed.
Sourcepub fn evict_to_fit(&mut self, needed_bytes: usize) -> Vec<u64>
pub fn evict_to_fit(&mut self, needed_bytes: usize) -> Vec<u64>
Evict entries (per the configured strategy) until at least needed_bytes
of space is available. Returns the IDs of evicted entries.
§Errors
Returns ScmCacheError::CacheAtCapacity if it is impossible to free
enough space (e.g., all entries are locked or the budget is zero).
Sourcepub fn semantic_neighbors(
&self,
embedding: &[f64],
top_k: usize,
) -> Vec<(u64, f64)>
pub fn semantic_neighbors( &self, embedding: &[f64], top_k: usize, ) -> Vec<(u64, f64)>
Return the top-k entries ranked by cosine similarity to embedding,
without any threshold filtering.
Sourcepub fn cluster_stats(&self) -> Vec<(f64, usize)>
pub fn cluster_stats(&self) -> Vec<(f64, usize)>
Compute greedy cluster statistics.
Groups embeddings into clusters where every member is within
config.cluster_radius (in cosine distance = 1 − similarity) of the
cluster centroid. Returns a Vec of (centroid_similarity, cluster_size).
The “centroid” here is represented as the embedding of the first entry assigned to each cluster (greedy seeding). The returned f64 is the average pairwise cosine similarity within the cluster.
Sourcepub fn stats(&self) -> ScmCacheStats
pub fn stats(&self) -> ScmCacheStats
Return a snapshot of current cache statistics.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SemanticCacheManager
impl RefUnwindSafe for SemanticCacheManager
impl Send for SemanticCacheManager
impl Sync for SemanticCacheManager
impl Unpin for SemanticCacheManager
impl UnsafeUnpin for SemanticCacheManager
impl UnwindSafe for SemanticCacheManager
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.