oxirag 0.1.1

A four-layer RAG engine with SMT-based logic verification and knowledge graph support
Documentation
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
//! In-memory prefix cache implementation.
//!
//! This module provides a thread-safe, LRU-based in-memory cache
//! for storing KV cache entries.

use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::Duration;

use async_trait::async_trait;

use super::traits::PrefixCacheStore;
use super::types::{
    CacheKey, CacheLookupResult, CacheStats, ContextFingerprint, KVCacheEntry, PrefixCacheConfig,
};
use crate::error::OxiRagError;

/// An in-memory implementation of `PrefixCacheStore` with LRU eviction.
///
/// This cache provides:
/// - LRU (Least Recently Used) eviction when capacity is reached
/// - TTL-based expiration
/// - Thread-safe access via `RwLock`
/// - Prefix matching for partial cache hits
/// - Memory tracking
///
/// # Example
///
/// ```rust,ignore
/// use oxirag::prefix_cache::{InMemoryPrefixCache, PrefixCacheConfig, PrefixCacheStore};
///
/// let config = PrefixCacheConfig::new(1000, 512 * 1024 * 1024);
/// let mut cache = InMemoryPrefixCache::new(config);
///
/// // Store and retrieve entries
/// cache.put(entry).await?;
/// let retrieved = cache.get(&fingerprint).await;
/// ```
#[derive(Debug)]
pub struct InMemoryPrefixCache {
    /// Configuration for this cache.
    config: PrefixCacheConfig,
    /// Internal storage protected by `RwLock`.
    pub(crate) inner: Arc<RwLock<CacheInner>>,
}

/// Internal cache state.
#[derive(Debug)]
pub(crate) struct CacheInner {
    /// Entries keyed by cache key.
    pub(crate) entries: HashMap<CacheKey, KVCacheEntry>,
    /// Mapping from fingerprint hash to cache key for fast lookup.
    pub(crate) fingerprint_index: HashMap<u64, CacheKey>,
    /// LRU ordering: Vec of cache keys.
    /// Kept sorted by access time (oldest first).
    lru_order: Vec<CacheKey>,
    /// Statistics.
    stats: CacheStats,
    /// Default TTL for new entries.
    default_ttl: Option<Duration>,
    /// Next key ID for generating unique keys.
    next_key_id: u64,
}

impl CacheInner {
    fn new(default_ttl: Option<Duration>) -> Self {
        Self {
            entries: HashMap::new(),
            fingerprint_index: HashMap::new(),
            lru_order: Vec::new(),
            stats: CacheStats::default(),
            default_ttl,
            next_key_id: 0,
        }
    }

    fn generate_key(&mut self) -> CacheKey {
        let key = format!("pc_{}", self.next_key_id);
        self.next_key_id += 1;
        key
    }

    fn update_lru(&mut self, key: &CacheKey) {
        // Remove from current position if exists
        if let Some(pos) = self.lru_order.iter().position(|k| k == key) {
            self.lru_order.remove(pos);
        }
        // Add to end (most recently used)
        self.lru_order.push(key.clone());
    }

    fn remove_from_lru(&mut self, key: &CacheKey) {
        if let Some(pos) = self.lru_order.iter().position(|k| k == key) {
            self.lru_order.remove(pos);
        }
    }

    fn get_lru_key(&self) -> Option<CacheKey> {
        self.lru_order.first().cloned()
    }

    fn calculate_memory(&self) -> usize {
        self.entries
            .values()
            .map(KVCacheEntry::estimated_size)
            .sum()
    }

    fn update_stats(&mut self) {
        self.stats.total_bytes = self.calculate_memory();
        self.stats.entry_count = self.entries.len();
    }
}

impl InMemoryPrefixCache {
    /// Create a new in-memory prefix cache with the given configuration.
    #[must_use]
    pub fn new(config: PrefixCacheConfig) -> Self {
        let default_ttl = if config.default_ttl_secs > 0 {
            Some(Duration::from_secs(config.default_ttl_secs))
        } else {
            None
        };

        Self {
            config,
            inner: Arc::new(RwLock::new(CacheInner::new(default_ttl))),
        }
    }

    /// Create a cache with default configuration.
    #[must_use]
    pub fn with_defaults() -> Self {
        Self::new(PrefixCacheConfig::default())
    }

    /// Get the cache configuration.
    #[must_use]
    pub fn config(&self) -> &PrefixCacheConfig {
        &self.config
    }

