pub struct SemanticCache { /* private fields */ }Expand description
Semantic cache using TF-IDF embeddings and cosine similarity.
The cache embeds every incoming prompt with a refittable TF-IDF model and
performs a brute-force cosine search over stored entries. When a result
above SemanticCacheConfig::similarity_threshold is found and has not
expired, the stored response is returned without running inference.
Thread-safety: all fields are guarded by Mutex. The cache is Send + Sync and can be shared across threads via Arc<SemanticCache>.
Implementations§
Source§impl SemanticCache
impl SemanticCache
Sourcepub fn new(config: SemanticCacheConfig) -> Self
pub fn new(config: SemanticCacheConfig) -> Self
Create a new SemanticCache with the given configuration.
The TF-IDF embedder is bootstrapped with synthetic vocabulary so that
lookup calls before any insert return gracefully.
Sourcepub fn lookup(&self, prompt: &str) -> Option<CachedResponse>
pub fn lookup(&self, prompt: &str) -> Option<CachedResponse>
Check whether a semantically similar response is cached.
Returns None on a miss, or when the best-matching entry has expired.
On a hit, the entry’s hit_count and the global access clock are updated.
Sourcepub fn insert(&self, prompt: &str, response: &str)
pub fn insert(&self, prompt: &str, response: &str)
Store a new prompt→response mapping in the cache.
If the cache is at capacity, the least-recently-used entry is evicted. The TF-IDF embedder is refitted periodically as new prompts accumulate.
Sourcepub fn evict_expired(&self) -> usize
pub fn evict_expired(&self) -> usize
Remove all expired entries from the cache.
Returns the number of entries that were removed.
Sourcepub fn stats(&self) -> SemanticCacheStats
pub fn stats(&self) -> SemanticCacheStats
Snapshot of current cache statistics.
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
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 more