zentinel-proxy 0.6.11

A security-first reverse proxy built on Pingora with sleepable ops at the edge
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
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
//! Hybrid cache storage backend
//!
//! Combines memory (hot) and disk (cold) tiers. Lookups check memory first,
//! falling back to disk with automatic promotion on hit. Writes go to both
//! tiers so that entries are immediately available in memory and durable on
//! disk.

use async_trait::async_trait;
use bytes::Bytes;
use futures::FutureExt;
use pingora_cache::key::{CacheHashKey, CacheKey, CompactCacheKey};
use pingora_cache::meta::CacheMeta;
use pingora_cache::storage::{
    HandleHit, HandleMiss, HitHandler, MissFinishType, MissHandler, PurgeType, Storage,
};
use pingora_cache::trace::SpanHandle;
use pingora_cache::MemCache;
use pingora_core::Result;
use std::any::Any;
use std::panic::AssertUnwindSafe;
use tracing::{debug, warn};

use crate::disk_cache::DiskCacheStorage;

// ============================================================================
// HybridCacheStorage
// ============================================================================

/// Two-tier cache: memory for hot entries, disk for cold, with automatic
/// promotion on disk hits.
pub struct HybridCacheStorage {
    memory: &'static MemCache,
    disk: &'static DiskCacheStorage,
}

impl HybridCacheStorage {
    pub fn new(memory: &'static MemCache, disk: &'static DiskCacheStorage) -> Self {
        Self { memory, disk }
    }
}

#[async_trait]
impl Storage for HybridCacheStorage {
    async fn lookup(
        &'static self,
        key: &CacheKey,
        trace: &SpanHandle,
    ) -> Result<Option<(CacheMeta, HitHandler)>> {
        // Fast path: check memory first
        if let Some(hit) = self.memory.lookup(key, trace).await? {
            debug!("hybrid cache: memory hit");
            return Ok(Some(hit));
        }

        // Slow path: check disk
        let (meta, mut disk_hit) = match self.disk.lookup(key, trace).await? {
            Some(hit) => hit,
            None => return Ok(None),
        };
        debug!("hybrid cache: disk hit, promoting to memory");

        // Read the full body from the disk hit handler
        let mut body_parts: Vec<Bytes> = Vec::new();
        while let Some(chunk) = disk_hit.read_body().await? {
            body_parts.push(chunk);
        }
        let full_body: Bytes = if body_parts.len() == 1 {
            body_parts.into_iter().next().unwrap()
        } else {
            let total: usize = body_parts.iter().map(|b| b.len()).sum();
            let mut buf = Vec::with_capacity(total);
            for part in &body_parts {
                buf.extend_from_slice(part);
            }
            Bytes::from(buf)
        };

        // Serialize meta before spawning (CacheMeta is not Send-safe to move
        // across spawn boundaries without serialization)
        let serialized_meta = meta.serialize()?;
        let promote_body = full_body.clone();
        let key_clone = key.clone();

        // Spawn background promotion into memory tier
        let mem = self.memory;
        tokio::spawn(async move {
            let promote_meta = match CacheMeta::deserialize(&serialized_meta.0, &serialized_meta.1)
            {
                Ok(m) => m,
                Err(e) => {
                    warn!(error = %e, "hybrid cache: failed to deserialize meta for promotion");
                    return;
                }
            };
            let inactive_span = pingora_cache::trace::Span::inactive().handle();
            match mem
                .get_miss_handler(&key_clone, &promote_meta, &inactive_span)
                .await
            {
                Ok(mut handler) => {
                    if let Err(e) = handler.write_body(promote_body, true).await {
                        warn!(error = %e, "hybrid cache: promotion write_body failed");
                        return;
                    }
                    if let Err(e) = handler.finish().await {
                        warn!(error = %e, "hybrid cache: promotion finish failed");
                    }
                }
                Err(e) => {
                    warn!(error = %e, "hybrid cache: promotion get_miss_handler failed");
                }
            }
        });

        // Return a hit handler wrapping the already-read body
        let handler = HybridHitHandler::new(full_body);
        Ok(Some((meta, Box::new(handler))))
    }