    /// Perform a lookup that can return partial matches.
    ///
    /// This is useful for finding the longest cached prefix when
    /// exact matches are not available.
    ///
    /// # Panics
    ///
    /// Panics if the internal lock is poisoned.
    #[must_use]
    pub fn lookup(&self, fingerprint: &ContextFingerprint) -> CacheLookupResult {
        let inner = self.inner.read().expect("lock poisoned");

        // First try exact match
        if let Some(key) = inner.fingerprint_index.get(&fingerprint.hash)
            && let Some(entry) = inner.entries.get(key)
            && !entry.is_expired()
            && entry.fingerprint.prefix_length == fingerprint.prefix_length
        {
            return CacheLookupResult::Hit(entry.clone());
        }

        // Try to find a prefix match
        let mut best_match: Option<&KVCacheEntry> = None;
        let mut best_length = 0;

        for entry in inner.entries.values() {
            if entry.is_expired() {
                continue;
            }
            if entry.fingerprint.is_prefix_of(fingerprint)
                && entry.fingerprint.prefix_length > best_length
            {
                best_match = Some(entry);
                best_length = entry.fingerprint.prefix_length;
            }
        }

        match best_match {
            Some(entry) if best_length == fingerprint.prefix_length => {
                CacheLookupResult::Hit(entry.clone())
            }
            Some(entry) => CacheLookupResult::PartialHit {
                entry: entry.clone(),
                remaining_length: fingerprint.prefix_length - best_length,
            },
            None => CacheLookupResult::Miss,
        }
    }

    /// Evict entries to make room for a new entry of the given size.
    fn evict_for_space(&self, needed_bytes: usize) -> Result<(), OxiRagError> {
        let mut inner = self.inner.write().expect("lock poisoned");

        while inner.calculate_memory() + needed_bytes > self.config.max_memory_bytes
            || inner.entries.len() >= self.config.max_entries
        {
            if let Some(lru_key) = inner.get_lru_key() {
                if let Some(entry) = inner.entries.remove(&lru_key) {
                    inner.fingerprint_index.remove(&entry.fingerprint.hash);
                    inner.remove_from_lru(&lru_key);
                    inner.stats.record_eviction();
                } else {
                    // Key in LRU but not in entries - clean up
                    inner.remove_from_lru(&lru_key);
                }
            } else {
                // No more entries to evict
                break;
            }
        }

        // Check if we have enough space now
        if inner.entries.len() >= self.config.max_entries {
            return Err(OxiRagError::Config(
                "Cache at maximum entry capacity".to_string(),
            ));
        }
        if inner.calculate_memory() + needed_bytes > self.config.max_memory_bytes {
            return Err(OxiRagError::Config(
                "Cache at maximum memory capacity".to_string(),
            ));
        }

        Ok(())
    }
}

impl Clone for InMemoryPrefixCache {
    fn clone(&self) -> Self {
        Self {
            config: self.config.clone(),
            inner: Arc::clone(&self.inner),
        }
    }
}

#[async_trait]
impl PrefixCacheStore for InMemoryPrefixCache {
    async fn get(&self, fingerprint: &ContextFingerprint) -> Option<KVCacheEntry> {
        let mut inner = self.inner.write().expect("lock poisoned");

        let Some(key) = inner.fingerprint_index.get(&fingerprint.hash).cloned() else {
            inner.stats.record_miss();
            return None;
        };

        // Check if entry exists and if it's expired
        let is_expired = inner
            .entries
            .get(&key)
            .is_some_and(KVCacheEntry::is_expired);

        if is_expired {
            // Remove expired entry
            if let Some(removed) = inner.entries.remove(&key) {
                inner.fingerprint_index.remove(&removed.fingerprint.hash);
            }
            inner.remove_from_lru(&key);
            inner.stats.record_expiration();
            inner.stats.record_miss();
            inner.update_stats();
            return None;
        }

        // Get, update access, and return clone
        if let Some(entry) = inner.entries.get_mut(&key) {
            entry.record_access();
            let result = entry.clone();
            inner.update_lru(&key);
            inner.stats.record_hit();
            return Some(result);
        }

        inner.stats.record_miss();
        None
    }

    async fn put(&mut self, entry: KVCacheEntry) -> Result<CacheKey, OxiRagError> {
        let entry_size = entry.estimated_size();
        self.evict_for_space(entry_size)?;

        let mut inner = self.inner.write().expect("lock poisoned");

        // Generate a key if the entry doesn't have one
        let key = if entry.key.is_empty() {
            inner.generate_key()
        } else {
            entry.key.clone()
        };

        // Apply default TTL if entry doesn't have one
        let mut entry = entry;
        if entry.ttl.is_none() {
            entry.ttl = inner.default_ttl;
        }
        entry.key.clone_from(&key);

        // Remove any existing entry with the same fingerprint
        if let Some(old_key) = inner
            .fingerprint_index
            .get(&entry.fingerprint.hash)
            .cloned()
        {
            inner.entries.remove(&old_key);
            inner.remove_from_lru(&old_key);
        }

        inner
            .fingerprint_index
            .insert(entry.fingerprint.hash, key.clone());
        inner.entries.insert(key.clone(), entry);
        inner.update_lru(&key);
        inner.update_stats();

        Ok(key)
    }

