litellm-rs 0.9.0

A high-performance AI Gateway written in Rust, providing OpenAI-compatible APIs with intelligent routing, load balancing, and enterprise features
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
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
use super::*;

#[tokio::test(flavor = "current_thread")]
async fn cleanup_task_does_not_retain_discarded_cache() {
    let mut signals = Vec::new();
    for _ in 0..100 {
        let cache = Arc::new(InMemoryCache::<String>::new(DualCacheConfig {
            cleanup_interval: Duration::from_secs(3600),
            ..DualCacheConfig::memory_only()
        }));
        let weak = Arc::downgrade(&cache);
        signals.push(cache.shutdown.clone());
        cache.start_cleanup_task();
        drop(cache);
        assert!(
            weak.upgrade().is_none(),
            "cleanup retained a discarded cache"
        );
    }
    tokio::time::timeout(Duration::from_secs(1), async {
        while signals.iter().any(|signal| signal.receiver_count() != 0) {
            tokio::task::yield_now().await;
        }
    })
    .await
    .expect("discarded cleanup tasks must exit before the next hourly interval");
}

#[tokio::test(flavor = "current_thread")]
async fn cleanup_shutdown_before_first_poll_is_persistent() {
    for start_first in [false, true] {
        let cache = Arc::new(InMemoryCache::<String>::with_defaults());
        if start_first {
            cache.start_cleanup_task();
        }
        cache.shutdown();
        cache.start_cleanup_task();
        tokio::time::timeout(Duration::from_secs(1), async {
            while cache.shutdown.receiver_count() != 0 {
                tokio::task::yield_now().await;
            }
        })
        .await
        .expect("shutdown must remain visible to a task that has not polled");
    }
}

#[tokio::test(flavor = "current_thread")]
async fn repeated_cleanup_start_has_one_task_and_shutdown_preserves_cached_values() {
    let cache = Arc::new(InMemoryCache::<String>::with_defaults());
    for _ in 0..100 {
        cache.start_cleanup_task();
    }
    assert_eq!(cache.shutdown.receiver_count(), 1);
    let key = CacheKey::new("pinned-cache-value");
    cache.set(key.clone(), "kept".into()).await;
    tokio::task::yield_now().await;
    cache.shutdown();
    tokio::time::timeout(Duration::from_secs(1), async {
        while cache.shutdown.receiver_count() != 0 {
            tokio::task::yield_now().await;
        }
    })
    .await
    .unwrap();
    assert_eq!(cache.get(&key).await.as_deref(), Some("kept"));
}

#[test]
fn test_cache_new() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();
    assert!(cache.is_empty());
    assert_eq!(cache.len(), 0);
}

#[tokio::test]
async fn test_cache_set_get() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();
    let key = CacheKey::new("test-key");

    cache.set(key.clone(), "test-value".to_string()).await;

    let result = cache.get(&key).await;
    assert_eq!(result, Some("test-value".to_string()));
}

#[tokio::test]
async fn test_cache_get_nonexistent() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();
    let key = CacheKey::new("nonexistent");

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

#[tokio::test]
async fn test_cache_delete() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();
    let key = CacheKey::new("to-delete");

    cache.set(key.clone(), "value".to_string()).await;
    assert!(cache.exists(&key).await);

    let deleted = cache.delete(&key).await;
    assert!(deleted);
    assert!(!cache.exists(&key).await);
}

#[tokio::test]
async fn test_cache_delete_nonexistent() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();
    let key = CacheKey::new("nonexistent");

    let deleted = cache.delete(&key).await;
    assert!(!deleted);
}

#[tokio::test]
async fn test_cache_exists() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();
    let key = CacheKey::new("exists-key");

    assert!(!cache.exists(&key).await);
    cache.set(key.clone(), "value".to_string()).await;
    assert!(cache.exists(&key).await);
}

#[tokio::test]
async fn test_cache_clear() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();

    cache.set(CacheKey::new("key1"), "value1".to_string()).await;
    cache.set(CacheKey::new("key2"), "value2".to_string()).await;
    cache.set(CacheKey::new("key3"), "value3".to_string()).await;

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

