pub struct EmbeddingSimilarityCache { /* private fields */ }Expand description
Two-level cache for cosine similarity scores between embedding ID pairs.
Entries are evicted using a Least-Frequently-Used (LFU) policy when the
cache reaches max_capacity, and are considered stale after ttl_secs
seconds.
Implementations§
Source§impl EmbeddingSimilarityCache
impl EmbeddingSimilarityCache
Sourcepub fn new(max_capacity: usize, ttl_secs: u64) -> Self
pub fn new(max_capacity: usize, ttl_secs: u64) -> Self
Create a new cache with the given capacity and TTL.
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 similarity score for the pair (a, b).
- Returns
Some(score)and incrementshit_count+stats.hitson a fresh hit. - Removes the entry and increments
stats.misseswhen stale. - Returns
Noneand incrementsstats.misseswhen absent.
Sourcepub fn insert(&mut self, a: u64, b: u64, score: f32)
pub fn insert(&mut self, a: u64, b: u64, score: f32)
Insert a similarity score for the pair (a, b).
When the cache is at capacity the entry with the lowest hit_count is
evicted first (ties broken arbitrarily).
Sourcepub fn cosine_similarity(a: &[f32], b: &[f32]) -> f32
pub fn cosine_similarity(a: &[f32], b: &[f32]) -> f32
Compute the cosine similarity between two dense vectors.
Returns 0.0 if either vector has a zero norm. Vectors of different
lengths are treated as though padded with zeros — only the shorter
length is used for the dot product, and each norm is computed over its
own full slice.
Sourcepub fn compute_and_cache(
&mut self,
a_id: u64,
a_vec: &[f32],
b_id: u64,
b_vec: &[f32],
) -> f32
pub fn compute_and_cache( &mut self, a_id: u64, a_vec: &[f32], b_id: u64, b_vec: &[f32], ) -> f32
Return the cached similarity for (a_id, b_id), computing and caching
it on a miss.
Sourcepub fn evict_stale(&mut self) -> usize
pub fn evict_stale(&mut self) -> usize
Remove all entries whose TTL has expired.
Returns the number of entries removed and updates stats.evictions.
Sourcepub fn stats(&self) -> &CacheStats
pub fn stats(&self) -> &CacheStats
Return a reference to the current cache statistics.
Auto Trait Implementations§
impl Freeze for EmbeddingSimilarityCache
impl RefUnwindSafe for EmbeddingSimilarityCache
impl Send for EmbeddingSimilarityCache
impl Sync for EmbeddingSimilarityCache
impl Unpin for EmbeddingSimilarityCache
impl UnsafeUnpin for EmbeddingSimilarityCache
impl UnwindSafe for EmbeddingSimilarityCache
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.