    async fn get_miss_handler(
        &'static self,
        key: &CacheKey,
        meta: &CacheMeta,
        trace: &SpanHandle,
    ) -> Result<MissHandler> {
        let mem_handler = self.memory.get_miss_handler(key, meta, trace).await?;
        let disk_handler = self.disk.get_miss_handler(key, meta, trace).await?;

        Ok(Box::new(HybridMissHandler {
            mem_handler: Some(mem_handler),
            disk_handler: Some(disk_handler),
            finished: false,
        }))
    }

    async fn purge(
        &'static self,
        key: &CompactCacheKey,
        purge_type: PurgeType,
        trace: &SpanHandle,
    ) -> Result<bool> {
        match purge_type {
            PurgeType::Eviction => {
                // Capacity demotion: remove from memory only, disk copy stays.
                debug!("hybrid cache: eviction demotion, keeping disk copy");
                self.memory.purge(key, purge_type, trace).await
            }
            PurgeType::Invalidation => {
                let mem = self.memory.purge(key, purge_type, trace).await?;
                let disk = self.disk.purge(key, purge_type, trace).await?;
                Ok(mem || disk)
            }
        }
    }

    async fn update_meta(
        &'static self,
        key: &CacheKey,
        meta: &CacheMeta,
        trace: &SpanHandle,
    ) -> Result<bool> {
        // MemCache::update_meta panics if the key is not in its cache map.
        // The entry may only exist on disk, so we catch the panic.
        let mem_updated = match AssertUnwindSafe(self.memory.update_meta(key, meta, trace))
            .catch_unwind()
            .await
        {
            Ok(Ok(v)) => v,
            Ok(Err(e)) => {
                warn!(error = %e, "hybrid cache: memory update_meta error");
                false
            }
            Err(_) => {
                debug!("hybrid cache: key not in memory tier, skipping memory update_meta");
                false
            }
        };

        let disk_updated = self.disk.update_meta(key, meta, trace).await?;
        Ok(mem_updated || disk_updated)
    }

    fn support_streaming_partial_write(&self) -> bool {
        // Delegate to memory tier which supports streaming partial writes
        self.memory.support_streaming_partial_write()
    }

    fn as_any(&self) -> &(dyn Any + Send + Sync + 'static) {
        self
    }
}

// ============================================================================
// HybridHitHandler — wraps an already-read body from a disk-promoted hit
// ============================================================================

pub struct HybridHitHandler {
    body: Bytes,
    done: bool,
    range_start: usize,
    range_end: usize,
}

impl HybridHitHandler {
    fn new(body: Bytes) -> Self {
        let len = body.len();
        Self {
            body,
            done: false,
            range_start: 0,
            range_end: len,
        }
    }
}

#[async_trait]
impl HandleHit for HybridHitHandler {
    async fn read_body(&mut self) -> Result<Option<Bytes>> {
        if self.done {
            return Ok(None);
        }
        self.done = true;
        Ok(Some(self.body.slice(self.range_start..self.range_end)))
    }

    async fn finish(
        self: Box<Self>,
        _storage: &'static (dyn Storage + Sync),
        _key: &CacheKey,
        _trace: &SpanHandle,
    ) -> Result<()> {
        Ok(())
    }

    fn can_seek(&self) -> bool {
        true
    }

    fn seek(&mut self, start: usize, end: Option<usize>) -> Result<()> {
        if start >= self.body.len() {
            return pingora_core::Error::e_explain(
                pingora_core::ErrorType::InternalError,
                format!("seek start out of range {} >= {}", start, self.body.len()),
            );
        }
        self.range_start = start;
        if let Some(end) = end {
            self.range_end = std::cmp::min(self.body.len(), end);
        }
        self.done = false;
        Ok(())
    }