    async fn remove(&mut self, key: &CacheKey) -> Option<KVCacheEntry> {
        let mut inner = self.inner.write().expect("lock poisoned");

        if let Some(entry) = inner.entries.remove(key) {
            inner.fingerprint_index.remove(&entry.fingerprint.hash);
            inner.remove_from_lru(key);
            inner.update_stats();
            Some(entry)
        } else {
            None
        }
    }

    async fn contains(&self, fingerprint: &ContextFingerprint) -> bool {
        let inner = self.inner.read().expect("lock poisoned");

        if let Some(key) = inner.fingerprint_index.get(&fingerprint.hash)
            && let Some(entry) = inner.entries.get(key)
        {
            return !entry.is_expired();
        }
        false
    }

    async fn clear(&mut self) {
        let mut inner = self.inner.write().expect("lock poisoned");
        inner.entries.clear();
        inner.fingerprint_index.clear();
        inner.lru_order.clear();
        inner.update_stats();
    }

    fn stats(&self) -> CacheStats {
        let inner = self.inner.read().expect("lock poisoned");
        inner.stats.clone()
    }

    fn len(&self) -> usize {
        let inner = self.inner.read().expect("lock poisoned");
        inner.entries.len()
    }

    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    async fn find_prefix_match(&self, fingerprint: &ContextFingerprint) -> Option<KVCacheEntry> {
        let inner = self.inner.read().expect("lock poisoned");

        let mut best_match: Option<&KVCacheEntry> = None;
        let mut best_length = 0;

        for entry in inner.entries.values() {
            if entry.is_expired() {
                continue;
            }
            if entry.fingerprint.is_prefix_of(fingerprint)
                && entry.fingerprint.prefix_length > best_length
                && entry.fingerprint.prefix_length < fingerprint.prefix_length
            {
                best_match = Some(entry);
                best_length = entry.fingerprint.prefix_length;
            }
        }

        best_match.cloned()
    }

    async fn evict_expired(&mut self) -> usize {
        let mut inner = self.inner.write().expect("lock poisoned");
        let mut expired_keys = Vec::new();

        for (key, entry) in &inner.entries {
            if entry.is_expired() {
                expired_keys.push(key.clone());
            }
        }

        let count = expired_keys.len();
        for key in expired_keys {
            if let Some(entry) = inner.entries.remove(&key) {
                inner.fingerprint_index.remove(&entry.fingerprint.hash);
                inner.remove_from_lru(&key);
                inner.stats.record_expiration();
            }
        }

        inner.update_stats();
        count
    }

    fn memory_usage(&self) -> usize {
        let inner = self.inner.read().expect("lock poisoned");
        inner.calculate_memory()
    }
}

#[cfg(test)]
#[allow(clippy::cast_sign_loss, clippy::float_cmp)]
mod tests {
    use super::*;
    use std::time::Duration;

    fn create_test_entry(id: &str, hash: u64, kv_size: usize) -> KVCacheEntry {
        let fp = ContextFingerprint::new(hash, 100, format!("test {id}"));
        KVCacheEntry::new(id, fp, vec![0.0; kv_size], 100)
    }

