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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
//! # Semantic Cache Layer
//!
//! A vector-similarity-based cache that returns cached results for queries whose
//! embeddings are semantically close to previously seen queries, avoiding redundant
//! computation.
//!
//! ## Overview
//!
//! The [`SemanticCacheLayer`] maintains a collection of [`CacheEntry`] items, each
//! associated with a query text and its embedding vector. On each lookup the cache
//! computes the cosine similarity between the incoming query embedding and every
//! non-expired stored embedding. If the best match exceeds `config.similarity_threshold`
//! the cached result is returned as a [`CacheLookupResult::Hit`]; otherwise
//! [`CacheLookupResult::Miss`] is returned and the caller should compute the result
//! and insert it.
//!
//! ## Example
//!
//! ```rust
//! use ipfrs_semantic::semantic_cache::{
//! CacheConfig, CacheEvictionPolicy, CacheKey, CacheLookupResult, SemanticCacheLayer,
//! };
//!
//! let config = CacheConfig {
//! max_entries: 128,
//! similarity_threshold: 0.92,
//! ttl_ms: None,
//! eviction_policy: CacheEvictionPolicy::Lru,
//! };
//!
//! let mut cache = SemanticCacheLayer::new(config);
//! let now: u64 = 0;
//!
//! let key = CacheKey {
//! query_text: "hello world".to_string(),
//! embedding: vec![1.0, 0.0, 0.0],
//! };
//! cache.insert(key, "result text".to_string(), now);
//!
//! match cache.lookup(&[0.999, 0.0447, 0.0], now) {
//! CacheLookupResult::Hit { similarity, result, .. } => {
//! println!("Cache hit (similarity={:.3}): {}", similarity, result);
//! }
//! CacheLookupResult::Miss => println!("Cache miss"),
//! }
//! ```
use std::cmp::Ordering;
// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------
/// The key used to store a query in the cache.
#[derive(Debug, Clone)]
pub struct CacheKey {
/// The original query text.
pub query_text: String,
/// The embedding vector for the query.
pub embedding: Vec<f64>,
}
/// A single entry stored in the [`SemanticCacheLayer`].
#[derive(Debug, Clone)]
pub struct CacheEntry {
/// The cache key (query text + embedding).
pub key: CacheKey,
/// The cached result string.
pub result: String,
/// How many times this entry has been returned as a cache hit.
pub hit_count: u64,
/// Timestamp (milliseconds, caller-supplied) when the entry was inserted.
pub inserted_at: u64,
/// Timestamp (milliseconds, caller-supplied) when the entry was last accessed.
pub last_accessed: u64,
/// Optional time-to-live in milliseconds; `None` means the entry never expires.
pub ttl_ms: Option<u64>,
}
impl CacheEntry {
/// Returns `true` if this entry has expired at the given `now` timestamp.
#[inline]
pub fn is_expired(&self, now: u64) -> bool {
match self.ttl_ms {
Some(ttl) => now.saturating_sub(self.inserted_at) > ttl,
None => false,
}
}
}
/// Policy used to select which entry to evict when the cache is full.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CacheEvictionPolicy {
/// Evict the entry that was least recently accessed.
#[default]
Lru,
/// Evict the entry with the lowest hit count.
Lfu,
/// Evict the entry with the soonest expiry; fall back to LRU when no TTLs.
TtlFirst,
}
/// Configuration for the [`SemanticCacheLayer`].
#[derive(Debug, Clone)]
pub struct CacheConfig {
/// Maximum number of entries to hold before eviction kicks in.
pub max_entries: usize,
/// Cosine similarity threshold in [0, 1]. A lookup whose best match is
/// strictly below this value is treated as a miss.
pub similarity_threshold: f64,
/// Default time-to-live (milliseconds) applied to each inserted entry.
/// `None` means entries never expire based on time.
pub ttl_ms: Option<u64>,
/// Eviction strategy to use when the cache reaches `max_entries`.
pub eviction_policy: CacheEvictionPolicy,
}
impl Default for CacheConfig {
fn default() -> Self {
Self {
max_entries: 1024,
similarity_threshold: 0.92,
ttl_ms: None,
eviction_policy: CacheEvictionPolicy::Lru,
}
}
}
/// The result returned by [`SemanticCacheLayer::lookup`].
#[derive(Debug, Clone, PartialEq)]
pub enum CacheLookupResult {
/// A sufficiently similar query was found in the cache.
Hit {
/// Index of the matching entry in the internal entry vector at the time
/// of the lookup (informational; may become stale after mutations).
entry_id: usize,
/// Cosine similarity between the query and the stored embedding.
similarity: f64,
/// The cached result.
result: String,
},
/// No sufficiently similar query was found.
Miss,
}
/// Aggregate statistics returned by [`SemanticCacheLayer::stats`].
#[derive(Debug, Clone)]
pub struct ScCacheStats {
/// Current number of entries in the cache.
pub total_entries: usize,
/// Cumulative cache hits since the cache was created.
pub total_hits: u64,
/// Cumulative cache misses since the cache was created.
pub total_misses: u64,
/// Hit rate (hits / (hits + misses)); 0.0 when no lookups have been made.
pub hit_rate: f64,
/// Total entries ever inserted (not counting evictions/expiry removals).
pub total_insertions: u64,
/// Average `hit_count` across all current entries; 0.0 if empty.
pub avg_hit_count: f64,
}
// ---------------------------------------------------------------------------
// SemanticCacheLayer
// ---------------------------------------------------------------------------
/// A vector-similarity-based cache.
///
/// See the [module-level documentation](self) for a usage example.
#[derive(Debug)]
pub struct SemanticCacheLayer {
/// Runtime configuration.
pub config: CacheConfig,
/// The stored entries.
pub entries: Vec<CacheEntry>,
/// Monotonically increasing counter used to assign stable entry IDs.
pub next_id: usize,
/// Total cache hits since creation.
pub total_hits: u64,
/// Total cache misses since creation.
pub total_misses: u64,
/// Total entries ever inserted.
pub total_insertions: u64,
}
impl SemanticCacheLayer {
// ------------------------------------------------------------------
// Construction
// ------------------------------------------------------------------
/// Create a new cache with the given configuration.
pub fn new(config: CacheConfig) -> Self {
let capacity = config.max_entries.min(4096);
Self {
config,
entries: Vec::with_capacity(capacity),
next_id: 0,
total_hits: 0,
total_misses: 0,
total_insertions: 0,
}
}
// ------------------------------------------------------------------
// Core similarity primitive
// ------------------------------------------------------------------
/// Compute the cosine similarity between two f64 slices.
///
/// Returns `0.0` if the vectors have different lengths, if either vector is
/// all-zeros, or if the denominator underflows to zero.
pub fn cosine_similarity(a: &[f64], b: &[f64]) -> f64 {
if a.len() != b.len() || a.is_empty() {
return 0.0;
}
let mut dot = 0.0_f64;
let mut norm_a = 0.0_f64;
let mut norm_b = 0.0_f64;
for (x, y) in a.iter().zip(b.iter()) {
dot += x * y;
norm_a += x * x;
norm_b += y * y;
}
let denom = norm_a.sqrt() * norm_b.sqrt();
if denom < f64::EPSILON {
return 0.0;
}
(dot / denom).clamp(-1.0, 1.0)
}
// ------------------------------------------------------------------
// Lookup
// ------------------------------------------------------------------
/// Look up the cache for a semantically similar previous query.
///
/// Scans all non-expired entries and finds the one with the highest cosine
/// similarity to `query_embedding`. If that similarity is ≥
/// `config.similarity_threshold` the entry's `hit_count` and `last_accessed`
/// fields are updated and a [`CacheLookupResult::Hit`] is returned; otherwise
/// [`CacheLookupResult::Miss`] is returned.
///
/// The global `total_hits` / `total_misses` counters are always updated.
///
/// # Arguments
///
/// * `query_embedding` — embedding vector for the incoming query.
/// * `now` — current timestamp in milliseconds (caller-supplied for
/// testability).
pub fn lookup(&mut self, query_embedding: &[f64], now: u64) -> CacheLookupResult {
let threshold = self.config.similarity_threshold;
let mut best_idx: Option<usize> = None;
let mut best_sim: f64 = f64::NEG_INFINITY;
for (idx, entry) in self.entries.iter().enumerate() {
if entry.is_expired(now) {
continue;
}
let sim = Self::cosine_similarity(query_embedding, &entry.key.embedding);
if sim > best_sim {
best_sim = sim;
best_idx = Some(idx);
}
}
if let Some(idx) = best_idx {
if best_sim >= threshold {
// Update hit metadata.
self.entries[idx].hit_count += 1;
self.entries[idx].last_accessed = now;
let result = self.entries[idx].result.clone();
let entry_id = idx;
self.total_hits += 1;
return CacheLookupResult::Hit {
entry_id,
similarity: best_sim,
result,
};
}
}
self.total_misses += 1;
CacheLookupResult::Miss
}
// ------------------------------------------------------------------
// Insertion
// ------------------------------------------------------------------
/// Insert a new entry into the cache.
///
/// If the cache has reached `config.max_entries`, one entry is evicted first
/// according to `config.eviction_policy`. The `config.ttl_ms` value is
/// applied to the new entry's `ttl_ms` field.
///
/// # Arguments
///
/// * `key` — the query key (text + embedding).
/// * `result` — the result to cache.
/// * `now` — current timestamp in milliseconds.
pub fn insert(&mut self, key: CacheKey, result: String, now: u64) {
if self.entries.len() >= self.config.max_entries && self.config.max_entries > 0 {
self.evict_one(now);
}
let entry = CacheEntry {
key,
result,
hit_count: 0,
inserted_at: now,
last_accessed: now,
ttl_ms: self.config.ttl_ms,
};
self.entries.push(entry);
self.total_insertions += 1;
self.next_id += 1;
}
// ------------------------------------------------------------------
// Eviction
// ------------------------------------------------------------------
/// Evict a single entry according to the configured [`CacheEvictionPolicy`].
///
/// * **LRU** — remove the entry with the smallest `last_accessed` timestamp.
/// * **LFU** — remove the entry with the smallest `hit_count`.
/// * **TTLFirst** — among entries that have a `ttl_ms`, remove the one that
/// will expire soonest (`inserted_at + ttl_ms` is smallest); if no entry
/// has a TTL, fall back to LRU.
///
/// Does nothing if the cache is empty.
pub fn evict_one(&mut self, now: u64) {
if self.entries.is_empty() {
return;
}
let victim_idx = match self.config.eviction_policy {
CacheEvictionPolicy::Lru => self.find_lru_victim(),
CacheEvictionPolicy::Lfu => self.find_lfu_victim(),
CacheEvictionPolicy::TtlFirst => self.find_ttl_victim(now),
};
if let Some(idx) = victim_idx {
self.entries.swap_remove(idx);
}
}
/// Find the index of the least-recently-used entry.
fn find_lru_victim(&self) -> Option<usize> {
self.entries
.iter()
.enumerate()
.min_by(|(_, a), (_, b)| {
a.last_accessed
.partial_cmp(&b.last_accessed)
.unwrap_or(Ordering::Equal)
})
.map(|(idx, _)| idx)
}
/// Find the index of the least-frequently-used entry.
fn find_lfu_victim(&self) -> Option<usize> {
self.entries
.iter()
.enumerate()
.min_by(|(_, a), (_, b)| {
a.hit_count
.partial_cmp(&b.hit_count)
.unwrap_or(Ordering::Equal)
})
.map(|(idx, _)| idx)
}
/// Find the index of the soonest-expiring entry; fall back to LRU.
fn find_ttl_victim(&self, _now: u64) -> Option<usize> {
// Among entries that have TTLs, pick the one whose absolute expiry
// (inserted_at + ttl_ms) is smallest — i.e. expires soonest.
let ttl_victim = self
.entries
.iter()
.enumerate()
.filter_map(|(idx, e)| e.ttl_ms.map(|ttl| (idx, e.inserted_at.saturating_add(ttl))))
.min_by_key(|&(_, expiry)| expiry)
.map(|(idx, _)| idx);
ttl_victim.or_else(|| self.find_lru_victim())
}
/// Remove all expired entries from the cache.
///
/// Returns the number of entries that were removed.
pub fn evict_expired(&mut self, now: u64) -> usize {
let before = self.entries.len();
self.entries.retain(|e| !e.is_expired(now));
before - self.entries.len()
}
// ------------------------------------------------------------------
// Invalidation / management
// ------------------------------------------------------------------
/// Remove all entries whose `key.query_text` exactly matches `text`.
///
/// Returns the number of entries removed.
pub fn invalidate_by_text(&mut self, text: &str) -> usize {
let before = self.entries.len();
self.entries.retain(|e| e.key.query_text != text);
before - self.entries.len()
}
/// Remove all entries from the cache (statistics are preserved).
pub fn clear(&mut self) {
self.entries.clear();
}
// ------------------------------------------------------------------
// Accessors
// ------------------------------------------------------------------
/// Return the current number of entries in the cache.
pub fn entry_count(&self) -> usize {
self.entries.len()
}
/// Return aggregate statistics for this cache instance.
pub fn stats(&self) -> ScCacheStats {
let total_lookups = self.total_hits + self.total_misses;
let hit_rate = if total_lookups == 0 {
0.0
} else {
self.total_hits as f64 / total_lookups as f64
};
let avg_hit_count = if self.entries.is_empty() {
0.0
} else {
let sum: u64 = self.entries.iter().map(|e| e.hit_count).sum();
sum as f64 / self.entries.len() as f64
};
ScCacheStats {
total_entries: self.entries.len(),
total_hits: self.total_hits,
total_misses: self.total_misses,
hit_rate,
total_insertions: self.total_insertions,
avg_hit_count,
}
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::{
CacheConfig, CacheEntry, CacheEvictionPolicy, CacheKey, CacheLookupResult,
SemanticCacheLayer,
};
// ------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------
fn make_config(max: usize, threshold: f64, policy: CacheEvictionPolicy) -> CacheConfig {
CacheConfig {
max_entries: max,
similarity_threshold: threshold,
ttl_ms: None,
eviction_policy: policy,
}
}
fn make_key(text: &str, embedding: Vec<f64>) -> CacheKey {
CacheKey {
query_text: text.to_string(),
embedding,
}
}
fn unit_vec(dim: usize, hot: usize) -> Vec<f64> {
let mut v = vec![0.0_f64; dim];
v[hot] = 1.0;
v
}
fn normalized(v: Vec<f64>) -> Vec<f64> {
let norm: f64 = v.iter().map(|x| x * x).sum::<f64>().sqrt();
if norm < f64::EPSILON {
return v;
}
v.into_iter().map(|x| x / norm).collect()
}
// ------------------------------------------------------------------
// cosine_similarity
// ------------------------------------------------------------------
#[test]
fn test_cosine_same_vector() {
let v = vec![1.0, 2.0, 3.0];
let sim = SemanticCacheLayer::cosine_similarity(&v, &v);
assert!((sim - 1.0).abs() < 1e-9, "same vector should give sim=1.0");
}
#[test]
fn test_cosine_orthogonal() {
let a = vec![1.0, 0.0, 0.0];
let b = vec![0.0, 1.0, 0.0];
let sim = SemanticCacheLayer::cosine_similarity(&a, &b);
assert!((sim - 0.0).abs() < 1e-9);
}
#[test]
fn test_cosine_opposite() {
let a = vec![1.0, 0.0];
let b = vec![-1.0, 0.0];
let sim = SemanticCacheLayer::cosine_similarity(&a, &b);
assert!((sim + 1.0).abs() < 1e-9);
}
#[test]
fn test_cosine_different_lengths_returns_zero() {
let a = vec![1.0, 0.0];
let b = vec![1.0, 0.0, 0.0];
let sim = SemanticCacheLayer::cosine_similarity(&a, &b);
assert_eq!(sim, 0.0);
}
#[test]
fn test_cosine_zero_vector_a() {
let a = vec![0.0, 0.0];
let b = vec![1.0, 0.0];
let sim = SemanticCacheLayer::cosine_similarity(&a, &b);
assert_eq!(sim, 0.0);
}
#[test]
fn test_cosine_zero_vector_b() {
let a = vec![1.0, 0.0];
let b = vec![0.0, 0.0];
let sim = SemanticCacheLayer::cosine_similarity(&a, &b);
assert_eq!(sim, 0.0);
}
#[test]
fn test_cosine_both_zero() {
let a = vec![0.0; 4];
let b = vec![0.0; 4];
let sim = SemanticCacheLayer::cosine_similarity(&a, &b);
assert_eq!(sim, 0.0);
}
#[test]
fn test_cosine_empty_slices_returns_zero() {
let sim = SemanticCacheLayer::cosine_similarity(&[], &[]);
assert_eq!(sim, 0.0);
}
#[test]
fn test_cosine_known_value() {
// [1,1] vs [1,0] → dot=1, |a|=√2, |b|=1 → sim = 1/√2 ≈ 0.7071
let a = vec![1.0_f64, 1.0];
let b = vec![1.0_f64, 0.0];
let sim = SemanticCacheLayer::cosine_similarity(&a, &b);
let expected = 1.0_f64 / 2.0_f64.sqrt();
assert!((sim - expected).abs() < 1e-9);
}
#[test]
fn test_cosine_clamp_above_one() {
// Floating-point rounding can push the result slightly above 1.0; it
// must be clamped to ≤ 1.0.
let n = 1000;
let v: Vec<f64> = (0..n).map(|i| (i as f64).sin()).collect();
let sim = SemanticCacheLayer::cosine_similarity(&v, &v);
assert!(sim <= 1.0 + 1e-12);
}
// ------------------------------------------------------------------
// Basic insert + lookup
// ------------------------------------------------------------------
#[test]
fn test_lookup_hit_identical_embedding() {
let mut cache = SemanticCacheLayer::new(make_config(16, 0.90, CacheEvictionPolicy::Lru));
let emb = vec![1.0, 0.0, 0.0];
cache.insert(make_key("q1", emb.clone()), "result1".to_string(), 0);
match cache.lookup(&emb, 0) {
CacheLookupResult::Hit {
result, similarity, ..
} => {
assert_eq!(result, "result1");
assert!((similarity - 1.0).abs() < 1e-9);
}
CacheLookupResult::Miss => panic!("Expected a cache hit"),
}
}
#[test]
fn test_lookup_miss_empty_cache() {
let mut cache = SemanticCacheLayer::new(CacheConfig::default());
let result = cache.lookup(&[1.0, 0.0], 0);
assert_eq!(result, CacheLookupResult::Miss);
}
#[test]
fn test_lookup_miss_below_threshold() {
let mut cache = SemanticCacheLayer::new(make_config(16, 0.99, CacheEvictionPolicy::Lru));
let emb = normalized(vec![1.0, 1.0, 0.0]);
cache.insert(make_key("q", emb.clone()), "r".to_string(), 0);
// Query perpendicular to stored embedding → sim = 0.0
let query = vec![0.0, 0.0, 1.0];
assert_eq!(cache.lookup(&query, 0), CacheLookupResult::Miss);
}
#[test]
fn test_lookup_returns_best_match() {
let mut cache = SemanticCacheLayer::new(make_config(16, 0.5, CacheEvictionPolicy::Lru));
cache.insert(make_key("a", unit_vec(3, 0)), "result_a".to_string(), 0);
cache.insert(make_key("b", unit_vec(3, 1)), "result_b".to_string(), 0);
// Query aligned with axis 1 → should match "b"
match cache.lookup(&unit_vec(3, 1), 0) {
CacheLookupResult::Hit { result, .. } => assert_eq!(result, "result_b"),
CacheLookupResult::Miss => panic!("Expected hit"),
}
}
#[test]
fn test_lookup_updates_hit_count() {
let mut cache = SemanticCacheLayer::new(make_config(16, 0.9, CacheEvictionPolicy::Lru));
let emb = unit_vec(3, 0);
cache.insert(make_key("q", emb.clone()), "r".to_string(), 0);
cache.lookup(&emb, 1);
cache.lookup(&emb, 2);
assert_eq!(cache.entries[0].hit_count, 2);
}
#[test]
fn test_lookup_updates_last_accessed() {
let mut cache = SemanticCacheLayer::new(make_config(16, 0.9, CacheEvictionPolicy::Lru));
let emb = unit_vec(3, 0);
cache.insert(make_key("q", emb.clone()), "r".to_string(), 100);
cache.lookup(&emb, 200);
assert_eq!(cache.entries[0].last_accessed, 200);
}
// ------------------------------------------------------------------
// Statistics
// ------------------------------------------------------------------
#[test]
fn test_stats_initial() {
let cache = SemanticCacheLayer::new(CacheConfig::default());
let s = cache.stats();
assert_eq!(s.total_entries, 0);
assert_eq!(s.total_hits, 0);
assert_eq!(s.total_misses, 0);
assert_eq!(s.hit_rate, 0.0);
assert_eq!(s.total_insertions, 0);
assert_eq!(s.avg_hit_count, 0.0);
}
#[test]
fn test_stats_after_inserts_and_lookups() {
let mut cache = SemanticCacheLayer::new(make_config(16, 0.9, CacheEvictionPolicy::Lru));
let emb = unit_vec(3, 0);
cache.insert(make_key("q", emb.clone()), "r".to_string(), 0);
// 2 hits, 1 miss
cache.lookup(&emb, 1);
cache.lookup(&emb, 2);
cache.lookup(&unit_vec(3, 2), 3); // perpendicular → miss
let s = cache.stats();
assert_eq!(s.total_hits, 2);
assert_eq!(s.total_misses, 1);
assert!((s.hit_rate - 2.0 / 3.0).abs() < 1e-9);
assert_eq!(s.total_insertions, 1);
assert_eq!(s.avg_hit_count, 2.0);
}
#[test]
fn test_stats_hit_rate_zero_lookups() {
let mut cache = SemanticCacheLayer::new(CacheConfig::default());
cache.insert(make_key("q", unit_vec(3, 0)), "r".to_string(), 0);
assert_eq!(cache.stats().hit_rate, 0.0);
}
// ------------------------------------------------------------------
// Eviction — LRU
// ------------------------------------------------------------------
#[test]
fn test_lru_eviction_removes_oldest_accessed() {
let mut cache = SemanticCacheLayer::new(make_config(2, 0.9, CacheEvictionPolicy::Lru));
cache.insert(make_key("a", unit_vec(3, 0)), "ra".to_string(), 0);
cache.insert(make_key("b", unit_vec(3, 1)), "rb".to_string(), 5);
// Access "b" so its last_accessed is newer
cache.lookup(&unit_vec(3, 1), 10);
// Insert "c" — must evict "a" (last_accessed=0)
cache.insert(make_key("c", unit_vec(3, 2)), "rc".to_string(), 15);
assert_eq!(cache.entry_count(), 2);
let texts: Vec<&str> = cache
.entries
.iter()
.map(|e| e.key.query_text.as_str())
.collect();
assert!(!texts.contains(&"a"), "LRU should evict 'a'");
}
// ------------------------------------------------------------------
// Eviction — LFU
// ------------------------------------------------------------------
#[test]
fn test_lfu_eviction_removes_lowest_hit_count() {
let mut cache = SemanticCacheLayer::new(make_config(2, 0.9, CacheEvictionPolicy::Lfu));
cache.insert(make_key("a", unit_vec(3, 0)), "ra".to_string(), 0);
cache.insert(make_key("b", unit_vec(3, 1)), "rb".to_string(), 0);
// Give "b" one hit
cache.lookup(&unit_vec(3, 1), 1);
// Insert "c" → must evict "a" (hit_count=0 < b's hit_count=1)
cache.insert(make_key("c", unit_vec(3, 2)), "rc".to_string(), 2);
assert_eq!(cache.entry_count(), 2);
let texts: Vec<&str> = cache
.entries
.iter()
.map(|e| e.key.query_text.as_str())
.collect();
assert!(!texts.contains(&"a"), "LFU should evict 'a'");
}
// ------------------------------------------------------------------
// Eviction — TTLFirst
// ------------------------------------------------------------------
#[test]
fn test_ttlfirst_evicts_soonest_expiring() {
let mut cache = SemanticCacheLayer::new(make_config(2, 0.9, CacheEvictionPolicy::TtlFirst));
// Manually insert entries with different TTLs
cache.entries.push(CacheEntry {
key: make_key("a", unit_vec(3, 0)),
result: "ra".to_string(),
hit_count: 0,
inserted_at: 0,
last_accessed: 0,
ttl_ms: Some(100), // expires at 100
});
cache.entries.push(CacheEntry {
key: make_key("b", unit_vec(3, 1)),
result: "rb".to_string(),
hit_count: 0,
inserted_at: 0,
last_accessed: 0,
ttl_ms: Some(500), // expires at 500
});
// Insert "c" → must evict "a" (expires soonest)
cache.insert(make_key("c", unit_vec(3, 2)), "rc".to_string(), 10);
assert_eq!(cache.entry_count(), 2);
let texts: Vec<&str> = cache
.entries
.iter()
.map(|e| e.key.query_text.as_str())
.collect();
assert!(!texts.contains(&"a"), "TTLFirst should evict 'a'");
}
#[test]
fn test_ttlfirst_fallback_to_lru_when_no_ttls() {
let mut cache = SemanticCacheLayer::new(make_config(2, 0.9, CacheEvictionPolicy::TtlFirst));
cache.insert(make_key("a", unit_vec(3, 0)), "ra".to_string(), 0);
cache.insert(make_key("b", unit_vec(3, 1)), "rb".to_string(), 10);
// Access "b" to update last_accessed
cache.lookup(&unit_vec(3, 1), 20);
// Insert "c" → TTLFirst with no TTLs → fall back to LRU → evict "a"
cache.insert(make_key("c", unit_vec(3, 2)), "rc".to_string(), 30);
let texts: Vec<&str> = cache
.entries
.iter()
.map(|e| e.key.query_text.as_str())
.collect();
assert!(!texts.contains(&"a"));
}
// ------------------------------------------------------------------
// evict_expired
// ------------------------------------------------------------------
#[test]
fn test_evict_expired_removes_expired_entries() {
let mut cache = SemanticCacheLayer::new(CacheConfig {
ttl_ms: Some(100),
..CacheConfig::default()
});
cache.insert(make_key("a", unit_vec(3, 0)), "ra".to_string(), 0);
cache.insert(make_key("b", unit_vec(3, 1)), "rb".to_string(), 50);
// Advance time by 200ms — "a" has expired (200 > 100), "b" has not (200-50=150 > 100 — also expired)
let removed = cache.evict_expired(200);
assert_eq!(removed, 2);
assert_eq!(cache.entry_count(), 0);
}
#[test]
fn test_evict_expired_keeps_non_expired() {
let mut cache = SemanticCacheLayer::new(CacheConfig {
ttl_ms: Some(1000),
..CacheConfig::default()
});
cache.insert(make_key("a", unit_vec(3, 0)), "ra".to_string(), 0);
cache.insert(make_key("b", unit_vec(3, 1)), "rb".to_string(), 0);
let removed = cache.evict_expired(500); // 500ms < 1000ms TTL
assert_eq!(removed, 0);
assert_eq!(cache.entry_count(), 2);
}
#[test]
fn test_evict_expired_no_ttl_never_expires() {
let mut cache = SemanticCacheLayer::new(CacheConfig {
ttl_ms: None,
..CacheConfig::default()
});
cache.insert(make_key("a", unit_vec(3, 0)), "ra".to_string(), 0);
let removed = cache.evict_expired(u64::MAX);
assert_eq!(removed, 0);
assert_eq!(cache.entry_count(), 1);
}
// ------------------------------------------------------------------
// TTL expiry during lookup
// ------------------------------------------------------------------
#[test]
fn test_lookup_skips_expired_entry() {
let mut cache = SemanticCacheLayer::new(CacheConfig {
max_entries: 16,
similarity_threshold: 0.9,
ttl_ms: Some(100),
eviction_policy: CacheEvictionPolicy::Lru,
});
let emb = unit_vec(3, 0);
cache.insert(make_key("q", emb.clone()), "r".to_string(), 0);
// After 200ms the entry is expired; lookup should return Miss
let result = cache.lookup(&emb, 200);
assert_eq!(result, CacheLookupResult::Miss);
}
#[test]
fn test_lookup_hits_non_expired_when_mixed() {
let mut cache = SemanticCacheLayer::new(CacheConfig {
max_entries: 16,
similarity_threshold: 0.9,
ttl_ms: None,
eviction_policy: CacheEvictionPolicy::Lru,
});
// First entry: TTL 100ms, expires at 100
cache.entries.push(CacheEntry {
key: make_key("expired", unit_vec(4, 0)),
result: "old".to_string(),
hit_count: 0,
inserted_at: 0,
last_accessed: 0,
ttl_ms: Some(100),
});
// Second entry: no TTL, same embedding direction
cache.entries.push(CacheEntry {
key: make_key("valid", unit_vec(4, 0)),
result: "new".to_string(),
hit_count: 0,
inserted_at: 50,
last_accessed: 50,
ttl_ms: None,
});
// At t=200 the first entry is expired; lookup should find the second
match cache.lookup(&unit_vec(4, 0), 200) {
CacheLookupResult::Hit { result, .. } => assert_eq!(result, "new"),
CacheLookupResult::Miss => panic!("Expected hit on the non-expired entry"),
}
}
// ------------------------------------------------------------------
// invalidate_by_text
// ------------------------------------------------------------------
#[test]
fn test_invalidate_by_text_removes_matching() {
let mut cache = SemanticCacheLayer::new(CacheConfig::default());
cache.insert(make_key("hello", unit_vec(3, 0)), "r1".to_string(), 0);
cache.insert(make_key("hello", unit_vec(3, 1)), "r2".to_string(), 0);
cache.insert(make_key("world", unit_vec(3, 2)), "r3".to_string(), 0);
let removed = cache.invalidate_by_text("hello");
assert_eq!(removed, 2);
assert_eq!(cache.entry_count(), 1);
assert_eq!(cache.entries[0].key.query_text, "world");
}
#[test]
fn test_invalidate_by_text_no_match_returns_zero() {
let mut cache = SemanticCacheLayer::new(CacheConfig::default());
cache.insert(make_key("hello", unit_vec(3, 0)), "r".to_string(), 0);
let removed = cache.invalidate_by_text("nonexistent");
assert_eq!(removed, 0);
assert_eq!(cache.entry_count(), 1);
}
// ------------------------------------------------------------------
// clear
// ------------------------------------------------------------------
#[test]
fn test_clear_removes_all_entries() {
let mut cache = SemanticCacheLayer::new(CacheConfig::default());
for i in 0..10 {
cache.insert(
make_key(&i.to_string(), unit_vec(3, i % 3)),
"r".to_string(),
0,
);
}
assert_eq!(cache.entry_count(), 10);
cache.clear();
assert_eq!(cache.entry_count(), 0);
}
#[test]
fn test_clear_preserves_statistics() {
let mut cache = SemanticCacheLayer::new(make_config(16, 0.9, CacheEvictionPolicy::Lru));
let emb = unit_vec(3, 0);
cache.insert(make_key("q", emb.clone()), "r".to_string(), 0);
cache.lookup(&emb, 1);
cache.clear();
let s = cache.stats();
assert_eq!(s.total_hits, 1);
assert_eq!(s.total_insertions, 1);
assert_eq!(s.total_entries, 0);
}
// ------------------------------------------------------------------
// entry_count
// ------------------------------------------------------------------
#[test]
fn test_entry_count_tracks_insertions() {
let mut cache = SemanticCacheLayer::new(make_config(100, 0.9, CacheEvictionPolicy::Lru));
assert_eq!(cache.entry_count(), 0);
for i in 0..5 {
cache.insert(
make_key(&i.to_string(), unit_vec(4, i % 4)),
"r".to_string(),
i as u64,
);
}
assert_eq!(cache.entry_count(), 5);
}
// ------------------------------------------------------------------
// max_entries capacity
// ------------------------------------------------------------------
#[test]
fn test_max_entries_respected() {
let mut cache = SemanticCacheLayer::new(make_config(3, 0.9, CacheEvictionPolicy::Lru));
for i in 0..6_usize {
cache.insert(
make_key(&i.to_string(), unit_vec(4, i % 4)),
"r".to_string(),
i as u64,
);
}
assert!(cache.entry_count() <= 3, "Cache exceeded max_entries");
}
#[test]
fn test_max_entries_zero_does_not_insert() {
let mut cache = SemanticCacheLayer::new(make_config(0, 0.9, CacheEvictionPolicy::Lru));
cache.insert(make_key("q", unit_vec(3, 0)), "r".to_string(), 0);
// max_entries=0 means evict immediately before push, so the entry may or may not remain.
// The key invariant is that we do not panic.
let _ = cache.entry_count();
}
// ------------------------------------------------------------------
// CacheEvictionPolicy default
// ------------------------------------------------------------------
#[test]
fn test_eviction_policy_default_is_lru() {
assert_eq!(CacheEvictionPolicy::default(), CacheEvictionPolicy::Lru);
}
// ------------------------------------------------------------------
// CacheConfig default
// ------------------------------------------------------------------
#[test]
fn test_cache_config_defaults() {
let c = CacheConfig::default();
assert_eq!(c.max_entries, 1024);
assert!((c.similarity_threshold - 0.92).abs() < 1e-9);
assert!(c.ttl_ms.is_none());
assert_eq!(c.eviction_policy, CacheEvictionPolicy::Lru);
}
// ------------------------------------------------------------------
// ScCacheStats avg_hit_count
// ------------------------------------------------------------------
#[test]
fn test_avg_hit_count_across_entries() {
let mut cache = SemanticCacheLayer::new(make_config(16, 0.5, CacheEvictionPolicy::Lru));
cache.insert(make_key("a", unit_vec(3, 0)), "ra".to_string(), 0);
cache.insert(make_key("b", unit_vec(3, 1)), "rb".to_string(), 0);
// Hit "a" twice
cache.lookup(&unit_vec(3, 0), 1);
cache.lookup(&unit_vec(3, 0), 2);
// Hit "b" once
cache.lookup(&unit_vec(3, 1), 3);
let s = cache.stats();
// avg_hit_count = (2 + 1) / 2 = 1.5
assert!((s.avg_hit_count - 1.5).abs() < 1e-9);
}
// ------------------------------------------------------------------
// Multi-dimensional embeddings
// ------------------------------------------------------------------
#[test]
fn test_high_dimensional_embedding_hit() {
let dim = 768;
let mut cache = SemanticCacheLayer::new(make_config(16, 0.95, CacheEvictionPolicy::Lru));
let emb: Vec<f64> = (0..dim).map(|i| (i as f64 / dim as f64).sin()).collect();
let emb_n = normalized(emb.clone());
cache.insert(make_key("high-dim", emb_n.clone()), "result".to_string(), 0);
// Slightly perturb the query — should still hit at threshold 0.95
let query: Vec<f64> = emb_n
.iter()
.enumerate()
.map(|(i, x)| x + i as f64 * 1e-5)
.collect();
let query_n = normalized(query);
let result = cache.lookup(&query_n, 0);
assert!(
matches!(result, CacheLookupResult::Hit { .. }),
"Expected hit for slightly perturbed high-dim vector"
);
}
#[test]
fn test_many_insertions_and_lookups() {
let n = 50_usize;
let mut cache = SemanticCacheLayer::new(make_config(n, 0.99, CacheEvictionPolicy::Lfu));
for i in 0..n {
let emb = unit_vec(n, i);
cache.insert(make_key(&format!("q{i}"), emb), format!("r{i}"), i as u64);
}
// Each exact lookup should hit its own entry
let mut hits = 0_u64;
for i in 0..n {
let emb = unit_vec(n, i);
if matches!(
cache.lookup(&emb, n as u64 + i as u64),
CacheLookupResult::Hit { .. }
) {
hits += 1;
}
}
assert_eq!(hits, n as u64);
}
#[test]
fn test_total_insertions_counter() {
let mut cache = SemanticCacheLayer::new(make_config(3, 0.9, CacheEvictionPolicy::Lru));
for i in 0..5_usize {
cache.insert(
make_key(&i.to_string(), unit_vec(3, i % 3)),
"r".to_string(),
i as u64,
);
}
assert_eq!(cache.total_insertions, 5);
}
#[test]
fn test_evict_one_empty_cache_noop() {
let mut cache = SemanticCacheLayer::new(CacheConfig::default());
cache.evict_one(0); // must not panic
assert_eq!(cache.entry_count(), 0);
}
#[test]
fn test_insert_with_ttl_then_lookup() {
let mut cache = SemanticCacheLayer::new(CacheConfig {
max_entries: 16,
similarity_threshold: 0.9,
ttl_ms: Some(500),
eviction_policy: CacheEvictionPolicy::Lru,
});
let emb = unit_vec(3, 0);
cache.insert(make_key("q", emb.clone()), "r".to_string(), 0);
// Within TTL → hit
assert!(
matches!(cache.lookup(&emb, 400), CacheLookupResult::Hit { .. }),
"Should hit within TTL"
);
// After TTL → miss
assert_eq!(
cache.lookup(&emb, 600),
CacheLookupResult::Miss,
"Should miss after TTL"
);
}
}