#[tokio::test]
async fn test_cache_ttl_expiration() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();
    let key = CacheKey::new("expiring-key");

    cache
        .set_with_ttl(key.clone(), "value".to_string(), Duration::from_millis(10))
        .await;
    assert!(cache.exists(&key).await);

    tokio::time::sleep(Duration::from_millis(20)).await;
    assert!(!cache.exists(&key).await);
}

#[tokio::test]
async fn test_cache_get_expired() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();
    let key = CacheKey::new("expiring-key");

    cache
        .set_with_ttl(key.clone(), "value".to_string(), Duration::from_millis(10))
        .await;
    tokio::time::sleep(Duration::from_millis(20)).await;

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

#[tokio::test]
async fn test_cache_ttl_remaining() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();
    let key = CacheKey::new("ttl-key");

    cache
        .set_with_ttl(key.clone(), "value".to_string(), Duration::from_secs(60))
        .await;

    let ttl = cache.ttl(&key);
    assert!(matches!(
        ttl,
        Some(remaining) if remaining <= Duration::from_secs(60)
    ));
}

#[tokio::test]
async fn test_cache_lru_eviction() {
    let config = DualCacheConfig::default()
        .with_max_size(3)
        .with_eviction_policy(EvictionPolicy::LRU);
    let cache: InMemoryCache<String> = InMemoryCache::new(config);

    cache.set(CacheKey::new("key1"), "value1".to_string()).await;
    cache.set(CacheKey::new("key2"), "value2".to_string()).await;
    cache.set(CacheKey::new("key3"), "value3".to_string()).await;

    cache.get(&CacheKey::new("key1")).await;
    cache.get(&CacheKey::new("key2")).await;
    cache.set(CacheKey::new("key4"), "value4".to_string()).await;

    assert!(cache.exists(&CacheKey::new("key1")).await);
    assert!(cache.exists(&CacheKey::new("key2")).await);
    assert!(!cache.exists(&CacheKey::new("key3")).await);
    assert!(cache.exists(&CacheKey::new("key4")).await);
}

#[tokio::test]
async fn test_cache_lfu_eviction() {
    let config = DualCacheConfig::default()
        .with_max_size(3)
        .with_eviction_policy(EvictionPolicy::LFU);
    let cache: InMemoryCache<String> = InMemoryCache::new(config);

    cache.set(CacheKey::new("key1"), "value1".to_string()).await;
    cache.set(CacheKey::new("key2"), "value2".to_string()).await;
    cache.set(CacheKey::new("key3"), "value3".to_string()).await;

    for _ in 0..5 {
        cache.get(&CacheKey::new("key1")).await;
    }
    for _ in 0..2 {
        cache.get(&CacheKey::new("key2")).await;
    }
    cache.set(CacheKey::new("key4"), "value4".to_string()).await;

    assert!(cache.exists(&CacheKey::new("key1")).await);
    assert!(cache.exists(&CacheKey::new("key2")).await);
    assert!(!cache.exists(&CacheKey::new("key3")).await);
    assert!(cache.exists(&CacheKey::new("key4")).await);
}

#[tokio::test]
async fn test_cache_ttl_eviction_prefers_expired_entry() {
    let config = DualCacheConfig::default()
        .with_max_size(3)
        .with_eviction_policy(EvictionPolicy::TTL);
    let cache: InMemoryCache<String> = InMemoryCache::new(config);

    cache
        .set_with_ttl(
            CacheKey::new("short"),
            "value1".to_string(),
            Duration::from_millis(10),
        )
        .await;
    cache
        .set_with_ttl(
            CacheKey::new("long1"),
            "value2".to_string(),
            Duration::from_secs(60),
        )
        .await;
    cache
        .set_with_ttl(
            CacheKey::new("long2"),
            "value3".to_string(),
            Duration::from_secs(60),
        )
        .await;

    tokio::time::sleep(Duration::from_millis(20)).await;
    cache.set(CacheKey::new("new"), "value4".to_string()).await;

    assert!(!cache.exists(&CacheKey::new("short")).await);
    assert!(cache.exists(&CacheKey::new("long1")).await);
    assert!(cache.exists(&CacheKey::new("long2")).await);
    assert!(cache.exists(&CacheKey::new("new")).await);
}

