pub struct SemanticSimilarityCache { /* private fields */ }Expand description
Pairwise cosine-similarity cache with LFU eviction and TTL staleness.
All public mutating methods are &mut self; no internal locking is
performed — the caller is responsible for synchronisation when used from
multiple threads.
Implementations§
Source§impl SemanticSimilarityCache
impl SemanticSimilarityCache
Sourcepub fn new(config: PairCacheConfig) -> Self
pub fn new(config: PairCacheConfig) -> Self
Create a new cache with the given PairCacheConfig.
Sourcepub fn get(&mut self, a: u64, b: u64) -> Option<f32>
pub fn get(&mut self, a: u64, b: u64) -> Option<f32>
Look up the cached similarity for the pair (a, b).
Returns None when:
- the pair is not in the cache, or
- the cached entry is stale (older than
ttl_ticks).
On a hit the entry’s access_count is incremented.
Sourcepub fn insert(&mut self, a: u64, b: u64, similarity: f32)
pub fn insert(&mut self, a: u64, b: u64, similarity: f32)
Insert (or update) the similarity score for the pair (a, b).
When the cache is at capacity the entry with the lowest
access_count is evicted first (LFU).
Sourcepub fn compute_and_cache(
&mut self,
a: u64,
vec_a: &[f32],
b: u64,
vec_b: &[f32],
) -> f32
pub fn compute_and_cache( &mut self, a: u64, vec_a: &[f32], b: u64, vec_b: &[f32], ) -> f32
Compute the cosine similarity between vec_a and vec_b, cache the
result under (a, b), and return the score.
If a == b the result is 1.0 without evaluating the vectors.
Sourcepub fn evict_stale(&mut self) -> usize
pub fn evict_stale(&mut self) -> usize
Remove all stale entries from the cache and return the number removed.
Sourcepub fn advance_tick(&mut self)
pub fn advance_tick(&mut self)
Advance the internal logical clock by one tick.
Sourcepub fn stats(&self) -> &PairCacheStats
pub fn stats(&self) -> &PairCacheStats
Return a reference to the current aggregate statistics.
Sourcepub fn current_tick(&self) -> u64
pub fn current_tick(&self) -> u64
Return the current tick value.
Auto Trait Implementations§
impl Freeze for SemanticSimilarityCache
impl RefUnwindSafe for SemanticSimilarityCache
impl Send for SemanticSimilarityCache
impl Sync for SemanticSimilarityCache
impl Unpin for SemanticSimilarityCache
impl UnsafeUnpin for SemanticSimilarityCache
impl UnwindSafe for SemanticSimilarityCache
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.