    fn get_eviction_weight(&self) -> usize {
        self.body.len()
    }

    fn as_any(&self) -> &(dyn Any + Send + Sync) {
        self
    }

    fn as_any_mut(&mut self) -> &mut (dyn Any + Send + Sync) {
        self
    }
}

// ============================================================================
// HybridMissHandler — writes to both tiers
// ============================================================================

struct HybridMissHandler {
    mem_handler: Option<MissHandler>,
    disk_handler: Option<MissHandler>,
    finished: bool,
}

#[async_trait]
impl HandleMiss for HybridMissHandler {
    async fn write_body(&mut self, data: Bytes, eof: bool) -> Result<()> {
        // Bytes::clone is a cheap Arc ref-count bump
        if let Some(ref mut mem) = self.mem_handler {
            mem.write_body(data.clone(), eof).await?;
        }
        if let Some(ref mut disk) = self.disk_handler {
            disk.write_body(data, eof).await?;
        }
        Ok(())
    }

    async fn finish(mut self: Box<Self>) -> Result<MissFinishType> {
        self.finished = true;

        // Finish memory first for immediate availability
        let mem_size = if let Some(mem) = self.mem_handler.take() {
            match mem.finish().await {
                Ok(MissFinishType::Created(s)) => s,
                Ok(MissFinishType::Appended(s, _)) => s,
                Err(e) => {
                    warn!(error = %e, "hybrid cache: memory finish failed");
                    0
                }
            }
        } else {
            0
        };

        // Finish disk for durability; failure is non-fatal
        if let Some(disk) = self.disk_handler.take() {
            if let Err(e) = disk.finish().await {
                warn!(error = %e, "hybrid cache: disk finish failed (non-fatal)");
            }
        }

        Ok(MissFinishType::Created(mem_size))
    }

    fn streaming_write_tag(&self) -> Option<&[u8]> {
        // Delegate to memory handler for streaming partial write support
        self.mem_handler
            .as_ref()
            .and_then(|h| h.streaming_write_tag())
    }
}