#[tokio::test]
async fn test_cache_stale_eviction_candidate_keeps_reinserted_metadata() {
    let config = DualCacheConfig::default()
        .with_max_size(2)
        .with_eviction_policy(EvictionPolicy::LRU);
    let cache: InMemoryCache<String> = InMemoryCache::new(config);
    let key = CacheKey::new("stale-candidate");

    cache.set(key.clone(), "old".to_string()).await;
    let Some(old_entry) = cache.cache.get(&key).map(|entry| entry.clone()) else {
        panic!("old entry should exist before stale candidate");
    };
    let Some((last_access_tick, access_count)) = cache
        .access_shard(&key)
        .get(&key)
        .map(|meta| meta.snapshot())
    else {
        panic!("old metadata should exist");
    };
    let stale_candidate = EvictionCandidate {
        key: key.clone(),
        last_access_tick,
        access_count,
        created_at: old_entry.created_at,
        remaining_ttl: old_entry.remaining_ttl(),
    };

    cache.cache.remove(&key);
    cache.set(key.clone(), "new".to_string()).await;
    assert!(
        !cache.evict_candidate(stale_candidate, "LRU"),
        "stale eviction candidate should report that no live entry was removed"
    );

    assert_eq!(cache.get(&key).await, Some("new".to_string()));
    assert!(
        cache.access_shard(&key).contains_key(&key),
        "metadata for the reinserted generation must remain"
    );
}

#[tokio::test]
async fn test_delete_if_preserves_replacement_access_meta() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();
    let key = CacheKey::new("delete-if-meta");

    cache.set(key.clone(), "poison".to_string()).await;
    let meta_before = cache
        .access_shard(&key)
        .get(&key)
        .map(|meta| meta.snapshot())
        .expect("poison entry must have access metadata");

    // Simulate the race: remove_if succeeds on poison, then a replacement is
    // inserted and resets access meta before bookkeeping runs.
    let removed = cache
        .cache
        .remove_if(&key, |_k, entry| entry.value == "poison");
    assert!(removed.is_some());
    cache.set(key.clone(), "safe".to_string()).await;
    let meta_after_replace = cache
        .access_shard(&key)
        .get(&key)
        .map(|meta| meta.snapshot())
        .expect("replacement must have access metadata");
    assert_ne!(
        meta_before, meta_after_replace,
        "replacement should reset access metadata"
    );

    cache.remove_access_meta(&key);

    assert!(
        cache.access_shard(&key).contains_key(&key),
        "delete_if must not clear replacement access metadata"
    );
    assert_eq!(
        cache
            .access_shard(&key)
            .get(&key)
            .map(|meta| meta.snapshot()),
        Some(meta_after_replace),
        "stale delete bookkeeping must leave the replacement's access tick intact"
    );
    assert_eq!(cache.get(&key).await, Some("safe".to_string()));
}

#[tokio::test]
async fn test_delete_if_clears_matching_entry_and_meta() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();
    let key = CacheKey::new("delete-if-match");

    cache.set(key.clone(), "poison".to_string()).await;
    assert!(cache.delete_if(&key, |v| v == "poison").await.is_some());
    assert!(cache.get(&key).await.is_none());
    assert!(
        !cache.access_shard(&key).contains_key(&key),
        "matching delete_if should clear access metadata for the removed entry"
    );
}

#[tokio::test]
async fn test_cache_stats_hits_misses() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();
    let key = CacheKey::new("stats-key");

    cache.set(key.clone(), "value".to_string()).await;
    cache.get(&key).await;
    cache.get(&key).await;
    cache.get(&CacheKey::new("nonexistent1")).await;
    cache.get(&CacheKey::new("nonexistent2")).await;

    let stats = cache.stats().snapshot();
    assert_eq!(stats.memory_hits, 2);
    assert_eq!(stats.memory_misses, 2);
}

