cortexai_cache/traits.rs
1//! Cache trait definitions
2
3use async_trait::async_trait;
4
5use crate::{CacheEntry, CacheError, CacheStats};
6
7/// Trait for cache implementations
8#[async_trait]
9pub trait Cache: Send + Sync {
10 /// Get an entry from the cache
11 async fn get(&self, query: &str, context: &str) -> Result<Option<CacheEntry>, CacheError>;
12
13 /// Store an entry in the cache
14 async fn store(
15 &self,
16 query: &str,
17 context: &str,
18 response: &str,
19 function_calls: Vec<String>,
20 ) -> Result<(), CacheError>;
21
22 /// Delete an entry from the cache
23 async fn delete(&self, query: &str, context: &str) -> Result<bool, CacheError>;
24
25 /// Clear all entries
26 async fn clear(&self) -> Result<usize, CacheError>;
27
28 /// Get cache statistics
29 async fn stats(&self) -> Result<CacheStats, CacheError>;
30
31 /// Check if cache contains an entry
32 async fn contains(&self, query: &str, context: &str) -> Result<bool, CacheError> {
33 Ok(self.get(query, context).await?.is_some())
34 }
35}
36
37/// Trait for semantic cache with similarity matching
38///
39/// Unlike regular caching, semantic caching finds similar queries
40/// based on embedding vectors rather than exact string matches.
41#[async_trait]
42pub trait SemanticCache: Cache {
43 /// Find similar entries based on embedding similarity
44 ///
45 /// Takes a pre-computed embedding vector and finds cached entries
46 /// with embeddings above the similarity threshold.
47 ///
48 /// Returns the best matching entry and its similarity score (0.0 to 1.0).
49 async fn find_similar_by_embedding(
50 &self,
51 query_embedding: &[f32],
52 threshold: f32,
53 ) -> Result<Option<(CacheEntry, f32)>, CacheError>;
54
55 /// Store with embedding for semantic matching
56 ///
57 /// Stores the response along with its embedding vector for
58 /// future similarity searches.
59 async fn store_with_embedding(
60 &self,
61 query: &str,
62 context: &str,
63 response: &str,
64 function_calls: Vec<String>,
65 embedding: Vec<f32>,
66 ) -> Result<(), CacheError>;
67}