http-cache 1.0.0-alpha.2

An HTTP caching middleware
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
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
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
//! File-based streaming cache manager that stores response metadata and body content separately.
//! This enables streaming by never loading complete response bodies into memory.
//!
//! This implementation is based on the [http-cache-stream](https://github.com/stjude-rust-labs/http-cache-stream) approach.

use crate::{
    body::StreamingBody,
    error::{Result, StreamingError},
    runtime, StreamingCacheManager,
};
use async_trait::async_trait;
use bytes::Bytes;
use http::{Response, Version};
use http_body_util::{BodyExt, Empty};
use http_cache_semantics::CachePolicy;
use log::warn;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use url::Url;

use {
    blake3,
    dashmap::DashMap,
    lru::LruCache,
    std::num::NonZeroUsize,
    std::sync::atomic::{AtomicU64, AtomicUsize, Ordering},
};

use std::collections::HashMap;

// Import async-compatible synchronization primitives based on feature flags
cfg_if::cfg_if! {
    if #[cfg(feature = "streaming-tokio")] {
        use tokio::sync::Mutex;
    } else if #[cfg(feature = "streaming-smol")] {
        use async_lock::Mutex;
    } else {
        use std::sync::Mutex;
    }
}

const CACHE_VERSION: &str = "cache-v2";

/// Configuration for the streaming cache manager
#[derive(Debug, Clone, Copy)]
pub struct StreamingCacheConfig {
    /// Maximum cache size in bytes (None for unlimited)
    pub max_cache_size: Option<u64>,
    /// Maximum number of cache entries (None for unlimited)
    pub max_entries: Option<usize>,
    /// Streaming buffer size in bytes (default: 8192)
    pub streaming_buffer_size: usize,
}

impl Default for StreamingCacheConfig {
    fn default() -> Self {
        Self {
            max_cache_size: None,
            max_entries: None,
            streaming_buffer_size: 8192, // 8KB
        }
    }
}

/// LRU tracking entry for cache management
#[derive(Debug, Clone, Serialize, Deserialize)]
struct LruEntry {
    cache_key: String,
    content_digest: String,
    last_accessed: u64,
    file_size: u64,
}

/// Reference counting for content files to prevent premature deletion
#[derive(Debug)]
struct ContentRefCounter {
    refs: DashMap<String, Arc<AtomicUsize>>,
    lru_cache: Arc<Mutex<LruCache<String, LruEntry>>>,
    current_cache_size: AtomicU64,
    current_entries: AtomicUsize,
}

impl ContentRefCounter {
    fn new() -> Self {
        Self {
            refs: DashMap::new(),
            lru_cache: Arc::new(Mutex::new(LruCache::new(
                NonZeroUsize::new(10000).unwrap(),
            ))),
            current_cache_size: AtomicU64::new(0),
            current_entries: AtomicUsize::new(0),
        }
    }

    /// Get current cache size in bytes
    async fn current_cache_size(&self) -> Result<u64> {
        Ok(self.current_cache_size.load(Ordering::Relaxed))
    }

    /// Get current number of cache entries
    async fn current_entries(&self) -> Result<usize> {
        Ok(self.current_entries.load(Ordering::Relaxed))
    }

    /// Add cache entry to LRU tracking
    async fn add_cache_entry(
        &self,
        cache_key: String,
        content_digest: String,
        file_size: u64,
    ) -> Result<()> {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let entry = LruEntry {
            cache_key: cache_key.clone(),
            content_digest,
            last_accessed: now,
            file_size,
        };

        // Use modern LRU cache with atomic counters
        cfg_if::cfg_if! {
            if #[cfg(any(feature = "streaming-tokio", feature = "streaming-smol"))] {
                let mut lru = self.lru_cache.lock().await;
                lru.put(cache_key, entry);
            } else {
                let mut lru = self.lru_cache.lock().map_err(|e| {
                    StreamingError::concurrency(format!(
                        "Failed to acquire lock for lru_cache: {e}"
                    ))
                })?;
                lru.put(cache_key, entry);
            }
        }

        self.current_cache_size.fetch_add(file_size, Ordering::Relaxed);
        self.current_entries.fetch_add(1, Ordering::Relaxed);

        Ok(())
    }

    /// Update last accessed time for a cache entry (move to front of LRU)
    async fn update_access_time(&self, cache_key: &str) -> Result<()> {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        // With LRU cache, just access the entry to move it to front
        cfg_if::cfg_if! {
            if #[cfg(any(feature = "streaming-tokio", feature = "streaming-smol"))] {
                let mut lru = self.lru_cache.lock().await;
                if let Some(entry) = lru.get_mut(cache_key) {
                    entry.last_accessed = now;
                }
            } else {
                let mut lru = self.lru_cache.lock().map_err(|e| {
                    StreamingError::concurrency(format!(
                        "Failed to acquire lock for lru_cache: {e}"
                    ))
                })?;
                if let Some(entry) = lru.get_mut(cache_key) {
                    entry.last_accessed = now;
                }
            }
        }

        Ok(())
    }

    /// Get least recently used entries for eviction
    async fn get_lru_entries_for_eviction(
        &self,
        target_size: u64,
        target_count: usize,
    ) -> Result<Vec<LruEntry>> {
        let current_size = self.current_cache_size().await?;
        let current_count = self.current_entries().await?;

        // Use LRU cache's built-in iteration
        cfg_if::cfg_if! {
            if #[cfg(any(feature = "streaming-tokio", feature = "streaming-smol"))] {
                let lru = self.lru_cache.lock().await;
                let mut entries_to_evict = Vec::new();
                let mut size_to_free = current_size.saturating_sub(target_size);
                let mut entries_to_free = current_count.saturating_sub(target_count);

                // Iterate from least recently used
                for (_, entry) in lru.iter().rev() {
                    if size_to_free == 0 && entries_to_free == 0 {
                        break;
                    }
                    entries_to_evict.push(entry.clone());
                    if size_to_free > 0 {
                        size_to_free = size_to_free.saturating_sub(entry.file_size);
                    }
                    entries_to_free = entries_to_free.saturating_sub(1);
                }

                Ok(entries_to_evict)
            } else {
                let lru = self.lru_cache.lock().map_err(|e| {
                    StreamingError::concurrency(format!(
                        "Failed to acquire lock for lru_cache: {e}"
                    ))
                })?;
                let mut entries_to_evict = Vec::new();
                let mut size_to_free = current_size.saturating_sub(target_size);
                let mut entries_to_free = current_count.saturating_sub(target_count);

                for (_, entry) in lru.iter().rev() {
                    if size_to_free == 0 && entries_to_free == 0 {
                        break;
                    }
                    entries_to_evict.push(entry.clone());
                    if size_to_free > 0 {
                        size_to_free = size_to_free.saturating_sub(entry.file_size);
                    }
                    entries_to_free = entries_to_free.saturating_sub(1);
                }

                Ok(entries_to_evict)
            }
        }
    }

    /// Remove cache entry from LRU tracking
    async fn remove_cache_entry(
        &self,
        cache_key: &str,
    ) -> Result<Option<LruEntry>> {
        cfg_if::cfg_if! {
            if #[cfg(any(feature = "streaming-tokio", feature = "streaming-smol"))] {
                let mut lru = self.lru_cache.lock().await;
                if let Some(entry) = lru.pop(cache_key) {
                    self.current_cache_size.fetch_sub(entry.file_size, Ordering::Relaxed);
                    self.current_entries.fetch_sub(1, Ordering::Relaxed);
                    return Ok(Some(entry));
                }
            } else {
                let mut lru = self.lru_cache.lock().map_err(|e| {
                    StreamingError::concurrency(format!(
                        "Failed to acquire lock for lru_cache: {e}"
                    ))
                })?;
                if let Some(entry) = lru.pop(cache_key) {
                    self.current_cache_size.fetch_sub(entry.file_size, Ordering::Relaxed);
                    self.current_entries.fetch_sub(1, Ordering::Relaxed);
                    return Ok(Some(entry));
                }
            }
        }

        Ok(None)
    }

    /// Rollback cache entry from LRU tracking using the exact size that was added
    /// This prevents cache size corruption during rollback operations
    async fn rollback_cache_entry(
        &self,
        cache_key: &str,
        exact_file_size: u64,
    ) -> Result<()> {
        cfg_if::cfg_if! {
            if #[cfg(any(feature = "streaming-tokio", feature = "streaming-smol"))] {
                let mut lru = self.lru_cache.lock().await;
                if lru.pop(cache_key).is_some() {
                    self.current_cache_size.fetch_sub(exact_file_size, Ordering::Relaxed);
                    self.current_entries.fetch_sub(1, Ordering::Relaxed);
                }
            } else {
                let mut lru = self.lru_cache.lock().map_err(|e| {
                    StreamingError::concurrency(format!(
                        "Failed to acquire lock for lru_cache: {e}"
                    ))
                })?;
                if lru.pop(cache_key).is_some() {
                    self.current_cache_size.fetch_sub(exact_file_size, Ordering::Relaxed);
                    self.current_entries.fetch_sub(1, Ordering::Relaxed);
                }
            }
        }

        Ok(())
    }

    /// Increment reference count for a content digest
    /// Returns the new reference count
    async fn add_ref(&self, digest: &str) -> Result<usize> {
        let counter = self
            .refs
            .entry(digest.to_string())
            .or_insert_with(|| Arc::new(AtomicUsize::new(0)));
        Ok(counter.fetch_add(1, Ordering::Relaxed) + 1)
    }

    /// Decrement reference count for a content digest, returns true if safe to delete
    async fn remove_ref(&self, digest: &str) -> Result<bool> {
        // Simple approach: use entry API for atomic decrement and removal
        if let Some(entry) = self.refs.get_mut(digest) {
            let current_count = entry.load(Ordering::Relaxed);
            if current_count > 0 {
                entry.fetch_sub(1, Ordering::Relaxed);
                let new_count = entry.load(Ordering::Relaxed);
                if new_count == 0 {
                    drop(entry); // Release the mutable reference
                    self.refs.remove(digest);
                    return Ok(true);
                }
            }
        }
        Ok(false)
    }
}