#[tokio::test]
async fn test_cache_stats_writes() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();

    cache.set(CacheKey::new("key1"), "value1".to_string()).await;
    cache.set(CacheKey::new("key2"), "value2".to_string()).await;

    let stats = cache.stats().snapshot();
    assert_eq!(stats.writes, 2);
}

#[tokio::test]
async fn test_cache_stats_deletions() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();
    let key = CacheKey::new("to-delete");

    cache.set(key.clone(), "value".to_string()).await;
    cache.delete(&key).await;

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

#[tokio::test]
async fn test_cache_concurrent_read_write() {
    use std::sync::Arc;

    let cache = Arc::new(InMemoryCache::<i32>::with_defaults());
    let mut handles = vec![];

    for i in 0..4 {
        let cache_clone = Arc::clone(&cache);
        let handle = tokio::spawn(async move {
            for j in 0..25 {
                let key = CacheKey::new(format!("key-{}-{}", i, j));
                cache_clone.set(key, i * 25 + j).await;
            }
        });
        handles.push(handle);
    }

    for _ in 0..4 {
        let cache_clone = Arc::clone(&cache);
        let handle = tokio::spawn(async move {
            for i in 0..4 {
                for j in 0..25 {
                    let key = CacheKey::new(format!("key-{}-{}", i, j));
                    let _ = cache_clone.get(&key).await;
                }
            }
        });
        handles.push(handle);
    }

    for handle in handles {
        match handle.await {
            Ok(()) => {}
            Err(error) => panic!("cache worker failed: {error}"),
        }
    }

    assert!(cache.len() <= 100);
}

#[tokio::test]
async fn test_cache_get_entry() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();
    let key = CacheKey::new("entry-key");

    cache
        .set_with_size(
            key.clone(),
            "value".to_string(),
            Duration::from_secs(60),
            100,
        )
        .await;

    let entry = cache.get_entry(&key).await;
    assert!(entry.is_some());

    let Some(entry) = entry else {
        panic!("expected cache entry");
    };
    assert_eq!(entry.value, "value");
    assert_eq!(entry.size_bytes, 100);
    assert_eq!(entry.access_count, 1);
}

#[tokio::test]
async fn test_cache_get_entry_includes_prior_get_accesses() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();
    let key = CacheKey::new("entry-access-key");

    cache.set(key.clone(), "value".to_string()).await;
    cache.get(&key).await;
    cache.get(&key).await;

    let Some(entry) = cache.get_entry(&key).await else {
        panic!("expected cache entry");
    };
    assert_eq!(entry.access_count, 3);
}

#[tokio::test]
async fn test_cache_keys() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();

    cache.set(CacheKey::new("key1"), "value1".to_string()).await;
    cache.set(CacheKey::new("key2"), "value2".to_string()).await;
    cache.set(CacheKey::new("key3"), "value3".to_string()).await;

    let keys = cache.keys();
    assert_eq!(keys.len(), 3);
}

#[tokio::test]
async fn test_cache_update_existing() {
    let cache: InMemoryCache<String> = InMemoryCache::with_defaults();
    let key = CacheKey::new("update-key");

    cache.set(key.clone(), "initial".to_string()).await;
    cache.set(key.clone(), "updated".to_string()).await;

    let result = cache.get(&key).await;
    assert_eq!(result, Some("updated".to_string()));
    assert_eq!(cache.len(), 1);
}

fn assert_eviction_metadata_matches_live_keys<T: Clone + Send + Sync + 'static>(
    cache: &InMemoryCache<T>,
) {
    use std::collections::HashSet;

    let live: HashSet<_> = cache.keys().into_iter().collect();
    let mut metadata = HashSet::new();
    let mut candidates = HashSet::new();
    for shard in cache.access_meta.iter() {
        for entry in shard.iter() {
            assert!(metadata.insert(entry.key().clone()));
        }
    }
    for queue in cache.access_queue.iter() {
        for (key, _) in lock_queue(queue).iter() {
            assert!(candidates.insert(key.clone()), "duplicate candidate key");
        }
    }
    assert_eq!(metadata, live);
    assert_eq!(candidates, live);
}

