pub struct ResponseCache { /* private fields */ }Implementations§
Source§impl ResponseCache
impl ResponseCache
pub fn new(pool: SqlitePool, ttl_secs: u64) -> Self
Sourcepub async fn get(&self, key: &str) -> Result<Option<String>, MemoryError>
pub async fn get(&self, key: &str) -> Result<Option<String>, MemoryError>
Look up a cached response by key. Returns None if not found or expired.
§Errors
Returns an error if the database query fails.
Sourcepub async fn get_semantic(
&self,
embedding: &[f32],
embedding_model: &str,
similarity_threshold: f32,
max_candidates: u32,
) -> Result<Option<(String, f32)>, MemoryError>
pub async fn get_semantic( &self, embedding: &[f32], embedding_model: &str, similarity_threshold: f32, max_candidates: u32, ) -> Result<Option<(String, f32)>, MemoryError>
Semantic similarity-based cache lookup.
Fetches up to max_candidates non-expired rows with matching embedding_model,
deserializes each embedding, computes cosine similarity against the query vector,
and returns the response with the highest score if it meets similarity_threshold.
Returns (response_text, score) on hit, None on miss.
§Errors
Returns an error if the database query fails.
Sourcepub async fn put_with_embedding(
&self,
key: &str,
response: &str,
model: &str,
embedding: &[f32],
embedding_model: &str,
) -> Result<(), MemoryError>
pub async fn put_with_embedding( &self, key: &str, response: &str, model: &str, embedding: &[f32], embedding_model: &str, ) -> Result<(), MemoryError>
Store a response with an embedding vector for future semantic matching.
Uses INSERT OR REPLACE — updates the embedding on existing rows.
§Errors
Returns an error if the database insert fails.
Sourcepub async fn invalidate_embeddings_for_model(
&self,
old_model: &str,
) -> Result<u64, MemoryError>
pub async fn invalidate_embeddings_for_model( &self, old_model: &str, ) -> Result<u64, MemoryError>
Set embedding = NULL for all rows with the given embedding_model.
Called when the embedding model changes to prevent cross-model false hits. Returns the number of rows updated.
§Errors
Returns an error if the database update fails.
Sourcepub async fn cleanup(
&self,
current_embedding_model: &str,
) -> Result<u64, MemoryError>
pub async fn cleanup( &self, current_embedding_model: &str, ) -> Result<u64, MemoryError>
Two-phase cleanup: delete expired rows, then NULL-ify stale embeddings.
Phase 1: DELETE rows where expires_at <= now.
Phase 2: UPDATE rows where embedding_model != current_embedding_model to NULL out
the embedding columns. Exact-match data (cache_key, response) is preserved.
Returns the total number of rows affected (deleted + updated).
§Errors
Returns an error if either database operation fails.
Sourcepub async fn cleanup_expired(&self) -> Result<u64, MemoryError>
pub async fn cleanup_expired(&self) -> Result<u64, MemoryError>
Delete expired cache entries. Returns the number of rows deleted.
§Errors
Returns an error if the database delete fails.
Sourcepub fn compute_key(last_user_message: &str, model: &str) -> String
pub fn compute_key(last_user_message: &str, model: &str) -> String
Compute a deterministic cache key from the last user message and model name using blake3.
The key intentionally ignores conversation history so that identical user messages produce cache hits regardless of what preceded them. This is the desired behavior for a short-TTL response cache, but it means context-dependent questions (e.g. “Explain this”) may return a cached response from a different context. The TTL bounds staleness.
Auto Trait Implementations§
impl Freeze for ResponseCache
impl !RefUnwindSafe for ResponseCache
impl Send for ResponseCache
impl Sync for ResponseCache
impl Unpin for ResponseCache
impl UnsafeUnpin for ResponseCache
impl !UnwindSafe for ResponseCache
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 moreSource§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