/// Cache statistics for monitoring
#[derive(Debug, Clone, Copy)]
pub struct CacheStats {
    pub current_size: u64,
    pub current_entries: usize,
    pub max_size: Option<u64>,
    pub max_entries: Option<usize>,
}

/// Report of corrupted cache entry
#[derive(Debug, Clone)]
pub struct CorruptedEntry {
    pub cache_key: String,
    pub digest: String,
    pub reason: String,
}

/// Cache integrity verification report
#[derive(Debug, Clone)]
pub struct CacheIntegrityReport {
    pub total_entries: usize,
    pub valid_entries: usize,
    pub corrupted_entries: Vec<CorruptedEntry>,
    pub orphaned_content: Vec<String>,
}

/// Metadata stored for each cached response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheMetadata {
    pub status: u16,
    pub version: u8,
    pub headers: HashMap<String, String>,
    pub content_digest: String,
    pub policy: CachePolicy,
    pub created_at: u64,
}

/// File-based streaming cache manager
#[derive(Debug)]
pub struct StreamingManager {
    root_path: PathBuf,
    ref_counter: ContentRefCounter,
    config: StreamingCacheConfig,
}

impl Clone for StreamingManager {
    fn clone(&self) -> Self {
        Self {
            root_path: self.root_path.clone(),
            ref_counter: ContentRefCounter {
                refs: self.ref_counter.refs.clone(),
                lru_cache: self.ref_counter.lru_cache.clone(),
                current_cache_size: AtomicU64::new(
                    self.ref_counter.current_cache_size.load(Ordering::Relaxed),
                ),
                current_entries: AtomicUsize::new(
                    self.ref_counter.current_entries.load(Ordering::Relaxed),
                ),
            },
            config: self.config,
        }
    }
}

impl StreamingManager {
    /// Create a new streaming cache manager with default configuration
    pub fn new(root_path: PathBuf) -> Self {
        Self::new_with_config(root_path, StreamingCacheConfig::default())
    }

    /// Create a new streaming cache manager with custom configuration
    pub fn new_with_config(
        root_path: PathBuf,
        config: StreamingCacheConfig,
    ) -> Self {
        Self { root_path, ref_counter: ContentRefCounter::new(), config }
    }

    /// Create a new streaming cache manager and rebuild reference counts from existing cache
    pub async fn new_with_existing_cache(root_path: PathBuf) -> Result<Self> {
        Self::new_with_existing_cache_and_config(
            root_path,
            StreamingCacheConfig::default(),
        )
        .await
    }

    /// Create a new streaming cache manager with config and rebuild reference counts from existing cache
    pub async fn new_with_existing_cache_and_config(
        root_path: PathBuf,
        config: StreamingCacheConfig,
    ) -> Result<Self> {
        let manager = Self::new_with_config(root_path, config);

        // Reference counting is now in-memory only for simplicity

        // Fallback to rebuilding from metadata files if no persistent data or if disabled
        let current_entries = manager.ref_counter.current_entries().await?;
        if current_entries == 0 {
            manager.rebuild_reference_counts().await?;
        }

        Ok(manager)
    }

    /// Verify content integrity by checking if file content matches its digest
    pub async fn verify_content_integrity(
        &self,
        digest: &str,
        content_path: &Path,
    ) -> Result<bool> {
        if !content_path.exists() {
            return Ok(false);
        }

        // Read the content file and compute its digest
        let content =
            runtime::read(content_path).await.map_err(StreamingError::io)?;
        let computed_digest = Self::calculate_digest(&content);

        Ok(computed_digest == digest)
    }

    /// Verify content integrity using streaming for large files to avoid OOM
    async fn verify_content_integrity_streaming(
        &self,
        digest: &str,
        content_path: &Path,
    ) -> Result<bool> {
        if !content_path.exists() {
            return Ok(false);
        }

        // Get file size first
        let file_size = match runtime::metadata(content_path).await {
            Ok(meta) => meta.len(),
            Err(_) => return Ok(false), // File doesn't exist or inaccessible
        };

        // Use streaming verification for large files, buffered for small files
        let computed_digest =
            if file_size > self.config.streaming_buffer_size as u64 {
                self.compute_digest_streaming(content_path).await?
            } else {
                // For small files, use the existing efficient method
                let content = runtime::read(content_path)
                    .await
                    .map_err(StreamingError::io)?;
                Self::calculate_digest(&content)
            };

        Ok(computed_digest == digest)
    }

