pub struct SemanticCache { /* private fields */ }Expand description
LRU-evicting semantic similarity cache.
Internally maintains an ordered list of keys (front = most recently used,
back = least recently used) and a HashMap of entries. On lookup the
matching key is moved to the front; on insert an LRU eviction is performed
when the cache is full.
Implementations§
Source§impl SemanticCache
impl SemanticCache
Sourcepub fn new(capacity: usize, similarity_threshold: f64) -> Self
pub fn new(capacity: usize, similarity_threshold: f64) -> Self
Create a new SemanticCache with the given capacity and cosine
similarity threshold in [0.0, 1.0].
Sourcepub fn lookup(&mut self, prompt: &str) -> Option<&str>
pub fn lookup(&mut self, prompt: &str) -> Option<&str>
Look up prompt in the cache.
Returns Some(&str) with the stored response if any entry’s embedding
has cosine similarity ≥ threshold with prompt’s embedding.
The best match is returned and promoted to MRU position.
Sourcepub fn insert(&mut self, prompt: &str, response: String)
pub fn insert(&mut self, prompt: &str, response: String)
Insert a prompt/response pair into the cache.
If the cache is full the LRU entry is evicted first.
If prompt already exists (exact key match) it is updated in place.
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Return a statistics snapshot.
Auto Trait Implementations§
impl Freeze for SemanticCache
impl RefUnwindSafe for SemanticCache
impl Send for SemanticCache
impl Sync for SemanticCache
impl Unpin for SemanticCache
impl UnsafeUnpin for SemanticCache
impl UnwindSafe for SemanticCache
Blanket Implementations§
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
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request