#[tokio::test]
async fn test_cache_replacement_and_delete_churn_release_eviction_metadata() {
    let cache = InMemoryCache::new(DualCacheConfig::default().with_max_size(16));
    let retained = CacheKey::new("retained");
    cache.set(retained.clone(), 0).await;
    for round in 0..1_000 {
        cache.set(retained.clone(), round).await;
        assert_eq!(cache.get_entry(&retained).await.unwrap().access_count, 1);
        let transient = CacheKey::new(format!("transient-{round}"));
        cache.set(transient.clone(), round).await;
        cache.get(&transient).await;
        if round % 2 == 0 {
            assert!(cache.delete(&transient).await);
        } else {
            assert_eq!(cache.delete_if(&transient, |_| true).await, Some(round));
        }
        assert_eviction_metadata_matches_live_keys(&cache);
    }
    assert_eq!(cache.len(), 1);
    cache.clear().await;
    assert_eviction_metadata_matches_live_keys(&cache);
    cache.set(retained, 1).await;
    assert_eviction_metadata_matches_live_keys(&cache);
}

#[tokio::test]
async fn test_cache_expiration_releases_eviction_metadata() {
    let cache = InMemoryCache::new(DualCacheConfig::default().with_max_size(16));
    for round in 0..100 {
        for operation in 0..6 {
            let key = CacheKey::new(format!("expired-{round}-{operation}"));
            cache.set_with_ttl(key.clone(), 1, Duration::ZERO).await;
            match operation {
                0 => assert!(cache.get(&key).await.is_none()),
                1 => assert!(cache.peek(&key).await.is_none()),
                2 => assert!(cache.get_entry(&key).await.is_none()),
                3 => assert!(cache.peek_entry(&key).await.is_none()),
                4 => assert!(!cache.exists(&key).await),
                _ => cache.cleanup_expired().await,
            }
            assert!(cache.is_empty());
            assert_eviction_metadata_matches_live_keys(&cache);
        }
    }
}

#[tokio::test]
async fn test_cache_delayed_read_and_insert_bookkeeping_do_not_recreate_deleted_key() {
    let cache = InMemoryCache::with_defaults();
    let key = CacheKey::new("deleted-before-bookkeeping");
    cache.set(key.clone(), 1).await;
    let _sampled = cache.sample_eviction_keys(cache.access_shard_index(&key), 1);
    assert!(cache.delete(&key).await);

    // Model a read that cloned its value before deletion, and a writer whose
    // bookkeeping was delayed until after its value was removed.
    assert_eq!(cache.record_access(&key), 0);
    cache.reset_access_meta_for_insert(&key);
    assert_eviction_metadata_matches_live_keys(&cache);
    assert!(cache.eviction_candidates().is_empty());
}

#[tokio::test]
async fn test_cache_sampling_rotates_unique_keys_without_metadata_growth() {
    use std::collections::HashSet;

    let cache = InMemoryCache::new(DualCacheConfig::default().with_max_size(512));
    let mut keys = Vec::new();
    let shard_index = 0;
    for number in 0.. {
        let key = CacheKey::new(format!("same-shard-{number}"));
        if cache.access_shard_index(&key) == shard_index {
            cache.set(key.clone(), number).await;
            keys.push(key);
            if keys.len() == EVICTION_SAMPLE_SIZE * 2 {
                break;
            }
        }
    }
    let mut visited = HashSet::new();
    for _ in 0..2 {
        let sampled = cache.sample_eviction_keys(shard_index, EVICTION_SAMPLE_SIZE);
        assert_eq!(sampled.len(), EVICTION_SAMPLE_SIZE);
        assert_eq!(sampled.iter().collect::<HashSet<_>>().len(), sampled.len());
        visited.extend(sampled);
    }
    assert_eq!(visited, keys.into_iter().collect());
    for _ in 0..100 {
        cache.eviction_candidates();
        cache.expired_eviction_candidate();
        assert_eviction_metadata_matches_live_keys(&cache);
    }
}

