patronus_ark/cache/
store.rs1use thiserror::Error;
3
4use super::{CacheKey, CachedHeadOutput, CachedModelOutput};
5
6#[derive(Debug, Clone)]
7pub(crate) struct SimilarityStoreRecord {
8 pub(crate) id: [u8; 32],
9 pub(crate) vector_space: String,
10 pub(crate) producer_model_sha: String,
11 pub(crate) embedding: Vec<f32>,
12 pub(crate) heads: Vec<CachedHeadOutput>,
13 pub(crate) bucket_keys: Vec<Vec<u8>>,
14 pub(crate) created_at_unix_ms: u64,
15 pub(crate) expires_at_unix_ms: u64,
16}
17
18#[derive(Debug, Error)]
19pub enum CacheError {
20 #[error("cache storage error: {0}")]
21 Storage(String),
22 #[error("invalid cache record: {0}")]
23 InvalidRecord(String),
24}
25
26pub(crate) trait ExactCacheStore: Send + Sync {
27 fn get(
28 &self,
29 key: &CacheKey,
30 now_unix_ms: u64,
31 ) -> Result<Option<CachedModelOutput>, CacheError>;
32
33 fn put(&self, key: CacheKey, value: CachedModelOutput) -> Result<(), CacheError>;
34
35 fn put_batch(&self, entries: Vec<(CacheKey, CachedModelOutput)>) -> Result<(), CacheError> {
36 for (key, value) in entries {
37 self.put(key, value)?;
38 }
39 Ok(())
40 }
41
42 fn remove_expired(&self, now_unix_ms: u64) -> Result<usize, CacheError>;
43
44 fn remove_created_before(&self, until_unix_ms: u64) -> Result<usize, CacheError>;
45
46 fn similarity_candidates(
47 &self,
48 _bucket_keys: &[Vec<u8>],
49 _now_unix_ms: u64,
50 _limit: usize,
51 ) -> Result<Vec<SimilarityStoreRecord>, CacheError> {
52 Ok(Vec::new())
53 }
54
55 fn put_similarity(&self, _record: SimilarityStoreRecord) -> Result<(), CacheError> {
56 Ok(())
57 }
58
59 fn put_similarity_batch(&self, records: Vec<SimilarityStoreRecord>) -> Result<(), CacheError> {
60 for record in records {
61 self.put_similarity(record)?;
62 }
63 Ok(())
64 }
65
66 fn flush(&self) -> Result<(), CacheError> {
67 Ok(())
68 }
69}