    #[tokio::test]
    async fn test_cache_put_and_get() {
        let mut cache = InMemoryPrefixCache::with_defaults();
        let entry = create_test_entry("test1", 12345, 10);
        let fingerprint = entry.fingerprint.clone();

        let key = cache.put(entry).await.unwrap();
        assert!(!key.is_empty());

        let retrieved = cache.get(&fingerprint).await;
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().fingerprint.hash, 12345);
    }

    #[tokio::test]
    async fn test_cache_miss() {
        let cache = InMemoryPrefixCache::with_defaults();
        let fingerprint = ContextFingerprint::new(99999, 100, "nonexistent");

        let result = cache.get(&fingerprint).await;
        assert!(result.is_none());

        let stats = cache.stats();
        assert_eq!(stats.misses, 1);
    }

    #[tokio::test]
    async fn test_cache_contains() {
        let mut cache = InMemoryPrefixCache::with_defaults();
        let entry = create_test_entry("test1", 12345, 10);
        let fingerprint = entry.fingerprint.clone();

        assert!(!cache.contains(&fingerprint).await);

        cache.put(entry).await.unwrap();

        assert!(cache.contains(&fingerprint).await);
    }

    #[tokio::test]
    async fn test_cache_remove() {
        let mut cache = InMemoryPrefixCache::with_defaults();
        let entry = create_test_entry("test1", 12345, 10);
        let fingerprint = entry.fingerprint.clone();

        let key = cache.put(entry).await.unwrap();
        assert_eq!(cache.len(), 1);

        let removed = cache.remove(&key).await;
        assert!(removed.is_some());
        assert_eq!(cache.len(), 0);
        assert!(!cache.contains(&fingerprint).await);
    }

    #[tokio::test]
    async fn test_cache_clear() {
        let mut cache = InMemoryPrefixCache::with_defaults();

        for i in 0..5 {
            let entry = create_test_entry(&format!("test{i}"), i as u64, 10);
            cache.put(entry).await.unwrap();
        }

        assert_eq!(cache.len(), 5);

        cache.clear().await;

        assert_eq!(cache.len(), 0);
        assert!(cache.is_empty());
    }

    #[tokio::test]
    async fn test_cache_stats_tracking() {
        let mut cache = InMemoryPrefixCache::with_defaults();
        let entry = create_test_entry("test1", 12345, 10);
        let fingerprint = entry.fingerprint.clone();

        cache.put(entry).await.unwrap();

        // Hit
        cache.get(&fingerprint).await;
        cache.get(&fingerprint).await;

        // Miss
        let missing_fp = ContextFingerprint::new(99999, 100, "missing");
        cache.get(&missing_fp).await;

        let stats = cache.stats();
        assert_eq!(stats.hits, 2);
        assert_eq!(stats.misses, 1);
        assert!((stats.hit_rate() - 66.666_666_666_666_66).abs() < 0.01);
    }

    #[tokio::test]
    async fn test_cache_lru_eviction() {
        let config = PrefixCacheConfig::new(3, 10 * 1024 * 1024); // Max 3 entries
        let mut cache = InMemoryPrefixCache::new(config);

        // Add 3 entries
        let entry1 = create_test_entry("test1", 1, 10);
        let entry2 = create_test_entry("test2", 2, 10);
        let entry3 = create_test_entry("test3", 3, 10);

        let fp1 = entry1.fingerprint.clone();
        let fp2 = entry2.fingerprint.clone();
        let fp3 = entry3.fingerprint.clone();

        cache.put(entry1).await.unwrap();
        cache.put(entry2).await.unwrap();
        cache.put(entry3).await.unwrap();

        assert_eq!(cache.len(), 3);

        // Access entry1 and entry3 to make entry2 the LRU
        cache.get(&fp1).await;
        cache.get(&fp3).await;

        // Add a 4th entry - should evict entry2 (LRU)
        let entry4 = create_test_entry("test4", 4, 10);
        cache.put(entry4).await.unwrap();

        assert_eq!(cache.len(), 3);
        assert!(!cache.contains(&fp2).await); // entry2 should be evicted
        assert!(cache.contains(&fp1).await);
        assert!(cache.contains(&fp3).await);

        let stats = cache.stats();
        assert_eq!(stats.evictions, 1);
    }

    #[tokio::test]
    async fn test_cache_ttl_expiration() {
        let mut cache = InMemoryPrefixCache::with_defaults();

        // Create entry with immediate TTL (0 seconds = expires immediately)
        let fp = ContextFingerprint::new(12345, 100, "test");
        let entry = KVCacheEntry::new("test1", fp.clone(), vec![0.0; 10], 100)
            .with_ttl(Duration::from_secs(0)); // Immediate expiration

        cache.put(entry).await.unwrap();

        // Entry should be expired immediately
        std::thread::sleep(Duration::from_millis(1));
        let result = cache.get(&fp).await;
        assert!(result.is_none());

        let stats = cache.stats();
        assert_eq!(stats.expirations, 1);
    }

    #[tokio::test]
    async fn test_cache_evict_expired() {
        let mut cache = InMemoryPrefixCache::with_defaults();

        // Add entries with immediate expiration
        for i in 0..5 {
            let fp = ContextFingerprint::new(i as u64, 100, format!("test {i}"));
            let entry = KVCacheEntry::new(format!("test{i}"), fp, vec![0.0; 10], 100)
                .with_ttl(Duration::from_secs(0)); // Immediate expiration
            cache.put(entry).await.unwrap();
        }

        assert_eq!(cache.len(), 5);

        std::thread::sleep(Duration::from_millis(1));
        let evicted = cache.evict_expired().await;

        assert_eq!(evicted, 5);
        assert_eq!(cache.len(), 0);
    }

    #[tokio::test]
    async fn test_cache_memory_tracking() {
        let mut cache = InMemoryPrefixCache::with_defaults();

        let entry = create_test_entry("test1", 12345, 100);
        cache.put(entry).await.unwrap();

        let memory = cache.memory_usage();
        assert!(memory > 0);
        assert!(memory >= 100 * std::mem::size_of::<f32>());
    }

    #[tokio::test]
    async fn test_cache_memory_limit() {
        let config = PrefixCacheConfig::new(1000, 1000); // Very small memory limit
        let mut cache = InMemoryPrefixCache::new(config);

        // Try to add entries that exceed memory limit
        for i in 0..10 {
            let entry = create_test_entry(&format!("test{i}"), i as u64, 100);
            let _ = cache.put(entry).await;
        }

        // Should have evicted some entries
        assert!(cache.memory_usage() <= 1000 || cache.len() <= 2);
    }

    #[tokio::test]
    async fn test_cache_prefix_match() {
        let mut cache = InMemoryPrefixCache::with_defaults();

        // Add entry with prefix length 50
        let short_fp = ContextFingerprint::new(100, 50, "short");
        let short_entry = KVCacheEntry::new("short", short_fp.clone(), vec![1.0; 10], 50);
        cache.put(short_entry).await.unwrap();

        // Look for prefix of longer content
        let long_fp = ContextFingerprint::new(200, 100, "long");
        let prefix_match = cache.find_prefix_match(&long_fp).await;

        assert!(prefix_match.is_some());
        assert_eq!(prefix_match.unwrap().fingerprint.prefix_length, 50);
    }

    #[tokio::test]
    async fn test_cache_lookup_result_types() {
        let mut cache = InMemoryPrefixCache::with_defaults();

        // Add entry with prefix length 50
        let fp = ContextFingerprint::new(100, 50, "test");
        let entry = KVCacheEntry::new("test", fp.clone(), vec![1.0; 10], 50);
        cache.put(entry).await.unwrap();

        // Exact match
        let result = cache.lookup(&fp);
        assert!(matches!(result, CacheLookupResult::Hit(_)));

        // Miss - use a shorter prefix length so is_prefix_of won't match
        // (since 50 is not <= 30, no prefix match is found)
        let missing_fp = ContextFingerprint::new(999, 30, "missing");
        let result = cache.lookup(&missing_fp);
        assert!(matches!(result, CacheLookupResult::Miss));
    }

    #[tokio::test]
    async fn test_cache_update_existing() {
        let mut cache = InMemoryPrefixCache::with_defaults();

        let fp = ContextFingerprint::new(12345, 100, "test");
        let entry1 = KVCacheEntry::new("test1", fp.clone(), vec![1.0; 10], 100);
        let entry2 = KVCacheEntry::new("test2", fp.clone(), vec![2.0; 10], 100);

        cache.put(entry1).await.unwrap();
        cache.put(entry2).await.unwrap();

        // Should only have one entry (updated)
        assert_eq!(cache.len(), 1);

        let retrieved = cache.get(&fp).await.unwrap();
        assert_eq!(retrieved.kv_data[0], 2.0);
    }

    #[tokio::test]
    async fn test_cache_clone_shares_state() {
        let mut cache1 = InMemoryPrefixCache::with_defaults();
        let cache2 = cache1.clone();

        let entry = create_test_entry("test1", 12345, 10);
        let fingerprint = entry.fingerprint.clone();

        cache1.put(entry).await.unwrap();

        // Both caches should see the entry
        assert!(cache2.contains(&fingerprint).await);
        assert_eq!(cache1.len(), cache2.len());
    }

    #[tokio::test]
    async fn test_cache_access_updates_lru() {
        let config = PrefixCacheConfig::new(2, 10 * 1024 * 1024);
        let mut cache = InMemoryPrefixCache::new(config);

        let entry1 = create_test_entry("test1", 1, 10);
        let entry2 = create_test_entry("test2", 2, 10);

        let fp1 = entry1.fingerprint.clone();
        let fp2 = entry2.fingerprint.clone();

        cache.put(entry1).await.unwrap();
        cache.put(entry2).await.unwrap();

        // Access entry1 to make it more recent than entry2
        cache.get(&fp1).await;

        // Add entry3 - should evict entry2 (LRU)
        let entry3 = create_test_entry("test3", 3, 10);
        cache.put(entry3).await.unwrap();

        assert!(cache.contains(&fp1).await);
        assert!(!cache.contains(&fp2).await);
    }
}