pub struct SemanticEmbeddingCache { /* private fields */ }Expand description
A tick-based embedding cache with LRU batch eviction and TTL expiry.
Implementations§
Source§impl SemanticEmbeddingCache
impl SemanticEmbeddingCache
Sourcepub fn new(config: EmbeddingCacheConfig) -> Self
pub fn new(config: EmbeddingCacheConfig) -> Self
Create a new cache with the given configuration.
Sourcepub fn put(&mut self, key: &str, embedding: Vec<f64>, ttl: Option<u64>)
pub fn put(&mut self, key: &str, embedding: Vec<f64>, ttl: Option<u64>)
Insert an embedding into the cache.
If ttl is None the default TTL from the config is used.
When the cache exceeds max_entries after insertion the least-recently
used entries (by access_count, then oldest computed_tick) are evicted
in a batch.
Sourcepub fn get(&mut self, key: &str) -> Option<&[f64]>
pub fn get(&mut self, key: &str) -> Option<&[f64]>
Retrieve the embedding for key if it exists and has not expired.
Increments the entry’s access_count and records a hit. Returns None
(and records a miss) when the key is absent or the entry has expired.
Sourcepub fn contains(&self, key: &str) -> bool
pub fn contains(&self, key: &str) -> bool
Check whether key is present and not expired without updating
the entry’s access_count.
Sourcepub fn invalidate(&mut self, key: &str) -> bool
pub fn invalidate(&mut self, key: &str) -> bool
Remove a specific entry. Returns true if the entry existed.
Sourcepub fn invalidate_prefix(&mut self, prefix: &str) -> usize
pub fn invalidate_prefix(&mut self, prefix: &str) -> usize
Remove all entries whose key starts with prefix. Returns the number
of entries removed.
Sourcepub fn tick_cleanup(&mut self)
pub fn tick_cleanup(&mut self)
Advance the internal tick counter by one and remove all expired entries.
Sourcepub fn entry_count(&self) -> usize
pub fn entry_count(&self) -> usize
Return the current number of entries in the cache.
Sourcepub fn hit_rate(&self) -> f64
pub fn hit_rate(&self) -> f64
Return the hit rate as a value in [0.0, 1.0].
Returns 0.0 when no lookups have been performed.
Sourcepub fn memory_estimate(&self) -> usize
pub fn memory_estimate(&self) -> usize
Estimate the memory footprint of all cached entries in bytes.
Each entry contributes key.len() + embedding.len() * 8 bytes (the
heap portion of the key string plus the heap portion of the Vec<f64>).
Sourcepub fn stats(&self) -> EmbeddingCacheStats
pub fn stats(&self) -> EmbeddingCacheStats
Return a snapshot of the cache statistics.
Auto Trait Implementations§
impl Freeze for SemanticEmbeddingCache
impl RefUnwindSafe for SemanticEmbeddingCache
impl Send for SemanticEmbeddingCache
impl Sync for SemanticEmbeddingCache
impl Unpin for SemanticEmbeddingCache
impl UnsafeUnpin for SemanticEmbeddingCache
impl UnwindSafe for SemanticEmbeddingCache
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.