impl Drop for HybridMissHandler {
    fn drop(&mut self) {
        // Inner handlers clean up their own state via their Drop impls
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use once_cell::sync::Lazy;
    use pingora_cache::trace::Span;
    use pingora_http::ResponseHeader;
    use std::path::Path;
    use std::time::SystemTime;

    fn create_test_meta() -> CacheMeta {
        let mut header = ResponseHeader::build(200, None).unwrap();
        header.append_header("content-type", "text/plain").unwrap();
        header.append_header("x-test", "hybrid").unwrap();
        CacheMeta::new(
            SystemTime::now() + std::time::Duration::from_secs(3600),
            SystemTime::now(),
            60,
            300,
            header,
        )
    }

    fn span() -> SpanHandle {
        Span::inactive().handle()
    }

    fn test_disk(name: &str) -> DiskCacheStorage {
        let path = std::env::temp_dir().join(format!("zentinel-hybrid-test-{}", name));
        let _ = std::fs::remove_dir_all(&path);
        DiskCacheStorage::new(&path, 2, 50 * 1024 * 1024)
    }

    fn cleanup_disk(name: &str) {
        let path = std::env::temp_dir().join(format!("zentinel-hybrid-test-{}", name));
        let _ = std::fs::remove_dir_all(&path);
    }

    // ---------- test 1: miss then hit ----------

    static HYBRID_1_MEM: Lazy<MemCache> = Lazy::new(MemCache::new);
    static HYBRID_1_DISK: Lazy<DiskCacheStorage> = Lazy::new(|| test_disk("miss-then-hit"));
    static HYBRID_1: Lazy<HybridCacheStorage> =
        Lazy::new(|| HybridCacheStorage::new(&HYBRID_1_MEM, &HYBRID_1_DISK));

    #[tokio::test]
    async fn test_hybrid_miss_then_hit() {
        let trace = &span();
        let key = CacheKey::new("", "hybrid-miss-hit", "1");
        let meta = create_test_meta();

        // Lookup should miss
        assert!(HYBRID_1.lookup(&key, trace).await.unwrap().is_none());

        // Write via miss handler
        let mut handler = HYBRID_1.get_miss_handler(&key, &meta, trace).await.unwrap();
        handler
            .write_body(Bytes::from_static(b"hello hybrid"), true)
            .await
            .unwrap();
        handler.finish().await.unwrap();

        // Lookup should hit
        let (read_meta, mut hit) = HYBRID_1.lookup(&key, trace).await.unwrap().unwrap();
        assert_eq!(read_meta.response_header().status.as_u16(), 200);
        let body = hit.read_body().await.unwrap().unwrap();
        assert_eq!(body.as_ref(), b"hello hybrid");

        // Second read should return None
        assert!(hit.read_body().await.unwrap().is_none());

        cleanup_disk("miss-then-hit");
    }

    // ---------- test 2: disk promotion ----------

    static HYBRID_2_MEM: Lazy<MemCache> = Lazy::new(MemCache::new);
    static HYBRID_2_DISK: Lazy<DiskCacheStorage> = Lazy::new(|| test_disk("disk-promotion"));
    static HYBRID_2: Lazy<HybridCacheStorage> =
        Lazy::new(|| HybridCacheStorage::new(&HYBRID_2_MEM, &HYBRID_2_DISK));

    #[tokio::test]
    async fn test_hybrid_disk_promotion() {
        let trace = &span();
        let key = CacheKey::new("", "hybrid-promote", "1");
        let meta = create_test_meta();

        // Write directly to disk tier only
        let mut disk_handler = HYBRID_2_DISK
            .get_miss_handler(&key, &meta, trace)
            .await
            .unwrap();
        disk_handler
            .write_body(Bytes::from_static(b"cold data"), true)
            .await
            .unwrap();
        disk_handler.finish().await.unwrap();

        // Memory should have nothing
        assert!(HYBRID_2_MEM.lookup(&key, trace).await.unwrap().is_none());

        // Hybrid lookup triggers disk hit + promotion
        let (_meta, mut hit) = HYBRID_2.lookup(&key, trace).await.unwrap().unwrap();
        let body = hit.read_body().await.unwrap().unwrap();
        assert_eq!(body.as_ref(), b"cold data");

        // Give the background promotion task time to complete
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;

        // Now memory should have the entry
        let mem_result = HYBRID_2_MEM.lookup(&key, trace).await.unwrap();
        assert!(mem_result.is_some(), "entry should be promoted to memory");

        cleanup_disk("disk-promotion");
    }

    // ---------- test 3: purge both tiers ----------

    static HYBRID_3_MEM: Lazy<MemCache> = Lazy::new(MemCache::new);
    static HYBRID_3_DISK: Lazy<DiskCacheStorage> = Lazy::new(|| test_disk("purge-both"));
    static HYBRID_3: Lazy<HybridCacheStorage> =
        Lazy::new(|| HybridCacheStorage::new(&HYBRID_3_MEM, &HYBRID_3_DISK));

    #[tokio::test]
    async fn test_hybrid_purge_both_tiers() {
        let trace = &span();
        let key = CacheKey::new("", "hybrid-purge", "1");
        let meta = create_test_meta();

        // Write via hybrid (goes to both tiers)
        let mut handler = HYBRID_3.get_miss_handler(&key, &meta, trace).await.unwrap();
        handler
            .write_body(Bytes::from_static(b"purge me"), true)
            .await
            .unwrap();
        handler.finish().await.unwrap();

        // Verify both tiers have it
        assert!(HYBRID_3_MEM.lookup(&key, trace).await.unwrap().is_some());
        assert!(HYBRID_3_DISK.lookup(&key, trace).await.unwrap().is_some());

        // Purge
        let compact = key.to_compact();
        let purged = HYBRID_3
            .purge(&compact, PurgeType::Invalidation, trace)
            .await
            .unwrap();
        assert!(purged);

        // Both tiers should be empty
        assert!(HYBRID_3_MEM.lookup(&key, trace).await.unwrap().is_none());
        assert!(HYBRID_3_DISK.lookup(&key, trace).await.unwrap().is_none());

        cleanup_disk("purge-both");
    }

    // ---------- test 4: update meta ----------

    static HYBRID_4_MEM: Lazy<MemCache> = Lazy::new(MemCache::new);
    static HYBRID_4_DISK: Lazy<DiskCacheStorage> = Lazy::new(|| test_disk("update-meta"));
    static HYBRID_4: Lazy<HybridCacheStorage> =
        Lazy::new(|| HybridCacheStorage::new(&HYBRID_4_MEM, &HYBRID_4_DISK));

    #[tokio::test]
    async fn test_hybrid_update_meta() {
        let trace = &span();
        let key = CacheKey::new("", "hybrid-update-meta", "1");
        let meta = create_test_meta();

        // Write entry
        let mut handler = HYBRID_4.get_miss_handler(&key, &meta, trace).await.unwrap();
        handler
            .write_body(Bytes::from_static(b"update me"), true)
            .await
            .unwrap();
        handler.finish().await.unwrap();

        // Create updated meta with different header
        let mut new_header = ResponseHeader::build(200, None).unwrap();
        new_header
            .append_header("content-type", "application/json")
            .unwrap();
        new_header.append_header("x-updated", "true").unwrap();
        let new_meta = CacheMeta::new(
            SystemTime::now() + std::time::Duration::from_secs(7200),
            SystemTime::now(),
            120,
            600,
            new_header,
        );

        // Update meta via hybrid
        let updated = HYBRID_4.update_meta(&key, &new_meta, trace).await.unwrap();
        assert!(updated);

        // Verify lookup returns updated headers
        let (read_meta, _hit) = HYBRID_4.lookup(&key, trace).await.unwrap().unwrap();
        let headers = read_meta.response_header().headers.clone();
        assert_eq!(headers.get("x-updated").unwrap().to_str().unwrap(), "true");

        cleanup_disk("update-meta");
    }

    // ---------- test 5: miss handler drop ----------

    static HYBRID_5_MEM: Lazy<MemCache> = Lazy::new(MemCache::new);
    static HYBRID_5_DISK: Lazy<DiskCacheStorage> = Lazy::new(|| test_disk("miss-drop"));
    static HYBRID_5: Lazy<HybridCacheStorage> =
        Lazy::new(|| HybridCacheStorage::new(&HYBRID_5_MEM, &HYBRID_5_DISK));

    #[tokio::test]
    async fn test_hybrid_miss_handler_drop() {
        let trace = &span();
        let key = CacheKey::new("", "hybrid-miss-drop", "1");
        let meta = create_test_meta();

        // Create miss handler, write data, drop without finish
        {
            let mut handler = HYBRID_5.get_miss_handler(&key, &meta, trace).await.unwrap();
            handler
                .write_body(Bytes::from_static(b"incomplete"), false)
                .await
                .unwrap();
            // Drop without calling finish
        }

        // Neither tier should have the entry
        assert!(HYBRID_5_MEM.lookup(&key, trace).await.unwrap().is_none());
        assert!(HYBRID_5_DISK.lookup(&key, trace).await.unwrap().is_none());

        cleanup_disk("miss-drop");
    }

    // ---------- test 6: chunked write ----------

    static HYBRID_6_MEM: Lazy<MemCache> = Lazy::new(MemCache::new);
    static HYBRID_6_DISK: Lazy<DiskCacheStorage> = Lazy::new(|| test_disk("chunked-write"));
    static HYBRID_6: Lazy<HybridCacheStorage> =
        Lazy::new(|| HybridCacheStorage::new(&HYBRID_6_MEM, &HYBRID_6_DISK));

    #[tokio::test]
    async fn test_hybrid_chunked_write() {
        let trace = &span();
        let key = CacheKey::new("", "hybrid-chunked", "1");
        let meta = create_test_meta();

        let mut handler = HYBRID_6.get_miss_handler(&key, &meta, trace).await.unwrap();
        handler
            .write_body(Bytes::from_static(b"chunk1-"), false)
            .await
            .unwrap();
        handler
            .write_body(Bytes::from_static(b"chunk2-"), false)
            .await
            .unwrap();
        handler
            .write_body(Bytes::from_static(b"chunk3"), true)
            .await
            .unwrap();
        handler.finish().await.unwrap();

        let (_meta, mut hit) = HYBRID_6.lookup(&key, trace).await.unwrap().unwrap();
        let body = hit.read_body().await.unwrap().unwrap();
        assert_eq!(body.as_ref(), b"chunk1-chunk2-chunk3");

        cleanup_disk("chunked-write");
    }

    // ---------- test 7: seek ----------

    static HYBRID_7_MEM: Lazy<MemCache> = Lazy::new(MemCache::new);
    static HYBRID_7_DISK: Lazy<DiskCacheStorage> = Lazy::new(|| test_disk("seek"));
    static HYBRID_7: Lazy<HybridCacheStorage> =
        Lazy::new(|| HybridCacheStorage::new(&HYBRID_7_MEM, &HYBRID_7_DISK));

    #[tokio::test]
    async fn test_hybrid_seek() {
        let trace = &span();
        let key = CacheKey::new("", "hybrid-seek", "1");
        let meta = create_test_meta();

        // Write directly to disk so lookup returns HybridHitHandler
        let mut disk_handler = HYBRID_7_DISK
            .get_miss_handler(&key, &meta, trace)
            .await
            .unwrap();
        disk_handler
            .write_body(Bytes::from_static(b"0123456789"), true)
            .await
            .unwrap();
        disk_handler.finish().await.unwrap();

        let (_meta, mut hit) = HYBRID_7.lookup(&key, trace).await.unwrap().unwrap();
        assert!(hit.can_seek());

        // Seek to a range
        hit.seek(3, Some(7)).unwrap();
        let body = hit.read_body().await.unwrap().unwrap();
        assert_eq!(body.as_ref(), b"3456");

        // Seek again
        hit.seek(0, Some(3)).unwrap();
        let body = hit.read_body().await.unwrap().unwrap();
        assert_eq!(body.as_ref(), b"012");

        // Out of range should fail
        assert!(hit.seek(100, None).is_err());

        cleanup_disk("seek");
    }

    // ---------- test 8: eviction demotion ----------

    static HYBRID_8_MEM: Lazy<MemCache> = Lazy::new(MemCache::new);
    static HYBRID_8_DISK: Lazy<DiskCacheStorage> = Lazy::new(|| test_disk("eviction-demotion"));
    static HYBRID_8: Lazy<HybridCacheStorage> =
        Lazy::new(|| HybridCacheStorage::new(&HYBRID_8_MEM, &HYBRID_8_DISK));

    #[tokio::test]
    async fn test_hybrid_eviction_demotion() {
        let trace = &span();
        let key = CacheKey::new("", "hybrid-evict-demote", "1");
        let meta = create_test_meta();

        // Write via hybrid (goes to both tiers)
        let mut handler = HYBRID_8.get_miss_handler(&key, &meta, trace).await.unwrap();
        handler
            .write_body(Bytes::from_static(b"demote me"), true)
            .await
            .unwrap();
        handler.finish().await.unwrap();

        // Verify both tiers have it
        assert!(HYBRID_8_MEM.lookup(&key, trace).await.unwrap().is_some());
        assert!(HYBRID_8_DISK.lookup(&key, trace).await.unwrap().is_some());

        // Eviction purge — should only remove from memory
        let compact = key.to_compact();
        let purged = HYBRID_8
            .purge(&compact, PurgeType::Eviction, trace)
            .await
            .unwrap();
        assert!(purged);

        // Memory should be empty, disk should still have it
        assert!(HYBRID_8_MEM.lookup(&key, trace).await.unwrap().is_none());
        assert!(HYBRID_8_DISK.lookup(&key, trace).await.unwrap().is_some());

        cleanup_disk("eviction-demotion");
    }

    // ---------- test 9: eviction then disk hit ----------

    static HYBRID_9_MEM: Lazy<MemCache> = Lazy::new(MemCache::new);
    static HYBRID_9_DISK: Lazy<DiskCacheStorage> = Lazy::new(|| test_disk("evict-then-hit"));
    static HYBRID_9: Lazy<HybridCacheStorage> =
        Lazy::new(|| HybridCacheStorage::new(&HYBRID_9_MEM, &HYBRID_9_DISK));

    #[tokio::test]
    async fn test_hybrid_eviction_then_disk_hit() {
        let trace = &span();
        let key = CacheKey::new("", "hybrid-evict-hit", "1");
        let meta = create_test_meta();

        // Write via hybrid (goes to both tiers)
        let mut handler = HYBRID_9.get_miss_handler(&key, &meta, trace).await.unwrap();
        handler
            .write_body(Bytes::from_static(b"evict and find"), true)
            .await
            .unwrap();
        handler.finish().await.unwrap();

        // Evict from memory
        let compact = key.to_compact();
        HYBRID_9
            .purge(&compact, PurgeType::Eviction, trace)
            .await
            .unwrap();

        // Memory empty
        assert!(HYBRID_9_MEM.lookup(&key, trace).await.unwrap().is_none());

        // Hybrid lookup should find it on disk and promote
        let (_meta, mut hit) = HYBRID_9.lookup(&key, trace).await.unwrap().unwrap();
        let body = hit.read_body().await.unwrap().unwrap();
        assert_eq!(body.as_ref(), b"evict and find");

        // Give background promotion time to complete
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;

        // Memory should now have the entry again
        let mem_result = HYBRID_9_MEM.lookup(&key, trace).await.unwrap();
        assert!(
            mem_result.is_some(),
            "entry should be re-promoted to memory"
        );

        cleanup_disk("evict-then-hit");
    }

    // ---------- test 10: invalidation clears both ----------

    static HYBRID_10_MEM: Lazy<MemCache> = Lazy::new(MemCache::new);
    static HYBRID_10_DISK: Lazy<DiskCacheStorage> = Lazy::new(|| test_disk("invalidation-both"));
    static HYBRID_10: Lazy<HybridCacheStorage> =
        Lazy::new(|| HybridCacheStorage::new(&HYBRID_10_MEM, &HYBRID_10_DISK));

    #[tokio::test]
    async fn test_hybrid_invalidation_clears_both() {
        let trace = &span();
        let key = CacheKey::new("", "hybrid-invalidate", "1");
        let meta = create_test_meta();

        // Write via hybrid (goes to both tiers)
        let mut handler = HYBRID_10
            .get_miss_handler(&key, &meta, trace)
            .await
            .unwrap();
        handler
            .write_body(Bytes::from_static(b"invalidate me"), true)
            .await
            .unwrap();
        handler.finish().await.unwrap();

        // Verify both tiers have it
        assert!(HYBRID_10_MEM.lookup(&key, trace).await.unwrap().is_some());
        assert!(HYBRID_10_DISK.lookup(&key, trace).await.unwrap().is_some());

        // Invalidation purge — should remove from both tiers
        let compact = key.to_compact();
        let purged = HYBRID_10
            .purge(&compact, PurgeType::Invalidation, trace)
            .await
            .unwrap();
        assert!(purged);

        // Both tiers should be empty
        assert!(HYBRID_10_MEM.lookup(&key, trace).await.unwrap().is_none());
        assert!(HYBRID_10_DISK.lookup(&key, trace).await.unwrap().is_none());

        cleanup_disk("invalidation-both");
    }
}