1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//! Embedding cache for reducing API calls.
//!
//! Provides LRU cache with TTL for embedding vectors.
//!
//! # Example
//!
//! ```
//! use oxios_kernel::memory::EmbeddingCache;
//!
//! let cache = EmbeddingCache::new(3600, 10000); // 1 hour TTL, 10k max
//! cache.insert("hello", vec![1.0, 2.0, 3.0]);
//! let embedded = cache.get("hello");
//! assert!(embedded.is_some());
//! ```
use lru::LruCache;
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::hash::{Hash, Hasher};
use std::time::{Duration, Instant};
/// Cache entry with TTL tracking.
struct CacheEntry<V> {
value: V,
created_at: Instant,
ttl: Duration,
}
impl<V> CacheEntry<V> {
fn is_expired(&self) -> bool {
self.created_at.elapsed() > self.ttl
}
}
/// Content-addressable embedding cache with TTL and LRU eviction.
pub struct EmbeddingCache {
inner: RwLock<LruCache<u64, CacheEntry<Vec<f32>>>>,
ttl: Duration,
max_entries: usize,
hits: RwLock<u64>,
misses: RwLock<u64>,
}
/// Cache statistics for monitoring.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheStats {
/// Number of cache hits.
pub hits: u64,
/// Number of cache misses.
pub misses: u64,
/// Hit rate as a fraction (0.0 to 1.0).
pub hit_rate: f64,
/// Current number of entries in cache.
pub size: usize,
/// Maximum capacity of cache.
pub capacity: usize,
}
impl EmbeddingCache {
/// Create a new cache with TTL and capacity.
///
/// # Arguments
/// * `ttl_secs` - Time-to-live for cached entries in seconds
/// * `max_entries` - Maximum number of entries to cache
pub fn new(ttl_secs: u64, max_entries: usize) -> Self {
Self {
inner: RwLock::new(LruCache::new(
std::num::NonZeroUsize::new(max_entries).unwrap_or(std::num::NonZeroUsize::MIN),
)),
ttl: Duration::from_secs(ttl_secs),
max_entries,
hits: RwLock::new(0),
misses: RwLock::new(0),
}
}
/// Hash content to cache key.
pub fn content_hash(content: &str) -> u64 {
use std::collections::hash_map::DefaultHasher;
let mut hasher = DefaultHasher::new();
content.hash(&mut hasher);
hasher.finish()
}
/// Get cached embedding if exists and not expired.
pub fn get(&self, content: &str) -> Option<Vec<f32>> {
let key = Self::content_hash(content);
let mut inner = self.inner.write();
match inner.get(&key) {
Some(entry) if !entry.is_expired() => {
*self.hits.write() += 1;
Some(entry.value.clone())
}
Some(_) => {
// Expired — remove
inner.pop(&key);
*self.misses.write() += 1;
None
}
None => {
*self.misses.write() += 1;
None
}
}
}
/// Cache an embedding.
pub fn insert(&self, content: &str, embedding: Vec<f32>) {
let key = Self::content_hash(content);
let mut inner = self.inner.write();
inner.push(
key,
CacheEntry {
value: embedding,
created_at: Instant::now(),
ttl: self.ttl,
},
);
}
/// Evict expired entries.
///
/// Returns the number of entries evicted.
pub fn evict_expired(&self) -> usize {
let mut inner = self.inner.write();
let mut evicted = 0;
let keys: Vec<_> = inner
.iter()
.filter(|(_, entry)| entry.is_expired())
.map(|(k, _)| *k)
.collect();
for key in keys {
inner.pop(&key);
evicted += 1;
}
evicted
}
/// Evict least recently used entries to free space.
///
/// Returns the number of entries evicted.
pub fn evict_lru(&self, target_size: usize) -> usize {
let mut inner = self.inner.write();
let mut evicted = 0;
while inner.len() > target_size {
if inner.pop_lru().is_none() {
break;
}
evicted += 1;
}
evicted
}
/// Cache statistics.
pub fn stats(&self) -> CacheStats {
let hits = *self.hits.read();
let misses = *self.misses.read();
let total = hits + misses;
CacheStats {
hits,
misses,
hit_rate: if total > 0 {
hits as f64 / total as f64
} else {
0.0
},
size: self.inner.read().len(),
capacity: self.max_entries,
}
}
/// Clear the cache.
pub fn clear(&self) {
self.inner.write().clear();
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::thread;
use std::time::Duration;
#[test]
fn test_cache_basic() {
let cache = EmbeddingCache::new(60, 100);
// Insert
cache.insert("hello", vec![1.0, 2.0, 3.0]);
// Get
let result = cache.get("hello");
assert!(result.is_some());
assert_eq!(result.unwrap(), vec![1.0, 2.0, 3.0]);
// Stats
let stats = cache.stats();
assert_eq!(stats.hits, 1);
assert_eq!(stats.misses, 0);
}
#[test]
fn test_cache_miss() {
let cache = EmbeddingCache::new(60, 100);
let result = cache.get("nonexistent");
assert!(result.is_none());
let stats = cache.stats();
assert_eq!(stats.hits, 0);
assert_eq!(stats.misses, 1);
}
#[test]
fn test_cache_ttl() {
let cache = EmbeddingCache::new(1, 100); // 1 second TTL
cache.insert("test", vec![1.0]);
assert!(cache.get("test").is_some());
// Wait for expiration
thread::sleep(Duration::from_millis(1_100));
// Should be expired
assert!(cache.get("test").is_none());
}
#[test]
fn test_cache_eviction() {
let cache = EmbeddingCache::new(60, 2);
cache.insert("a", vec![1.0]);
cache.insert("b", vec![2.0]);
cache.insert("c", vec![3.0]); // Should evict oldest
// a should be evicted
assert!(cache.get("a").is_none());
// b and c should exist
assert!(cache.get("b").is_some());
assert!(cache.get("c").is_some());
}
}