    /// Compute digest using streaming for large files
    async fn compute_digest_streaming(
        &self,
        file_path: &Path,
    ) -> Result<String> {
        let file =
            runtime::File::open(file_path).await.map_err(StreamingError::io)?;
        let mut hasher = blake3::Hasher::new();
        let mut buffer = vec![0u8; self.config.streaming_buffer_size];

        // Read file in chunks and update hasher
        cfg_if::cfg_if! {
            if #[cfg(feature = "streaming-tokio")] {
                use tokio::io::AsyncReadExt;
                let mut file = file;
                loop {
                    let bytes_read = file.read(&mut buffer).await.map_err(StreamingError::io)?;
                    if bytes_read == 0 {
                        break;
                    }
                    hasher.update(&buffer[..bytes_read]);
                }
            } else if #[cfg(feature = "streaming-smol")] {
                use smol::io::AsyncReadExt;
                let mut file = file;
                loop {
                    let bytes_read = file.read(&mut buffer).await.map_err(StreamingError::io)?;
                    if bytes_read == 0 {
                        break;
                    }
                    hasher.update(&buffer[..bytes_read]);
                }
            }
        }

        Ok(hasher.finalize().to_hex().to_string())
    }

    /// Verify all cached content integrity and return report
    pub async fn verify_cache_integrity(&self) -> Result<CacheIntegrityReport> {
        let mut report = CacheIntegrityReport {
            total_entries: 0,
            valid_entries: 0,
            corrupted_entries: Vec::new(),
            orphaned_content: Vec::new(),
        };

        let metadata_dir = self.root_path.join(CACHE_VERSION).join("metadata");
        let content_dir = self.root_path.join(CACHE_VERSION).join("content");

        if !metadata_dir.exists() {
            return Ok(report);
        }

        // Track all content files to identify orphans
        let mut referenced_digests = std::collections::HashSet::new();

        cfg_if::cfg_if! {
            if #[cfg(feature = "streaming-tokio")] {
                let mut entries = runtime::read_dir(&metadata_dir).await.map_err(StreamingError::io)?;

                while let Some(entry) = entries.next_entry().await.map_err(StreamingError::io)? {
                    let path = entry.path();

                    if path.extension().and_then(|s| s.to_str()) == Some("json") {
                        report.total_entries += 1;

                        let content = runtime::read(&path).await.map_err(StreamingError::io)?;
                        match serde_json::from_slice::<CacheMetadata>(&content) {
                            Ok(metadata) => {
                                referenced_digests.insert(metadata.content_digest.clone());

                                let content_path = self.content_path(&metadata.content_digest);
                                match self.verify_content_integrity_streaming(&metadata.content_digest, &content_path).await {
                                    Ok(true) => report.valid_entries += 1,
                                    Ok(false) => {
                                        let cache_key = path.file_stem()
                                            .and_then(|s| s.to_str())
                                            .and_then(|s| hex::decode(s).ok())
                                            .and_then(|bytes| String::from_utf8(bytes).ok())
                                            .unwrap_or_else(|| format!("unknown-{}", metadata.content_digest));
                                        report.corrupted_entries.push(CorruptedEntry {
                                            cache_key,
                                            digest: metadata.content_digest.clone(),
                                            reason: "Content digest mismatch".to_string(),
                                        });
                                    }
                                    Err(e) => {
                                        let cache_key = path.file_stem()
                                            .and_then(|s| s.to_str())
                                            .and_then(|s| hex::decode(s).ok())
                                            .and_then(|bytes| String::from_utf8(bytes).ok())
                                            .unwrap_or_else(|| format!("unknown-{}", metadata.content_digest));
                                        report.corrupted_entries.push(CorruptedEntry {
                                            cache_key,
                                            digest: metadata.content_digest.clone(),
                                            reason: format!("Verification error: {}", e),
                                        });
                                    }
                                }
                            }
                            Err(e) => {
                                report.corrupted_entries.push(CorruptedEntry {
                                    cache_key: "unknown".to_string(),
                                    digest: "unknown".to_string(),
                                    reason: format!("Invalid metadata: {}", e),
                                });
                            }
                        }
                    }
                }
            } else if #[cfg(feature = "streaming-smol")] {
                use futures::stream::StreamExt;

                let mut entries = runtime::read_dir(&metadata_dir).await.map_err(StreamingError::io)?;

                while let Some(entry_result) = entries.next().await {
                    let entry = entry_result.map_err(StreamingError::io)?;
                    let path = entry.path();

                    if path.extension().and_then(|s| s.to_str()) == Some("json") {
                        report.total_entries += 1;

                        let content = runtime::read(&path).await.map_err(StreamingError::io)?;
                        match serde_json::from_slice::<CacheMetadata>(&content) {
                            Ok(metadata) => {
                                referenced_digests.insert(metadata.content_digest.clone());

                                let content_path = self.content_path(&metadata.content_digest);
                                match self.verify_content_integrity_streaming(&metadata.content_digest, &content_path).await {
                                    Ok(true) => report.valid_entries += 1,
                                    Ok(false) => {
                                        let cache_key = path.file_stem()
                                            .and_then(|s| s.to_str())
                                            .and_then(|s| hex::decode(s).ok())
                                            .and_then(|bytes| String::from_utf8(bytes).ok())
                                            .unwrap_or_else(|| format!("unknown-{}", metadata.content_digest));
                                        report.corrupted_entries.push(CorruptedEntry {
                                            cache_key,
                                            digest: metadata.content_digest.clone(),
                                            reason: "Content digest mismatch".to_string(),
                                        });
                                    }
                                    Err(e) => {
                                        let cache_key = path.file_stem()
                                            .and_then(|s| s.to_str())
                                            .and_then(|s| hex::decode(s).ok())
                                            .and_then(|bytes| String::from_utf8(bytes).ok())
                                            .unwrap_or_else(|| format!("unknown-{}", metadata.content_digest));
                                        report.corrupted_entries.push(CorruptedEntry {
                                            cache_key,
                                            digest: metadata.content_digest.clone(),
                                            reason: format!("Verification error: {}", e),
                                        });
                                    }
                                }
                            }
                            Err(e) => {
                                report.corrupted_entries.push(CorruptedEntry {
                                    cache_key: "unknown".to_string(),
                                    digest: "unknown".to_string(),
                                    reason: format!("Invalid metadata: {}", e),
                                });
                            }
                        }
                    }
                }
            }
        }

        // Check for orphaned content files
        if content_dir.exists() {
            cfg_if::cfg_if! {
                if #[cfg(feature = "streaming-tokio")] {
                    let mut content_entries = runtime::read_dir(&content_dir).await.map_err(StreamingError::io)?;

                    while let Some(entry) = content_entries.next_entry().await.map_err(StreamingError::io)? {
                        let path = entry.path();
                        if let Some(filename) = path.file_name().and_then(|s| s.to_str()) {
                            if !referenced_digests.contains(filename) {
                                report.orphaned_content.push(filename.to_string());
                            }
                        }
                    }
                } else if #[cfg(feature = "streaming-smol")] {
                    use futures::stream::StreamExt;

                    let mut content_entries = runtime::read_dir(&content_dir).await.map_err(StreamingError::io)?;

                    while let Some(entry_result) = content_entries.next().await {
                        let entry = entry_result.map_err(StreamingError::io)?;
                        let path = entry.path();
                        if let Some(filename) = path.file_name().and_then(|s| s.to_str()) {
                            if !referenced_digests.contains(filename) {
                                report.orphaned_content.push(filename.to_string());
                            }
                        }
                    }
                }
            }
        }

        Ok(report)
    }

    /// Remove corrupted cache entries
    pub async fn remove_corrupted_entries(
        &self,
        corrupted_digests: &[String],
    ) -> Result<usize> {
        let mut removed_count = 0;

        for digest in corrupted_digests {
            let content_path = self.content_path(digest);

            // Remove corrupted content file
            if content_path.exists() {
                if let Err(e) = runtime::remove_file(&content_path).await {
                    warn!(
                        "Failed to remove corrupted content file {}: {}",
                        digest, e
                    );
                } else {
                    removed_count += 1;
                }
            }
        }

        Ok(removed_count)
    }

    /// Get cache statistics
    pub async fn cache_stats(&self) -> Result<CacheStats> {
        Ok(CacheStats {
            current_size: self.ref_counter.current_cache_size().await?,
            current_entries: self.ref_counter.current_entries().await?,
            max_size: self.config.max_cache_size,
            max_entries: self.config.max_entries,
        })
    }

    /// Enforce cache size and entry limits by evicting LRU entries
    async fn enforce_cache_limits(&self) -> Result<()> {
        let current_size = self.ref_counter.current_cache_size().await?;
        let current_entries = self.ref_counter.current_entries().await?;

        let target_size = self.config.max_cache_size.unwrap_or(u64::MAX);
        let target_count = self.config.max_entries.unwrap_or(usize::MAX);

        // Check if we need to evict entries
        if current_size <= target_size && current_entries <= target_count {
            return Ok(());
        }

        // Get entries that need to be evicted
        let entries_to_evict = self
            .ref_counter
            .get_lru_entries_for_eviction(target_size, target_count)
            .await?;

        // Evict the LRU entries
        for entry in entries_to_evict {
            if let Err(e) = self.delete(&entry.cache_key).await {
                // Log error but continue with other evictions
                warn!(
                    "Warning: Failed to evict cache entry '{}': {}",
                    entry.cache_key, e
                );
            }
        }

        Ok(())
    }

    /// Get the path for storing metadata
    fn metadata_path(&self, key: &str) -> PathBuf {
        let encoded_key = hex::encode(key.as_bytes());
        self.root_path
            .join(CACHE_VERSION)
            .join("metadata")
            .join(format!("{encoded_key}.json"))
    }

    /// Get the path for storing content
    fn content_path(&self, digest: &str) -> PathBuf {
        self.root_path.join(CACHE_VERSION).join("content").join(digest)
    }

    /// Calculate Blake3 digest of content
    fn calculate_digest(content: &[u8]) -> String {
        blake3::hash(content).to_hex().to_string()
    }

    /// Ensure directory exists
    async fn ensure_dir_exists(path: &Path) -> Result<()> {
        if let Some(parent) = path.parent() {
            runtime::create_dir_all(parent)
                .await
                .map_err(StreamingError::io)?;
        }
        Ok(())
    }

    /// Atomic file write operation to prevent corruption from concurrent writes
    async fn atomic_write(path: &Path, content: &[u8]) -> Result<()> {
        use std::ffi::OsString;

        // Create a temporary file with a unique suffix
        let mut temp_path = path.to_path_buf();
        let mut temp_name = temp_path
            .file_name()
            .map(|n| n.to_os_string())
            .unwrap_or_else(|| OsString::from("temp"));
        temp_name.push(".tmp");

        // Add process ID and timestamp to make it unique
        let pid = std::process::id();
        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos();
        temp_name.push(format!(".{}.{}", pid, timestamp));

        temp_path.set_file_name(&temp_name);

        // Ensure the parent directory exists
        Self::ensure_dir_exists(&temp_path).await?;

        // Write to temporary file first
        runtime::write(&temp_path, content)
            .await
            .map_err(StreamingError::io)?;

        // Atomically rename temporary file to final destination
        if let Err(e) = runtime::rename(&temp_path, path).await {
            // On Windows, rename might fail if destination exists due to file locking
            // Check if the destination file now exists - if so, treat as success since content is identical
            if runtime::metadata(path).await.is_ok() {
                // Content already exists, clean up temp file and succeed
                let _ = runtime::remove_file(&temp_path).await;
                return Ok(());
            }

            // Clean up temporary file on failure (async, best effort)
            let _ = runtime::remove_file(&temp_path).await;
            return Err(StreamingError::io(format!(
                "Failed to atomically write file {:?}: {}",
                path, e
            ))
            .into());
        }

        Ok(())
    }

    /// Build reference counts from existing cache entries
    /// This should be called on manager initialization to rebuild ref counts
    async fn rebuild_reference_counts(&self) -> Result<()> {
        let metadata_dir = self.root_path.join(CACHE_VERSION).join("metadata");

        if !metadata_dir.exists() {
            return Ok(());
        }

        cfg_if::cfg_if! {
            if #[cfg(feature = "streaming-tokio")] {
                let mut entries = runtime::read_dir(&metadata_dir).await.map_err(StreamingError::io)?;

                while let Some(entry) = entries.next_entry().await.map_err(StreamingError::io)? {
                    let path = entry.path();

                    if path.extension().and_then(|s| s.to_str()) == Some("json") {
                        let content = runtime::read(&path).await.map_err(StreamingError::io)?;
                        match serde_json::from_slice::<CacheMetadata>(&content) {
                            Ok(metadata) => {
                                // Add reference to this content digest
                                if let Err(e) = self.ref_counter.add_ref(&metadata.content_digest).await {
                                    return Err(StreamingError::consistency(format!("Failed to rebuild reference count for {}: {}", metadata.content_digest, e)).into());
                                }

                                // Rebuild LRU tracking - get file size
                                let cache_key = path.file_stem()
                                    .and_then(|s| s.to_str())
                                    .and_then(|s| hex::decode(s).ok())
                                    .and_then(|bytes| String::from_utf8(bytes).ok())
                                    .unwrap_or_else(|| format!("unknown-{}", metadata.content_digest));

                                let content_path = self.content_path(&metadata.content_digest);
                                let file_size = if let Ok(meta) = runtime::metadata(&content_path).await {
                                    meta.len()
                                } else {
                                    0
                                };

                                if let Err(e) = self.ref_counter.add_cache_entry(
                                    cache_key,
                                    metadata.content_digest.clone(),
                                    file_size
                                ).await {
                                    return Err(StreamingError::consistency(format!("Failed to rebuild LRU tracking for {}: {}", metadata.content_digest, e)).into());
                                }
                            }
                            Err(e) => {
                                return Err(StreamingError::serialization(format!("Failed to parse metadata file {:?}: {}", path, e)).into());
                            }
                        }
                    }
                }
            } else if #[cfg(feature = "streaming-smol")] {
                use futures::stream::StreamExt;

                let mut entries = runtime::read_dir(&metadata_dir).await.map_err(StreamingError::io)?;

                while let Some(entry_result) = entries.next().await {
                    let entry = entry_result.map_err(StreamingError::io)?;
                    let path = entry.path();

                    if path.extension().and_then(|s| s.to_str()) == Some("json") {
                        let content = runtime::read(&path).await.map_err(StreamingError::io)?;
                        match serde_json::from_slice::<CacheMetadata>(&content) {
                            Ok(metadata) => {
                                // Add reference to this content digest
                                if let Err(e) = self.ref_counter.add_ref(&metadata.content_digest).await {
                                    return Err(StreamingError::consistency(format!("Failed to rebuild reference count for {}: {}", metadata.content_digest, e)).into());
                                }

                                // Rebuild LRU tracking - get file size
                                let cache_key = path.file_stem()
                                    .and_then(|s| s.to_str())
                                    .and_then(|s| hex::decode(s).ok())
                                    .and_then(|bytes| String::from_utf8(bytes).ok())
                                    .unwrap_or_else(|| format!("unknown-{}", metadata.content_digest));

                                let content_path = self.content_path(&metadata.content_digest);
                                let file_size = if let Ok(meta) = runtime::metadata(&content_path).await {
                                    meta.len()
                                } else {
                                    0
                                };

                                if let Err(e) = self.ref_counter.add_cache_entry(
                                    cache_key,
                                    metadata.content_digest.clone(),
                                    file_size
                                ).await {
                                    return Err(StreamingError::consistency(format!("Failed to rebuild LRU tracking for {}: {}", metadata.content_digest, e)).into());
                                }
                            }
                            Err(e) => {
                                return Err(StreamingError::serialization(format!("Failed to parse metadata file {:?}: {}", path, e)).into());
                            }
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Process body efficiently using existing http-body-util libraries.
    /// Uses buffered collection optimized with configurable buffer size.
    /// Returns (digest, body_bytes, file_size) where body_bytes is for the response.
    async fn process_body_streaming<B>(
        &self,
        body: B,
    ) -> Result<(String, Bytes, u64)>
    where
        B: http_body::Body + Send + 'static,
        B::Data: Send,
        B::Error: Into<StreamingError>,
    {
        use http_body_util::BodyExt;

        // Use http-body-util's optimized collection with size hints
        let collected =
            body.collect().await.map_err(|e| StreamingError::new(e.into()))?;
        let body_bytes = collected.to_bytes();

        // Calculate content digest efficiently
        let content_digest = Self::calculate_digest(&body_bytes);
        let file_size = body_bytes.len() as u64;

        Ok((content_digest, body_bytes, file_size))
    }
}

#[async_trait]
impl StreamingCacheManager for StreamingManager {
    type Body = StreamingBody<Empty<Bytes>>;

    async fn get(
        &self,
        cache_key: &str,
    ) -> Result<Option<(Response<Self::Body>, CachePolicy)>>
    where
        <Self::Body as http_body::Body>::Data: Send,
        <Self::Body as http_body::Body>::Error:
            Into<StreamingError> + Send + Sync + 'static,
    {
        let metadata_path = self.metadata_path(cache_key);

        // Check if metadata file exists
        if !metadata_path.exists() {
            return Ok(None);
        }

        // Update LRU access time
        let _ = self.ref_counter.update_access_time(cache_key).await;

        // Read and parse metadata
        let metadata_content =
            runtime::read(&metadata_path).await.map_err(StreamingError::io)?;
        let metadata: CacheMetadata = serde_json::from_slice(&metadata_content)
            .map_err(StreamingError::serialization)?;

        // Check if content file exists
        let content_path = self.content_path(&metadata.content_digest);
        // Open content file for streaming (will fail if file doesn't exist)
        let file = match runtime::File::open(&content_path).await {
            Ok(file) => file,
            Err(_) => return Ok(None), // File doesn't exist
        };

        // Build response with streaming body
        let mut response_builder = Response::builder()
            .status(metadata.status)
            .version(match metadata.version {
                9 => Version::HTTP_09,
                10 => Version::HTTP_10,
                11 => Version::HTTP_11,
                2 => Version::HTTP_2,
                3 => Version::HTTP_3,
                _ => Version::HTTP_11,
            });

        // Add headers
        for (name, value) in &metadata.headers {
            if let (Ok(header_name), Ok(header_value)) = (
                name.parse::<http::HeaderName>(),
                value.parse::<http::HeaderValue>(),
            ) {
                response_builder =
                    response_builder.header(header_name, header_value);
            }
        }

        // Create streaming body from file
        let body = StreamingBody::from_file(file);
        let response =
            response_builder.body(body).map_err(StreamingError::new)?;

        Ok(Some((response, metadata.policy)))
    }

    async fn put<B>(
        &self,
        cache_key: String,
        response: Response<B>,
        policy: CachePolicy,
        _request_url: Url,
    ) -> Result<Response<Self::Body>>
    where
        B: http_body::Body + Send + 'static,
        B::Data: Send,
        B::Error: Into<StreamingError>,
        <Self::Body as http_body::Body>::Data: Send,
        <Self::Body as http_body::Body>::Error:
            Into<StreamingError> + Send + Sync + 'static,
    {
        let (parts, body) = response.into_parts();

        // Process body with improved streaming approach
        let (content_digest, body_bytes, file_size) =
            self.process_body_streaming(body).await?;
        let content_path = self.content_path(&content_digest);

        // Ensure content directory exists and write content if not already present
        if runtime::metadata(&content_path).await.is_err() {
            Self::atomic_write(&content_path, &body_bytes).await?;
        }

        // Add reference count for this content file (atomic operation)
        let _ref_count = self.ref_counter.add_ref(&content_digest).await?;

        // Ensure content file still exists after adding reference
        if runtime::metadata(&content_path).await.is_err() {
            // Content was deleted between creation and reference addition - rollback
            self.ref_counter.remove_ref(&content_digest).await?;
            return Err(StreamingError::consistency(
                "Content file was deleted during cache operation - possible race condition".to_string()
            ).into());
        }

        // Add to LRU tracking and enforce cache limits
        self.ref_counter
            .add_cache_entry(
                cache_key.clone(),
                content_digest.clone(),
                file_size,
            )
            .await?;
        self.enforce_cache_limits().await?;

        // Create metadata
        let metadata = CacheMetadata {
            status: parts.status.as_u16(),
            version: match parts.version {
                Version::HTTP_09 => 9,
                Version::HTTP_10 => 10,
                Version::HTTP_11 => 11,
                Version::HTTP_2 => 2,
                Version::HTTP_3 => 3,
                _ => 11,
            },
            headers: crate::HttpCacheOptions::headers_to_hashmap(
                &parts.headers,
            ),
            content_digest: content_digest.clone(),
            policy,
            created_at: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
        };

        // Write metadata atomically
        let metadata_path = self.metadata_path(&cache_key);
        let metadata_json = serde_json::to_vec(&metadata)
            .map_err(StreamingError::serialization)?;

        // If metadata write fails, we need to rollback to prevent resource leaks
        if let Err(e) = Self::atomic_write(&metadata_path, &metadata_json).await
        {
            // Rollback: remove reference count and LRU entry
            let content_removed = self
                .ref_counter
                .remove_ref(&content_digest)
                .await
                .unwrap_or(false);
            let _ = self
                .ref_counter
                .rollback_cache_entry(&cache_key, file_size)
                .await;

            // If reference count dropped to 0, clean up content file
            if content_removed {
                let _ = runtime::remove_file(&content_path).await;
            }

            return Err(e);
        }

        // Return response with buffered body for immediate use
        let response =
            Response::from_parts(parts, StreamingBody::buffered(body_bytes));
        Ok(response)
    }

    async fn convert_body<B>(
        &self,
        response: Response<B>,
    ) -> Result<Response<Self::Body>>
    where
        B: http_body::Body + Send + 'static,
        B::Data: Send,
        B::Error: Into<StreamingError>,
        <Self::Body as http_body::Body>::Data: Send,
        <Self::Body as http_body::Body>::Error:
            Into<StreamingError> + Send + Sync + 'static,
    {
        let (parts, body) = response.into_parts();

        // For non-cacheable responses, simply collect the body and return it as buffered
        // This is more efficient than creating temporary files
        let collected =
            body.collect().await.map_err(|e| StreamingError::new(e.into()))?;
        let body_bytes = collected.to_bytes();
        let streaming_body = StreamingBody::buffered(body_bytes);

        Ok(Response::from_parts(parts, streaming_body))
    }

    async fn delete(&self, cache_key: &str) -> Result<()> {
        let metadata_path = self.metadata_path(cache_key);

        // Read metadata to get content digest before removing metadata file
        let metadata_content =
            runtime::read(&metadata_path).await.map_err(StreamingError::io)?;

        let metadata: CacheMetadata = serde_json::from_slice(&metadata_content)
            .map_err(StreamingError::serialization)?;

        // Phase 1: Check if we can delete content file (if it would be orphaned)
        let can_delete_content = {
            // Temporarily decrement reference count to check
            let would_be_orphaned =
                self.ref_counter.remove_ref(&metadata.content_digest).await?;
            if would_be_orphaned {
                // Add the reference back for now - we'll remove it again if all operations succeed
                self.ref_counter.add_ref(&metadata.content_digest).await?;
                true
            } else {
                // Reference count was decremented but content still has other references
                false
            }
        };

        // Phase 2: If content needs deletion, verify we can delete it before proceeding
        if can_delete_content {
            let content_path = self.content_path(&metadata.content_digest);
            if content_path.exists() {
                // Try to open content file to ensure it's not locked
                match runtime::File::open(&content_path).await {
                    Ok(_) => {} // File can be accessed, proceed
                    Err(e) => {
                        // Restore reference count and abort
                        return Err(StreamingError::io(format!(
                            "Cannot delete content file {:?} (may be locked): {}",
                            content_path, e
                        )).into());
                    }
                }
            }
        }

        // Phase 3: Perform transactional delete
        // 3a. Remove from LRU tracking
        self.ref_counter.remove_cache_entry(cache_key).await?;

        // 3b. Decrement reference count (final time)
        let should_delete_content = if can_delete_content {
            // We added back the reference earlier, so remove it again
            self.ref_counter.remove_ref(&metadata.content_digest).await?
        } else {
            false
        };

        // 3c. Remove content file first (if needed) - if this fails, we can still rollback
        if should_delete_content {
            let content_path = self.content_path(&metadata.content_digest);
            if let Err(e) = runtime::remove_file(&content_path).await {
                // Rollback: restore reference count and LRU entry
                self.ref_counter.add_ref(&metadata.content_digest).await?;
                self.ref_counter
                    .add_cache_entry(
                        cache_key.to_string(),
                        metadata.content_digest.clone(),
                        0,
                    )
                    .await?;
                return Err(StreamingError::io(format!(
                    "Failed to remove content file {:?}: {}",
                    content_path, e
                ))
                .into());
            }
        }

        // 3d. Remove metadata file (point of no return)
        if let Err(e) = runtime::remove_file(&metadata_path).await {
            // If we deleted content but can't delete metadata, we're in a bad state
            // but metadata deletion failure is less critical than content deletion failure
            return Err(StreamingError::io(format!(
                "Warning: content deleted but metadata removal failed for {:?}: {}",
                metadata_path, e
            )).into());
        }

        Ok(())
    }

    #[cfg(feature = "streaming")]
    fn body_to_bytes_stream(
        body: Self::Body,
    ) -> impl futures_util::Stream<
        Item = std::result::Result<
            Bytes,
            Box<dyn std::error::Error + Send + Sync>,
        >,
    > + Send
    where
        <Self::Body as http_body::Body>::Data: Send,
        <Self::Body as http_body::Body>::Error: Send + Sync + 'static,
    {
        // Use the StreamingBody's built-in conversion method
        body.into_bytes_stream()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::StreamingCacheManager as StreamingCacheManagerTrait;
    use http_body_util::Full;
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_streaming_cache_put_get() {
        let temp_dir = TempDir::new().unwrap();
        let cache = StreamingManager::new(temp_dir.path().to_path_buf());

        let original_body = Full::new(Bytes::from("test response body"));
        let response = Response::builder()
            .status(200)
            .header("content-type", "text/plain")
            .body(original_body)
            .unwrap();

        let policy = CachePolicy::new(
            &http::request::Request::builder()
                .method("GET")
                .uri("/test")
                .body(())
                .unwrap()
                .into_parts()
                .0,
            &response.clone().map(|_| ()),
        );

        let request_url = Url::parse("http://example.com/test").unwrap();

        // Put response into cache
        let cached_response = cache
            .put("test-key".to_string(), response, policy.clone(), request_url)
            .await
            .unwrap();

        // Response should be returned immediately
        assert_eq!(cached_response.status(), 200);

        // Get response from cache
        let retrieved = cache.get("test-key").await.unwrap();
        assert!(retrieved.is_some());

        let (cached_response, cached_policy) = retrieved.unwrap();
        assert_eq!(cached_response.status(), 200);
        assert_eq!(
            cached_response.headers().get("content-type").unwrap(),
            "text/plain"
        );

        // Verify policy is preserved
        let now = std::time::SystemTime::now();
        assert_eq!(cached_policy.time_to_live(now), policy.time_to_live(now));
    }

    #[tokio::test]
    async fn test_streaming_cache_delete() {
        let temp_dir = TempDir::new().unwrap();
        let cache = StreamingManager::new(temp_dir.path().to_path_buf());

        let original_body = Full::new(Bytes::from("test response body"));
        let response = Response::builder()
            .status(200)
            .header("content-type", "text/plain")
            .body(original_body)
            .unwrap();

        let policy = CachePolicy::new(
            &http::request::Request::builder()
                .method("GET")
                .uri("/test")
                .body(())
                .unwrap()
                .into_parts()
                .0,
            &response.clone().map(|_| ()),
        );

        let request_url = Url::parse("http://example.com/test").unwrap();
        let cache_key = "test-key-delete";

        // Put response into cache
        cache
            .put(cache_key.to_string(), response, policy, request_url)
            .await
            .unwrap();

        // Verify it exists
        let retrieved = cache.get(cache_key).await.unwrap();
        assert!(retrieved.is_some());

        // Delete it
        cache.delete(cache_key).await.unwrap();

        // Verify it's gone
        let retrieved = cache.get(cache_key).await.unwrap();
        assert!(retrieved.is_none());
    }

    /// Test content deduplication - multiple cache entries with identical content
    /// should share the same content file with proper reference counting
    #[tokio::test]
    async fn test_content_deduplication() {
        let temp_dir = TempDir::new().unwrap();
        let cache = StreamingManager::new(temp_dir.path().to_path_buf());

        let identical_content = Bytes::from("identical response body content");
        let request_url = Url::parse("http://example.com/test").unwrap();

        // Create two different responses with identical content
        let response1 = Response::builder()
            .status(200)
            .header("cache-control", "max-age=3600")
            .body(Full::new(identical_content.clone()))
            .unwrap();

        let response2 = Response::builder()
            .status(200)
            .header("content-type", "application/json")
            .body(Full::new(identical_content.clone()))
            .unwrap();

        let policy1 = CachePolicy::new(
            &http::request::Request::builder()
                .method("GET")
                .uri("/test1")
                .body(())
                .unwrap()
                .into_parts()
                .0,
            &response1.clone().map(|_| ()),
        );

        let policy2 = CachePolicy::new(
            &http::request::Request::builder()
                .method("GET")
                .uri("/test2")
                .body(())
                .unwrap()
                .into_parts()
                .0,
            &response2.clone().map(|_| ()),
        );

        // Cache both responses
        cache
            .put("key1".to_string(), response1, policy1, request_url.clone())
            .await
            .unwrap();
        cache
            .put("key2".to_string(), response2, policy2, request_url)
            .await
            .unwrap();

        // Verify both can be retrieved
        let retrieved1 = cache.get("key1").await.unwrap().unwrap();
        let retrieved2 = cache.get("key2").await.unwrap().unwrap();

        assert_eq!(retrieved1.0.status(), 200);
        assert_eq!(retrieved2.0.status(), 200);

        // Verify they have the same content digest (content deduplication)
        let content_digest1 =
            StreamingManager::calculate_digest(&identical_content);
        let content_path1 = cache.content_path(&content_digest1);
        assert!(content_path1.exists());

        // Count content files - should only have one for identical content
        let content_dir = temp_dir.path().join(CACHE_VERSION).join("content");
        let mut content_file_count = 0;
        if content_dir.exists() {
            for entry in std::fs::read_dir(&content_dir).unwrap() {
                let entry = entry.unwrap();
                if entry.file_type().unwrap().is_file() {
                    content_file_count += 1;
                }
            }
        }
        assert_eq!(
            content_file_count, 1,
            "Should have only one content file due to deduplication"
        );

        // Delete one cache entry
        cache.delete("key1").await.unwrap();

        // Content file should still exist due to reference counting
        assert!(
            content_path1.exists(),
            "Content file should still exist after deleting one reference"
        );

        // Verify other entry still works
        let retrieved2_again = cache.get("key2").await.unwrap().unwrap();
        assert_eq!(retrieved2_again.0.status(), 200);

        // Delete second cache entry
        cache.delete("key2").await.unwrap();

        // Now content file should be gone
        assert!(
            !content_path1.exists(),
            "Content file should be deleted when no references remain"
        );
    }

    /// Test reference count persistence across cache manager restarts
    #[tokio::test]
    async fn test_reference_count_persistence() {
        let temp_dir = TempDir::new().unwrap();
        let cache_path = temp_dir.path().to_path_buf();

        let identical_content =
            Bytes::from("persistent reference test content");
        let request_url = Url::parse("http://example.com/test").unwrap();

        // Phase 1: Create initial cache with content deduplication
        {
            let cache = StreamingManager::new(cache_path.clone());

            let response1 = Response::builder()
                .status(200)
                .body(Full::new(identical_content.clone()))
                .unwrap();

            let response2 = Response::builder()
                .status(404)
                .body(Full::new(identical_content.clone()))
                .unwrap();

            let policy1 = CachePolicy::new(
                &http::request::Request::builder()
                    .method("GET")
                    .uri("/test1")
                    .body(())
                    .unwrap()
                    .into_parts()
                    .0,
                &response1.clone().map(|_| ()),
            );

            let policy2 = CachePolicy::new(
                &http::request::Request::builder()
                    .method("GET")
                    .uri("/test2")
                    .body(())
                    .unwrap()
                    .into_parts()
                    .0,
                &response2.clone().map(|_| ()),
            );

            cache
                .put(
                    "persistent-key1".to_string(),
                    response1,
                    policy1,
                    request_url.clone(),
                )
                .await
                .unwrap();
            cache
                .put(
                    "persistent-key2".to_string(),
                    response2,
                    policy2,
                    request_url.clone(),
                )
                .await
                .unwrap();

            // Verify content file exists
            let content_digest =
                StreamingManager::calculate_digest(&identical_content);
            let content_path = cache.content_path(&content_digest);
            assert!(content_path.exists(), "Content file should exist");
        }

        // Phase 2: Create new cache manager and rebuild reference counts
        {
            let cache =
                StreamingManager::new_with_existing_cache(cache_path.clone())
                    .await
                    .unwrap();

            // Verify both entries still exist
            let retrieved1 = cache.get("persistent-key1").await.unwrap();
            let retrieved2 = cache.get("persistent-key2").await.unwrap();
            assert!(retrieved1.is_some());
            assert!(retrieved2.is_some());

            // Delete one entry - content should still exist
            cache.delete("persistent-key1").await.unwrap();

            let content_digest =
                StreamingManager::calculate_digest(&identical_content);
            let content_path = cache.content_path(&content_digest);
            assert!(
                content_path.exists(),
                "Content file should still exist after one deletion"
            );

            // Verify other entry still works
            let retrieved2_again = cache.get("persistent-key2").await.unwrap();
            assert!(retrieved2_again.is_some());

            // Delete second entry - now content should be deleted
            cache.delete("persistent-key2").await.unwrap();

            assert!(
                !content_path.exists(),
                "Content file should be deleted when all references removed"
            );
        }
    }

    /// Test concurrent access to reference counting
    #[tokio::test]
    async fn test_concurrent_reference_counting() {
        use std::sync::Arc;
        use tokio::task;

        let temp_dir = TempDir::new().unwrap();
        let cache =
            Arc::new(StreamingManager::new(temp_dir.path().to_path_buf()));
        let request_url = Url::parse("http://example.com/test").unwrap();

        let shared_content = Bytes::from("concurrent test content");
        let tasks_count = 10;

        // Create multiple tasks that store identical content concurrently
        let mut handles = Vec::new();
        for i in 0..tasks_count {
            let cache = Arc::clone(&cache);
            let content = shared_content.clone();
            let url = request_url.clone();

            let handle = task::spawn(async move {
                let response = Response::builder()
                    .status(200)
                    .header("x-task-id", i.to_string())
                    .body(Full::new(content))
                    .unwrap();

                let policy = CachePolicy::new(
                    &http::request::Request::builder()
                        .method("GET")
                        .uri(format!("/concurrent-test-{}", i))
                        .body(())
                        .unwrap()
                        .into_parts()
                        .0,
                    &response.clone().map(|_| ()),
                );

                cache
                    .put(format!("concurrent-key-{}", i), response, policy, url)
                    .await
                    .unwrap();
            });
            handles.push(handle);
        }

        // Wait for all tasks to complete
        for handle in handles {
            handle.await.unwrap();
        }

        // Verify all entries can be retrieved
        for i in 0..tasks_count {
            let retrieved =
                cache.get(&format!("concurrent-key-{}", i)).await.unwrap();
            assert!(retrieved.is_some(), "Entry {} should exist", i);
        }

        // Verify content deduplication worked (only one content file)
        let content_digest =
            StreamingManager::calculate_digest(&shared_content);
        let content_path = cache.content_path(&content_digest);
        assert!(content_path.exists(), "Shared content file should exist");

        // Delete half the entries concurrently
        let mut delete_handles = Vec::new();
        for i in 0..tasks_count / 2 {
            let cache = Arc::clone(&cache);
            let handle = task::spawn(async move {
                cache.delete(&format!("concurrent-key-{}", i)).await.unwrap();
            });
            delete_handles.push(handle);
        }

        for handle in delete_handles {
            handle.await.unwrap();
        }

        // Content should still exist (remaining references)
        assert!(content_path.exists(), "Content file should still exist");

        // Delete remaining entries
        let mut final_delete_handles = Vec::new();
        for i in tasks_count / 2..tasks_count {
            let cache = Arc::clone(&cache);
            let handle = task::spawn(async move {
                cache.delete(&format!("concurrent-key-{}", i)).await.unwrap();
            });
            final_delete_handles.push(handle);
        }

        for handle in final_delete_handles {
            handle.await.unwrap();
        }

        // Now content should be deleted
        assert!(
            !content_path.exists(),
            "Content file should be deleted when all references removed"
        );
    }

    /// Test large content handling and streaming behavior
    #[tokio::test]
    async fn test_large_content_streaming() {
        let temp_dir = TempDir::new().unwrap();
        let cache = StreamingManager::new(temp_dir.path().to_path_buf());

        // Create a large response body (1MB)
        let large_content = vec![b'X'; 1024 * 1024];
        let large_bytes = Bytes::from(large_content);

        let response = Response::builder()
            .status(200)
            .header("content-type", "application/octet-stream")
            .header("content-length", large_bytes.len().to_string())
            .body(Full::new(large_bytes.clone()))
            .unwrap();

        let policy = CachePolicy::new(
            &http::request::Request::builder()
                .method("GET")
                .uri("/large-file")
                .body(())
                .unwrap()
                .into_parts()
                .0,
            &response.clone().map(|_| ()),
        );

        let request_url = Url::parse("http://example.com/large-file").unwrap();

        // Store large response
        let cached_response = cache
            .put("large-key".to_string(), response, policy, request_url)
            .await
            .unwrap();

        assert_eq!(cached_response.status(), 200);

        // Retrieve and verify
        let (retrieved_response, _) =
            cache.get("large-key").await.unwrap().unwrap();
        assert_eq!(retrieved_response.status(), 200);
        assert_eq!(
            retrieved_response.headers().get("content-type").unwrap(),
            "application/octet-stream"
        );

        // Verify content file exists and has correct size
        let content_digest = StreamingManager::calculate_digest(&large_bytes);
        let content_path = cache.content_path(&content_digest);
        assert!(content_path.exists(), "Large content file should exist");

        let metadata = std::fs::metadata(&content_path).unwrap();
        assert_eq!(
            metadata.len(),
            1024 * 1024,
            "Content file should have correct size"
        );
    }

    /// Test error handling for various failure scenarios
    #[tokio::test]
    async fn test_error_handling() {
        let temp_dir = TempDir::new().unwrap();
        let cache = StreamingManager::new(temp_dir.path().to_path_buf());

        // Test getting non-existent key
        let result = cache.get("non-existent").await.unwrap();
        assert!(result.is_none(), "Should return None for non-existent key");

        // Test deleting non-existent key
        let result = cache.delete("non-existent").await;
        assert!(result.is_err(), "Should error when deleting non-existent key");

        // Test with corrupted metadata (create invalid JSON file)
        let metadata_dir = temp_dir.path().join(CACHE_VERSION).join("metadata");
        std::fs::create_dir_all(&metadata_dir).unwrap();
        let corrupt_metadata_path = metadata_dir.join("corrupt.json");
        std::fs::write(&corrupt_metadata_path, "invalid json").unwrap();

        // Cache should still function normally despite corrupted file
        let response = Response::builder()
            .status(200)
            .body(Full::new(Bytes::from("test")))
            .unwrap();

        let policy = CachePolicy::new(
            &http::request::Request::builder()
                .method("GET")
                .uri("/test")
                .body(())
                .unwrap()
                .into_parts()
                .0,
            &response.clone().map(|_| ()),
        );

        let request_url = Url::parse("http://example.com/test").unwrap();

        let result = cache
            .put("valid-key".to_string(), response, policy, request_url)
            .await;
        assert!(result.is_ok(), "Should handle corrupted metadata gracefully");
    }

    /// Test content validation and corruption detection
    #[tokio::test]
    async fn test_content_integrity_validation() {
        let temp_dir = TempDir::new().unwrap();
        let cache = StreamingManager::new(temp_dir.path().to_path_buf());

        let original_content =
            Bytes::from("original content for integrity test");
        let response = Response::builder()
            .status(200)
            .body(Full::new(original_content.clone()))
            .unwrap();

        let policy = CachePolicy::new(
            &http::request::Request::builder()
                .method("GET")
                .uri("/integrity-test")
                .body(())
                .unwrap()
                .into_parts()
                .0,
            &response.clone().map(|_| ()),
        );

        let request_url =
            Url::parse("http://example.com/integrity-test").unwrap();

        // Store original content
        cache
            .put("integrity-key".to_string(), response, policy, request_url)
            .await
            .unwrap();

        // Verify content can be retrieved
        let retrieved = cache.get("integrity-key").await.unwrap();
        assert!(retrieved.is_some(), "Content should be retrievable");

        // Corrupt the content file
        let content_digest =
            StreamingManager::calculate_digest(&original_content);
        let content_path = cache.content_path(&content_digest);
        assert!(content_path.exists(), "Content file should exist");

        std::fs::write(&content_path, "corrupted content").unwrap();

        // Cache should handle corrupted content gracefully
        let retrieved_after_corruption =
            cache.get("integrity-key").await.unwrap();
        assert!(
            retrieved_after_corruption.is_some(),
            "Should still return metadata even with corrupted content"
        );
    }

    /// Test HTTP version handling
    #[tokio::test]
    async fn test_http_version_preservation() {
        let temp_dir = TempDir::new().unwrap();
        let cache = StreamingManager::new(temp_dir.path().to_path_buf());

        let test_versions = vec![
            (Version::HTTP_09, 9u8),
            (Version::HTTP_10, 10u8),
            (Version::HTTP_11, 11u8),
            (Version::HTTP_2, 2u8),
            (Version::HTTP_3, 3u8),
        ];

        for (i, (version, expected_stored)) in
            test_versions.into_iter().enumerate()
        {
            let response = Response::builder()
                .status(200)
                .version(version)
                .header("content-type", "text/plain")
                .body(Full::new(Bytes::from(format!("version test {}", i))))
                .unwrap();

            let policy = CachePolicy::new(
                &http::request::Request::builder()
                    .method("GET")
                    .uri(format!("/version-test-{}", i))
                    .body(())
                    .unwrap()
                    .into_parts()
                    .0,
                &response.clone().map(|_| ()),
            );

            let request_url =
                Url::parse(&format!("http://example.com/version-test-{}", i))
                    .unwrap();
            let cache_key = format!("version-key-{}", i);

            // Store response
            cache
                .put(cache_key.clone(), response, policy, request_url)
                .await
                .unwrap();

            // Retrieve and verify version is preserved
            let (retrieved_response, _) =
                cache.get(&cache_key).await.unwrap().unwrap();
            assert_eq!(
                retrieved_response.version(),
                version,
                "HTTP version should be preserved for version {:?}",
                version
            );

            // Verify the stored version value in metadata
            let metadata_path = cache.metadata_path(&cache_key);
            let metadata_content = std::fs::read(&metadata_path).unwrap();
            let metadata: CacheMetadata =
                serde_json::from_slice(&metadata_content).unwrap();
            assert_eq!(
                metadata.version, expected_stored,
                "Stored version should match expected for {:?}",
                version
            );
        }
    }

    /// Test header preservation and edge cases
    #[tokio::test]
    async fn test_header_edge_cases() {
        let temp_dir = TempDir::new().unwrap();
        let cache = StreamingManager::new(temp_dir.path().to_path_buf());

        // Test response with various header types
        let response = Response::builder()
            .status(200)
            .header("content-type", "application/json; charset=utf-8")
            .header("cache-control", "max-age=3600, public")
            .header("custom-header", "custom-value")
            .header("empty-header", "")
            .header("unicode-header", "test-ñ-value")
            .header("multiple-values", "value1")
            .header("multiple-values", "value2") // This will overwrite the first one in http crate
            .body(Full::new(Bytes::from(r#"{"test": "json"}"#)))
            .unwrap();

        let policy = CachePolicy::new(
            &http::request::Request::builder()
                .method("GET")
                .uri("/header-test")
                .body(())
                .unwrap()
                .into_parts()
                .0,
            &response.clone().map(|_| ()),
        );

        let request_url = Url::parse("http://example.com/header-test").unwrap();

        // Store response
        cache
            .put(
                "header-key".to_string(),
                response.clone(),
                policy,
                request_url,
            )
            .await
            .unwrap();

        // Retrieve and verify headers
        let (retrieved_response, _) =
            cache.get("header-key").await.unwrap().unwrap();

        // Verify critical headers are preserved
        assert_eq!(
            retrieved_response.headers().get("content-type").unwrap(),
            "application/json; charset=utf-8"
        );
        assert_eq!(
            retrieved_response.headers().get("cache-control").unwrap(),
            "max-age=3600, public"
        );
        assert_eq!(
            retrieved_response.headers().get("custom-header").unwrap(),
            "custom-value"
        );

        // Verify empty header is handled
        assert_eq!(
            retrieved_response.headers().get("empty-header").unwrap(),
            ""
        );
    }

    /// Test edge cases with cache keys
    #[tokio::test]
    async fn test_cache_key_edge_cases() {
        let temp_dir = TempDir::new().unwrap();
        let cache = StreamingManager::new(temp_dir.path().to_path_buf());

        let response = Response::builder()
            .status(200)
            .body(Full::new(Bytes::from("test content")))
            .unwrap();

        let policy = CachePolicy::new(
            &http::request::Request::builder()
                .method("GET")
                .uri("/test")
                .body(())
                .unwrap()
                .into_parts()
                .0,
            &response.clone().map(|_| ()),
        );

        let request_url = Url::parse("http://example.com/test").unwrap();

        // Test various cache key formats
        let edge_case_keys = vec![
            "simple-key",
            "key:with:colons",
            "key with spaces",
            "key/with/slashes",
            "key?with=query&params=true",
            "key#with-fragment",
            "very-long-key-that-exceeds-normal-filename-length-limits-and-should-still-work-properly-without-issues-abcdefghijklmnopqrstuvwxyz",
            "unicode-key-ñáéíóú-test",
            "",  // Empty key
        ];

        for (i, key) in edge_case_keys.into_iter().enumerate() {
            let test_response = Response::builder()
                .status(200)
                .header("x-key-index", i.to_string())
                .body(Full::new(Bytes::from(format!(
                    "content for key: {}",
                    key
                ))))
                .unwrap();

            // Store with edge case key
            let result = cache
                .put(
                    key.to_string(),
                    test_response,
                    policy.clone(),
                    request_url.clone(),
                )
                .await;

            // Skip empty keys and very long keys that might fail due to filesystem limitations
            if key.is_empty() || key.len() > 100 {
                continue;
            }

            assert!(result.is_ok(), "Should handle key: '{}'", key);

            // Retrieve and verify
            let retrieved = cache.get(key).await.unwrap();
            assert!(
                retrieved.is_some(),
                "Should retrieve content for key: '{}'",
                key
            );

            let (retrieved_response, _) = retrieved.unwrap();
            assert_eq!(retrieved_response.status(), 200);
            assert_eq!(
                retrieved_response.headers().get("x-key-index").unwrap(),
                &i.to_string()
            );
        }
    }

    /// Test cache size limits and LRU eviction logic
    #[tokio::test]
    async fn test_cache_size_limits_and_lru_eviction() {
        let temp_dir = TempDir::new().unwrap();
        let config = StreamingCacheConfig {
            max_cache_size: Some(1000), // Very small limit to force evictions
            max_entries: Some(3),       // Max 3 entries
            ..StreamingCacheConfig::default()
        };
        let cache = StreamingManager::new_with_config(
            temp_dir.path().to_path_buf(),
            config,
        );

        let request_url = Url::parse("http://example.com/test").unwrap();

        // Add entries that exceed the cache size limit
        let entries = vec![
            ("key1", "first content - should be evicted first", 500), // 500 bytes
            ("key2", "second content - larger", 600), // 600 bytes
            ("key3", "third content - should remain", 400), // 400 bytes
        ];

        for (key, content, _size) in &entries {
            let response = Response::builder()
                .status(200)
                .body(Full::new(Bytes::from(*content)))
                .unwrap();

            let policy = CachePolicy::new(
                &http::request::Request::builder()
                    .method("GET")
                    .uri(format!("/{}", key))
                    .body(())
                    .unwrap()
                    .into_parts()
                    .0,
                &response.clone().map(|_| ()),
            );

            cache
                .put(key.to_string(), response, policy, request_url.clone())
                .await
                .unwrap();

            // Small delay to ensure different access times
            std::thread::sleep(std::time::Duration::from_millis(10));
        }

        // Access key2 to make it more recently used than key1
        let _retrieved = cache.get("key2").await.unwrap();

        // Add a new entry that should trigger eviction
        let large_content = "x".repeat(700); // 700 bytes, should evict key1 (LRU)
        let response = Response::builder()
            .status(200)
            .body(Full::new(Bytes::from(large_content)))
            .unwrap();

        let policy = CachePolicy::new(
            &http::request::Request::builder()
                .method("GET")
                .uri("/key4")
                .body(())
                .unwrap()
                .into_parts()
                .0,
            &response.clone().map(|_| ()),
        );

        cache
            .put("key4".to_string(), response, policy, request_url)
            .await
            .unwrap();

        // key1 should be evicted (oldest), key2, key3, key4 should remain
        assert!(
            cache.get("key1").await.unwrap().is_none(),
            "key1 should be evicted"
        );
        assert!(
            cache.get("key2").await.unwrap().is_some(),
            "key2 should remain"
        );
        assert!(
            cache.get("key3").await.unwrap().is_some(),
            "key3 should remain"
        );
        assert!(
            cache.get("key4").await.unwrap().is_some(),
            "key4 should remain"
        );
    }

    /// Test background cleanup functionality
    #[tokio::test]
    async fn test_background_cleanup() {
        let temp_dir = TempDir::new().unwrap();
        let config = StreamingCacheConfig {
            // Background cleanup simplified - no longer configurable
            // Integrity verification simplified
            ..StreamingCacheConfig::default()
        };
        let cache = StreamingManager::new_with_config(
            temp_dir.path().to_path_buf(),
            config,
        );

        let request_url = Url::parse("http://example.com/test").unwrap();

        // Add some entries
        let response = Response::builder()
            .status(200)
            .body(Full::new(Bytes::from("cleanup test content")))
            .unwrap();

        let policy = CachePolicy::new(
            &http::request::Request::builder()
                .method("GET")
                .uri("/cleanup-test")
                .body(())
                .unwrap()
                .into_parts()
                .0,
            &response.clone().map(|_| ()),
        );

        cache
            .put("cleanup-key".to_string(), response, policy, request_url)
            .await
            .unwrap();

        // Manually create an orphaned content file (simulate a crash scenario)
        let content_dir = temp_dir.path().join(CACHE_VERSION).join("content");
        let orphaned_file = content_dir.join("orphaned_content_file");
        std::fs::write(&orphaned_file, "orphaned content").unwrap();

        // Trigger cleanup by waiting past the interval and doing an operation
        std::thread::sleep(std::time::Duration::from_secs(2));

        // This operation should trigger lazy background cleanup
        let _retrieved = cache.get("cleanup-key").await.unwrap();

        // Give cleanup time to run
        std::thread::sleep(std::time::Duration::from_millis(100));

        // Valid entry should still exist, orphaned file may be cleaned up
        assert!(cache.get("cleanup-key").await.unwrap().is_some());
    }

    /// Test rollback behavior when metadata write fails after content is written
    #[tokio::test]
    async fn test_metadata_write_failure_rollback() {
        let temp_dir = TempDir::new().unwrap();
        let cache = StreamingManager::new(temp_dir.path().to_path_buf());

        let request_url = Url::parse("http://example.com/test").unwrap();
        let content = Bytes::from("rollback test content");

        let response = Response::builder()
            .status(200)
            .body(Full::new(content.clone()))
            .unwrap();

        let policy = CachePolicy::new(
            &http::request::Request::builder()
                .method("GET")
                .uri("/rollback-test")
                .body(())
                .unwrap()
                .into_parts()
                .0,
            &response.clone().map(|_| ()),
        );

        // Use a cache key that will result in an invalid metadata path
        // Create a key that when hex-encoded will exceed path length limits on most systems
        // Most filesystems have a 255-byte filename limit, so create something longer
        let very_long_key = "a".repeat(300); // This will create a 600-character hex string

        // This put operation should fail due to metadata filename being too long
        let result = cache
            .put(very_long_key.clone(), response, policy, request_url)
            .await;

        // The operation should fail
        assert!(result.is_err(), "Put should fail when metadata write fails");

        // Verify that no entry exists for the long key after rollback
        let retrieved = cache.get(&very_long_key).await.unwrap();
        assert!(retrieved.is_none(), "Entry should not exist after rollback");

        // Verify that content files are properly cleaned up
        // Since content write should succeed but metadata write fails,
        // the content should be cleaned up during rollback
        let content_digest = StreamingManager::calculate_digest(&content);
        let content_path = cache.content_path(&content_digest);

        // Content file should either not exist or have been cleaned up
        // (it might not exist at all if the reference counting rollback worked perfectly)
        let content_exists = runtime::metadata(&content_path).await.is_ok();
        if content_exists {
            // If content exists, ensure reference count is 0 or the file is orphaned
            // This is acceptable as long as the cache entry doesn't exist
            println!(
                "Content file exists but cache entry was properly rolled back"
            );
        }

        // Content file should be cleaned up (not orphaned)
        let content_digest = StreamingManager::calculate_digest(&content);
        let _content_path = cache.content_path(&content_digest);

        // Content file might still exist if it was created by another entry,
        // but reference count should be properly managed
        let retrieved = cache.get("rollback-key").await.unwrap();
        assert!(retrieved.is_none(), "Entry should not exist after rollback");
    }

    /// Test atomic file operations under concurrent stress
    #[tokio::test]
    async fn test_atomic_operations_under_stress() {
        let temp_dir = TempDir::new().unwrap();
        let cache =
            Arc::new(StreamingManager::new(temp_dir.path().to_path_buf()));
        let request_url = Url::parse("http://example.com/test").unwrap();

        let tasks_count = 20;
        let mut handles = Vec::new();

        // Create tasks that perform rapid put/get/delete operations
        for i in 0..tasks_count {
            let cache = Arc::clone(&cache);
            let url = request_url.clone();

            let handle = tokio::task::spawn(async move {
                for j in 0..10 {
                    let key = format!("stress-key-{}-{}", i, j);
                    let content = format!("stress test content {} {}", i, j);

                    let response = Response::builder()
                        .status(200)
                        .body(Full::new(Bytes::from(content)))
                        .unwrap();

                    let policy = CachePolicy::new(
                        &http::request::Request::builder()
                            .method("GET")
                            .uri(format!("/stress-{}-{}", i, j))
                            .body(())
                            .unwrap()
                            .into_parts()
                            .0,
                        &response.clone().map(|_| ()),
                    );

                    // Put, get, and delete in rapid succession
                    let put_result = cache
                        .put(key.clone(), response, policy, url.clone())
                        .await;
                    assert!(
                        put_result.is_ok(),
                        "Put should succeed under stress"
                    );

                    let get_result = cache.get(&key).await.unwrap();
                    assert!(
                        get_result.is_some(),
                        "Get should succeed after put"
                    );

                    let delete_result = cache.delete(&key).await;
                    assert!(delete_result.is_ok(), "Delete should succeed");

                    // Verify it's gone
                    let final_get = cache.get(&key).await.unwrap();
                    assert!(
                        final_get.is_none(),
                        "Entry should be gone after delete"
                    );
                }
            });
            handles.push(handle);
        }

        // Wait for all stress test tasks to complete
        for handle in handles {
            handle.await.unwrap();
        }

        // Verify cache is in a consistent state - should be mostly empty
        let content_dir = temp_dir.path().join(CACHE_VERSION).join("content");
        let metadata_dir = temp_dir.path().join(CACHE_VERSION).join("metadata");

        // Count remaining files (should be minimal)
        let content_count = if content_dir.exists() {
            std::fs::read_dir(&content_dir).unwrap().count()
        } else {
            0
        };
        let metadata_count = if metadata_dir.exists() {
            std::fs::read_dir(&metadata_dir).unwrap().count()
        } else {
            0
        };

        // After all operations, there should be very few or no files left
        assert!(
            content_count <= 5,
            "Should have minimal content files after stress test"
        );
        assert!(
            metadata_count <= 5,
            "Should have minimal metadata files after stress test"
        );
    }

    /// Test configuration validation and edge cases
    #[tokio::test]
    async fn test_config_validation() {
        let temp_dir = TempDir::new().unwrap();

        // Test with extreme config values
        let config = StreamingCacheConfig {
            max_cache_size: Some(0), // Zero size
            max_entries: Some(0),    // Zero entries
            // Cleanup simplified - no intervals needed
            streaming_buffer_size: 1, // Minimum buffer
        };

        let cache = StreamingManager::new_with_config(
            temp_dir.path().to_path_buf(),
            config,
        );

        let request_url = Url::parse("http://example.com/test").unwrap();
        let response = Response::builder()
            .status(200)
            .body(Full::new(Bytes::from("config test")))
            .unwrap();

        let policy = CachePolicy::new(
            &http::request::Request::builder()
                .method("GET")
                .uri("/config-test")
                .body(())
                .unwrap()
                .into_parts()
                .0,
            &response.clone().map(|_| ()),
        );

        // Should handle extreme config gracefully
        let _result = cache
            .put("config-key".to_string(), response, policy, request_url)
            .await;

        // With zero cache size/entries, the put might succeed but get might fail
        // The important thing is it doesn't panic or crash
        let _get_result = cache.get("config-key").await;
        // Just verify we can call operations without panicking
    }
}