#[tokio::test]
async fn test_cache_fifo_ignores_hits_and_ttl_keeps_shortest_expiry_policy() {
    for policy in [EvictionPolicy::FIFO, EvictionPolicy::TTL] {
        let cache = InMemoryCache::new(
            DualCacheConfig::default()
                .with_max_size(2)
                .with_eviction_policy(policy),
        );
        let first = CacheKey::new("first");
        let second = CacheKey::new("second");
        cache
            .set_with_ttl(first.clone(), 1, Duration::from_secs(300))
            .await;
        cache
            .set_with_ttl(second.clone(), 2, Duration::from_secs(60))
            .await;
        for _ in 0..100 {
            cache.peek(&first).await;
            cache.peek_entry(&second).await;
        }
        cache.set(CacheKey::new("third"), 3).await;
        match policy {
            EvictionPolicy::FIFO => {
                assert!(!cache.exists(&first).await);
                assert!(cache.exists(&second).await);
            }
            EvictionPolicy::TTL => {
                assert!(cache.exists(&first).await);
                assert!(!cache.exists(&second).await);
            }
            _ => unreachable!(),
        }
        assert_eviction_metadata_matches_live_keys(&cache);
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_cache_concurrent_hot_reads_and_churn_keep_metadata_bounded() {
    let cache = Arc::new(InMemoryCache::new(
        DualCacheConfig::default().with_max_size(32),
    ));
    let hot = CacheKey::new("concurrent-hot");
    cache.set(hot.clone(), 1).await;
    let mut workers = Vec::new();
    for worker in 0..8 {
        let cache = Arc::clone(&cache);
        let hot = hot.clone();
        workers.push(tokio::spawn(async move {
            for round in 0..500 {
                assert_eq!(cache.get(&hot).await, Some(1));
                assert_eq!(cache.peek(&hot).await, Some(1));
                let key = CacheKey::new(format!("worker-{worker}-{round}"));
                cache.set(key.clone(), round).await;
                cache.get_entry(&key).await;
                cache.set(key.clone(), round + 1).await;
                cache.peek_entry(&key).await;
                assert!(cache.delete(&key).await);
                // Same-key replacement/delete races, including a delayed read.
                let shared = CacheKey::new("shared-churn");
                cache.set(shared.clone(), round).await;
                cache.get(&shared).await;
                cache.delete_if(&shared, |_| true).await;
                if round % 10 == 0 {
                    cache.eviction_candidates();
                    tokio::task::yield_now().await;
                }
            }
        }));
    }
    for worker in workers {
        worker.await.expect("cache worker should complete");
    }
    cache.delete(&CacheKey::new("shared-churn")).await;
    assert_eq!(cache.len(), 1);
    assert_eviction_metadata_matches_live_keys(&cache);
    assert_eq!(cache.get_entry(&hot).await.unwrap().access_count, 8_001);
}

#[tokio::test]
async fn test_cache_hot_reads_keep_eviction_metadata_bounded() {
    const KEY_COUNT: usize = 8;
    const ROUNDS: usize = 2_000;
    let cache = InMemoryCache::new(DualCacheConfig::default().with_max_size(64));
    let keys: Vec<_> = (0..KEY_COUNT)
        .map(|index| CacheKey::new(format!("hot-{index}")))
        .collect();

    for (index, key) in keys.iter().enumerate() {
        cache.set(key.clone(), index).await;
    }
    for round in 0..ROUNDS {
        for (index, key) in keys.iter().enumerate() {
            assert_eq!(cache.get(key).await, Some(index));
            assert_eq!(cache.peek(key).await, Some(index));
            assert_eq!(cache.get_entry(key).await.unwrap().value, index);
            let entry = cache.peek_entry(key).await.unwrap();
            assert_eq!(entry.value, index);
            assert_eq!(entry.access_count, ((round + 1) * 4) as u64);
        }
    }

    assert_eq!(cache.len(), KEY_COUNT);
    let metadata_count: usize = cache.access_meta.iter().map(DashMap::len).sum();
    let queued_count: usize = cache
        .access_queue
        .iter()
        .map(|queue| lock_queue(queue).len())
        .sum();
    assert_eq!(metadata_count, KEY_COUNT);
    assert_eq!(
        queued_count, KEY_COUNT,
        "hits must not accumulate key copies"
    );
}