cached 3.0.0-rc.10

Generic cache implementations and simplified function memoization
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
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
use crate::time::Duration;
use crate::time::Instant;
use crate::{CacheEvict, CacheTtl, Cached, CachedIter, CachedPeek, CachedRead, CloneCached};

use super::{DefaultHashBuilder, StripedCounter};
use std::borrow::Borrow;
use std::cmp::Ordering as CmpOrdering;
use std::collections::BTreeSet;
use std::hash::{BuildHasher, Hash, Hasher};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering as AtomicOrdering};
#[cfg(feature = "async_core")]
use {super::CachedGetOrSetAsync, std::future::Future};

use std::collections::HashMap;

/// Wrap keys in Arc for shared ownership between the HashMap values and BTreeSet index.
#[derive(Eq)]
struct CacheArc<T>(Arc<T>);

impl<T> CacheArc<T> {
    fn new(key: T) -> Self {
        CacheArc(Arc::new(key))
    }
}

impl<T> Clone for CacheArc<T> {
    fn clone(&self) -> Self {
        CacheArc(self.0.clone())
    }
}

impl<T: PartialEq> PartialEq for CacheArc<T> {
    fn eq(&self, other: &Self) -> bool {
        self.0.eq(&other.0)
    }
}

impl<T: PartialOrd> PartialOrd for CacheArc<T> {
    fn partial_cmp(&self, other: &Self) -> Option<CmpOrdering> {
        self.0.partial_cmp(&other.0)
    }
}
impl<T: Ord> Ord for CacheArc<T> {
    fn cmp(&self, other: &Self) -> CmpOrdering {
        self.0.cmp(&other.0)
    }
}

impl<T: Hash> Hash for CacheArc<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.0.hash(state);
    }
}

impl<T> Borrow<T> for CacheArc<T> {
    fn borrow(&self) -> &T {
        &self.0
    }
}

/// A timestamped key to allow identifying key ranges.
///
/// `expiry` is `Option<Instant>`: `None` means "never expires" and sorts as GREATER
/// than any `Some(instant)` so that never-expiring entries appear last in the
/// expiry-ordered BTreeSet (evicted last under size pressure, never swept by TTL).
/// Rust's default `Option` ordering would put `None` first (least), so we implement
/// a custom `Ord` / `PartialOrd` that reverses that.
#[derive(Hash, Eq, PartialEq)]
struct Stamped<K> {
    expiry: Option<Instant>,

    // wrapped in an option so it's easy to generate
    // a range bound containing None
    key: Option<CacheArc<K>>,
}

impl<K: Ord> Ord for Stamped<K> {
    fn cmp(&self, other: &Self) -> CmpOrdering {
        // Compare expiries: None (never-expires) sorts GREATEST.
        let expiry_ord = match (&self.expiry, &other.expiry) {
            (None, None) => CmpOrdering::Equal,
            (None, Some(_)) => CmpOrdering::Greater,
            (Some(_), None) => CmpOrdering::Less,
            (Some(a), Some(b)) => a.cmp(b),
        };
        expiry_ord.then_with(|| self.key.cmp(&other.key))
    }
}

impl<K: Ord> PartialOrd for Stamped<K> {
    fn partial_cmp(&self, other: &Self) -> Option<CmpOrdering> {
        Some(self.cmp(other))
    }
}

impl<K> Clone for Stamped<K> {
    fn clone(&self) -> Self {
        Self {
            expiry: self.expiry,
            key: self.key.clone(),
        }
    }
}

impl<K> Stamped<K> {
    /// Build a sentinel `Stamped` for use as a BTreeSet range bound.
    /// Only `Some(expiry)` bounds are used for expiry-sweep ranges; never-expiring
    /// entries (`None`) sort beyond all `Some(_)` values and are excluded automatically.
    ///
    /// This is the canonical constructor for the `key: None` sentinel that the `Option` in
    /// [`Stamped::key`] exists for: [`TtlSortedCache::evict_at`] uses it as the `split_off`
    /// pivot, and the tests use it to re-derive the pre-refactor range-based expected counts.
    fn bound(expiry: Instant) -> Stamped<K> {
        Stamped {
            expiry: Some(expiry),
            key: None,
        }
    }
}

/// A timestamped value to allow re-building a timestamped key.
/// `expiry` is `None` when the entry never expires (TTL was zero at insert time).
struct Entry<K, V> {
    expiry: Option<Instant>,
    key: CacheArc<K>,
    value: V,
}

impl<K, V> Entry<K, V> {
    fn as_stamped(&self) -> Stamped<K> {
        Stamped {
            expiry: self.expiry,
            key: Some(self.key.clone()),
        }
    }

    /// Returns `true` if the entry's expiry instant has passed as of `now`.
    ///
    /// Expiry is at-or-after the deadline (`now >= expiry`), matching
    /// [`TtlCache`](super::TtlCache) / `LruTtlCache`. Split from [`is_expired`](Self::is_expired)
    /// so the boundary can be unit-tested with a controlled `now` (the real monotonic clock
    /// never yields an exact `now == expiry` tie deterministically).
    fn is_expired_at(&self, now: Instant) -> bool {
        self.expiry.is_some_and(|e| e <= now)
    }

    /// Returns `true` if the entry's expiry instant has passed as of the current time.
    fn is_expired(&self) -> bool {
        self.is_expired_at(Instant::now())
    }
}

impl<K, V: Clone> Clone for Entry<K, V> {
    fn clone(&self) -> Self {
        Self {
            expiry: self.expiry,
            key: self.key.clone(),
            value: self.value.clone(),
        }
    }
}

/// A cache enforcing time expiration and an optional maximum size.
/// When a maximum size is specified, the values are dropped in the
/// order of expiration date, e.g. the next value to expire is dropped.
/// This cache is intended for high read scenarios to allow for concurrent
/// reads while still enforcing expiration and an optional maximum cache size.
///
/// To accomplish this, there are a few trade-offs:
///  - Maximum cache size logic cannot support "LRU", instead dropping the next value to expire
///  - Cache keys must implement `Ord`
///  - Eviction must be explicitly requested, either on its own or while inserting
///
/// **`len` / `iter` / `evict` contract**: `len()` returns the raw stored entry count
/// and may include expired-but-not-yet-swept entries - it is only guaranteed to be
/// accurate immediately after a call to `evict()` or `retain_latest()`. `iter()` omits
/// expired entries from the view but does not remove them. Call `evict()` (via
/// [`CacheEvict`](crate::CacheEvict)) to physically remove expired entries and obtain
/// an accurate live count.
///
/// `cache_get_or_set_with` returns `&V` (a shared reference), not `&mut V`.
/// Binding it as `&mut V` is a compile error; use
/// [`cache_get_or_set_with_mut`](crate::Cached::cache_get_or_set_with_mut) when
/// a mutable reference is needed.
///
/// ```compile_fail
/// use cached::{Cached, stores::TtlSortedCache};
/// use cached::time::Duration;
///
/// let mut cache = TtlSortedCache::<u32, u32>::builder()
///     .ttl(Duration::from_secs(60))
///     .build()
///     .unwrap();
/// // compile error: cannot bind &mut u32 from cache_get_or_set_with which returns &u32
/// let _: &mut u32 = cache.cache_get_or_set_with(1, || 2);
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "time_stores")))]
pub struct TtlSortedCache<K, V, S = DefaultHashBuilder> {
    // a minimum instant to compare ranges against since
    // all keys must logically expire after the creation
    // of the cache
    min_instant: Instant,

    // k/v where entry contains corresponds to an ordered value in `keys`
    map: HashMap<K, Entry<K, V>, S>,

    // ordered in ascending expiration `Instant`s
    // to support retaining/evicting without full traversal
    keys: BTreeSet<Stamped<K>>,

    pub(super) ttl: Duration,
    pub(super) size_limit: Option<usize>,
    // Preallocation hint captured at build so `cache_reset` can shrink back to
    // it instead of to zero, matching `TtlCache` (CORE-8).
    pub(super) initial_capacity: Option<usize>,
    pub(super) hits: StripedCounter,
    pub(super) misses: StripedCounter,
    pub(super) evictions: AtomicU64,
    pub(super) on_evict: Option<super::OnEvict<K, V>>,
}

impl<K, V, S> std::fmt::Debug for TtlSortedCache<K, V, S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TtlSortedCache")
            .field("ttl", &self.ttl)
            .field("size_limit", &self.size_limit)
            .field("hits", &self.hits.load())
            .field("misses", &self.misses.load())
            .field("evictions", &self.evictions.load(AtomicOrdering::Relaxed))
            .field("on_evict", &self.on_evict.as_ref().map(|_| "on_evict"))
            .finish()
    }
}

impl<K, V, S> Clone for TtlSortedCache<K, V, S>
where
    K: Clone + Hash + Eq + Ord,
    V: Clone,
    S: Clone,
{
    fn clone(&self) -> Self {
        Self {
            min_instant: self.min_instant,
            map: self.map.clone(),
            keys: self.keys.clone(),
            ttl: self.ttl,
            size_limit: self.size_limit,
            initial_capacity: self.initial_capacity,
            hits: self.hits.snapshot(),
            misses: self.misses.snapshot(),
            evictions: AtomicU64::new(self.evictions.load(AtomicOrdering::Relaxed)),
            on_evict: self.on_evict.clone(),
        }
    }
}

/// Builder for [`TtlSortedCache`].
#[cfg_attr(docsrs, doc(cfg(feature = "time_stores")))]
pub struct TtlSortedCacheBuilder<K, V, S = DefaultHashBuilder> {
    size: Option<usize>,
    capacity: Option<usize>,
    ttl: Option<Duration>,
    on_evict: Option<super::OnEvict<K, V>>,
    hasher: S,
}

impl<K, V> Default for TtlSortedCacheBuilder<K, V, DefaultHashBuilder> {
    fn default() -> Self {
        Self {
            size: None,
            capacity: None,
            ttl: None,
            on_evict: None,
            hasher: super::new_default_hash_builder(),
        }
    }
}

impl<K, V> TtlSortedCacheBuilder<K, V> {
    /// Create a builder with default settings. Equivalent to [`TtlSortedCache::builder`].
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }
}

impl<K, V, S> TtlSortedCacheBuilder<K, V, S> {
    /// Set the maximum number of entries (eviction bound). When the cache exceeds this
    /// limit, the next-to-expire entries are evicted until it is within bounds. Unlike
    /// [`initial_capacity`](Self::initial_capacity), this is a hard cap on entry count, not a
    /// preallocation hint.
    #[doc(alias = "size")]
    #[doc(alias = "capacity")]
    #[must_use]
    pub fn max_size(mut self, max_size: usize) -> Self {
        self.size = Some(max_size);
        self
    }

    /// Pre-allocate capacity for the backing store. This is a *preallocation hint* only —
    /// it does **not** bound the cache. Use [`max_size`](Self::max_size) to set the eviction
    /// bound. Reserves room for at least `capacity` entries in the backing map (the exact
    /// amount may be rounded up by the allocator), matching the preallocation semantics of
    /// the pre-2.0 `with_ttl_and_capacity` constructor.
    ///
    /// When set, this takes precedence over the preallocation implied by
    /// [`max_size`](Self::max_size): the backing map reserves for `capacity` entries rather
    /// than `max_size + 1`. This lets you cap entries at a large `max_size` while starting
    /// with a small allocation that grows on demand. Passing `capacity` larger than
    /// `max_size` is valid — the map simply starts larger; `max_size` still bounds the entry
    /// count. Only the backing map is pre-allocated; the `BTreeSet` TTL index is not.
    ///
    /// Note that [`set_max_size`](TtlSortedCache::set_max_size) on a live cache may re-grow
    /// the backing map to `max_size + 1`, overriding a smaller `initial_capacity` set here.
    #[must_use]
    pub fn initial_capacity(mut self, capacity: usize) -> Self {
        self.capacity = Some(capacity);
        self
    }

    /// Set the TTL for cache entries. Required.
    ///
    /// Overrides any previously set ttl/ttl_secs/ttl_millis on this builder.
    #[must_use]
    pub fn ttl(mut self, ttl: Duration) -> Self {
        self.ttl = Some(ttl);
        self
    }

    /// Set the TTL for cache entries in whole seconds. Equivalent to
    /// `ttl(Duration::from_secs(secs))`.
    ///
    /// Overrides any previously set ttl/ttl_secs/ttl_millis on this builder.
    #[must_use]
    pub fn ttl_secs(self, secs: u64) -> Self {
        self.ttl(Duration::from_secs(secs))
    }

    /// Set the TTL for cache entries in milliseconds. Equivalent to
    /// `ttl(Duration::from_millis(millis))`.
    ///
    /// Overrides any previously set ttl/ttl_secs/ttl_millis on this builder.
    #[must_use]
    pub fn ttl_millis(self, millis: u64) -> Self {
        self.ttl(Duration::from_millis(millis))
    }

    /// Set a callback invoked when an entry is evicted. Fires for:
    /// - Size-limit evictions during insert (capacity-based, oldest-TTL-first).
    /// - TTL-expiry sweeps via [`evict`](TtlSortedCache::evict) and [`retain_latest`](TtlSortedCache::retain_latest).
    /// - Lazy expiry removal during [`cache_get`](crate::Cached::cache_get) / [`cache_get_mut`](crate::Cached::cache_get_mut).
    /// - Explicit [`cache_remove`](crate::Cached::cache_remove), including when the removed entry was already expired.
    ///
    /// Does **not** fire on [`cache_clear`](crate::Cached::cache_clear) / [`cache_reset`](crate::Cached::cache_reset).
    /// Use [`cache_clear_with_on_evict`](TtlSortedCache::cache_clear_with_on_evict)
    /// instead of [`cache_clear`](crate::Cached::cache_clear) to opt into callback
    /// firing and eviction counter increments when clearing all entries.
    #[must_use]
    pub fn on_evict(mut self, on_evict: impl Fn(&K, &V) + Send + Sync + 'static) -> Self {
        self.on_evict = Some(Arc::new(on_evict));
        self
    }

    /// Switch to a custom hash builder `S2`, returning a builder parameterized on `S2`.
    ///
    /// The hasher is used to hash keys in the internal `HashMap`. Calling this method
    /// changes the builder's type parameter so `build()` returns a `TtlSortedCache<K, V, S2>`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use cached::{Cached, stores::TtlSortedCache};
    /// use cached::time::Duration;
    /// use std::collections::hash_map::RandomState;
    ///
    /// let mut cache = TtlSortedCache::<u32, u32>::builder()
    ///     .ttl_secs(60)
    ///     .hasher(RandomState::new())
    ///     .build()
    ///     .unwrap();
    /// cache.cache_set(1, 100);
    /// assert_eq!(cache.cache_get(&1), Some(&100));
    /// ```
    #[doc(alias = "with_hasher")]
    #[must_use]
    pub fn hasher<S2: BuildHasher>(self, hasher: S2) -> TtlSortedCacheBuilder<K, V, S2> {
        TtlSortedCacheBuilder {
            size: self.size,
            capacity: self.capacity,
            ttl: self.ttl,
            on_evict: self.on_evict,
            hasher,
        }
    }

    /// Build the cache.
    ///
    /// # Errors
    ///
    /// Returns [`BuildError`](super::BuildError) if `ttl` is not set or is zero, or if `size` is `0`.
    pub fn build(self) -> Result<TtlSortedCache<K, V, S>, super::BuildError>
    where
        K: Hash + Eq + Ord + Clone,
        S: BuildHasher,
    {
        let ttl = self.ttl.ok_or(super::BuildError::MissingRequired("ttl"))?;
        super::validate_ttl(ttl)?;
        if self.size == Some(0) {
            return Err(super::BuildError::InvalidValue {
                field: "max_size",
                reason: "must be greater than zero",
            });
        }
        let mut cache = TtlSortedCache {
            min_instant: Instant::now(),
            map: HashMap::with_hasher(self.hasher),
            keys: BTreeSet::new(),
            ttl,
            size_limit: self.size,
            initial_capacity: None,
            hits: StripedCounter::new(),
            misses: StripedCounter::new(),
            evictions: AtomicU64::new(0),
            on_evict: self.on_evict,
        };
        // Decide the single preallocation amount once all options are known.
        // An explicit `capacity` is the preallocation hint and takes precedence,
        // reserving for `capacity` and matching the old `with_ttl_and_capacity`.
        // Otherwise fall back to the previous internal behavior where a size limit
        // pre-reserved `size + 1` entries. We reserve only once: issuing the
        // `size + 1` reservation first would defeat a smaller explicit `capacity`,
        // since `HashMap::reserve` does not reduce an existing allocation.
        // A fallible `try_reserve` so an oversized `max_size`/`initial_capacity`
        // returns `Err(BuildError)` instead of aborting on capacity overflow,
        // matching `LruCache`/`LruTtlCache` (CORE-1).
        let (preallocate, field) = match self.capacity {
            Some(cap) => (Some(cap), "initial_capacity"),
            None => (self.size.map(|size| size.saturating_add(1)), "max_size"),
        };
        if let Some(amount) = preallocate {
            cache
                .map
                .try_reserve(amount)
                .map_err(|_| super::BuildError::InvalidValue {
                    field,
                    reason: "allocation failed",
                })?;
            cache.initial_capacity = Some(amount);
        }
        Ok(cache)
    }
}

impl<K: Hash + Eq + Ord + Clone, V> TtlSortedCache<K, V> {
    /// Construct a ready-to-use [`TtlSortedCache`] with the given `ttl` and no size bound.
    ///
    /// For optional settings (`max_size`, `capacity`, `on_evict`) use
    /// [`builder`](Self::builder).
    ///
    /// # Panics
    ///
    /// Panics if `ttl` is zero. Use [`builder`](Self::builder) with
    /// [`build`](TtlSortedCacheBuilder::build) to handle a zero TTL without panicking.
    #[must_use]
    pub fn new(ttl: Duration) -> Self {
        Self::builder()
            .ttl(ttl)
            .build()
            .expect("TtlSortedCache::new requires a non-zero ttl")
    }

    /// Return a builder for constructing a [`TtlSortedCache`].
    #[must_use]
    pub fn builder() -> TtlSortedCacheBuilder<K, V> {
        TtlSortedCacheBuilder::default()
    }
}

impl<K: Hash + Eq + Ord + Clone, V, S: BuildHasher> TtlSortedCache<K, V, S> {
    /// Set the maximum number of entries. When reached, the next entries to expire are evicted.
    /// Returns the previous value if one was set.
    ///
    /// If the new bound is smaller than the current entry count, entries are evicted immediately
    /// (in expiry order, next-to-expire first) so the cache is within the new bound on return,
    /// firing `on_evict` and counting each eviction. This matches
    /// [`LruCache::set_max_size`](super::LruCache::set_max_size), which also evicts down to the
    /// new bound eagerly rather than deferring to the next insert.
    ///
    /// The backing map grows on demand as entries are inserted (it is not pre-reserved here),
    /// matching [`LruCache::set_max_size`](super::LruCache::set_max_size); so this cannot abort
    /// on a capacity-overflowing `max_size`.
    ///
    /// # Panics
    ///
    /// Panics if `max_size` is 0. Use [`TtlSortedCache::try_set_max_size`] to handle invalid
    /// sizes without panicking.
    ///
    /// # See also
    ///
    /// [`LruCache::set_max_size`](super::LruCache::set_max_size) and
    /// [`LruTtlCache::set_max_size`](super::LruTtlCache::set_max_size) are parallel methods
    /// on the other LRU-family stores. Note that this method returns `Option<usize>` (the
    /// previous bound, which is optional) rather than `usize`, because `TtlSortedCache` does
    /// not require a size bound at construction. All stores also provide a fallible
    /// `try_set_max_size` counterpart.
    pub fn set_max_size(&mut self, max_size: usize) -> Option<usize> {
        assert!(max_size > 0, "max_size must be greater than zero");
        let prev = self.size_limit;
        self.size_limit = Some(max_size);
        // Evict down to the new bound immediately rather than waiting for the next insert, so a
        // shrink takes effect on return (matching `LruCache::set_max_size`). `retain_latest`
        // drops the next-to-expire entries first, firing `on_evict` and counting each eviction.
        if self.map.len() > max_size {
            let _ = self.retain_latest(max_size, false);
        }
        prev
    }

    /// Set a non-zero maximum number of entries. When reached, the next entries to expire are evicted.
    ///
    /// # Errors
    ///
    /// Returns [`SetMaxSizeError::ZeroMaxSize`](super::SetMaxSizeError) if `max_size` is 0.
    pub fn try_set_max_size(
        &mut self,
        max_size: usize,
    ) -> Result<Option<usize>, super::SetMaxSizeError> {
        if max_size == 0 {
            return Err(super::SetMaxSizeError::ZeroMaxSize);
        }
        Ok(self.set_max_size(max_size))
    }

    /// Returns the maximum number of entries this cache will hold before evicting,
    /// or `None` if no size bound is configured.
    ///
    /// This is the bound set via [`TtlSortedCacheBuilder::max_size`] /
    /// [`set_max_size`](Self::set_max_size), not the current number of entries — use
    /// [`cache_size`](crate::Cached::cache_size) for that. Unlike
    /// [`LruCache::capacity`](crate::LruCache::capacity) (which returns `usize`),
    /// this returns `Option<usize>` because the bound is optional for this store.
    #[doc(alias = "size")]
    #[doc(alias = "max_size")]
    #[must_use]
    pub fn capacity(&self) -> Option<usize> {
        self.size_limit
    }

    /// Increase backing stores with enough capacity to store `more`
    pub fn reserve(&mut self, more: usize) {
        self.map.reserve(more);
    }

    /// Evict values that have expired.
    /// Returns number of dropped items.
    #[must_use]
    pub fn evict(&mut self) -> usize {
        self.evict_at(Instant::now())
    }

    /// [`evict`](Self::evict) against an explicit `cutoff`, so a caller that already sampled
    /// the clock (e.g. [`set_inner`](Self::set_inner) on an evicting insert) does not pay for a
    /// second `Instant::now()`.
    ///
    /// The expired entries are exactly the front prefix of `self.keys`: the index is ordered by
    /// expiry and `None` (never-expires) sorts GREATEST (see [`Stamped`]'s `Ord`). Rather than
    /// counting a range and then popping the same prefix one entry at a time (two traversals,
    /// and an `O(log n)` rebalance per popped entry), the prefix is detached in one
    /// `split_off` and drained by value: the split is a single `O(log n)` descent and the
    /// drain is an in-order walk of a tree that is being consumed, so no rebalancing happens
    /// at all.
    ///
    /// The boundary is `expiry < cutoff` (strictly-less), exactly what the previous
    /// `range(Included(bound(min_instant))..Excluded(bound(cutoff)))` selected: the sentinel
    /// `Stamped::bound(cutoff)` carries `key: None`, which sorts below every real key at the
    /// same expiry, so a real entry expiring exactly at `cutoff` stays on the live side under
    /// `split_off` just as it was excluded from the old range. That matches `is_expired_at`'s
    /// at-or-after boundary in practice: `cutoff` is sampled by the caller, so no stored expiry
    /// (computed at an earlier insert) ever equals it, and any entry that did tie is caught by
    /// the next `is_expired` get. The old `min_instant` lower bound is likewise unnecessary —
    /// every stored expiry is `>= min_instant` by construction (it is `insert_time + ttl` and
    /// the cache was built before any insert) — so dropping it saves a tree descent.
    ///
    /// Returns the number of index entries dropped (matching the old `pop_first` count), while
    /// `evictions` counts only the entries that were actually present in the map.
    fn evict_at(&mut self, cutoff: Instant) -> usize {
        // Detach `[.., cutoff)` in one operation: `split_off` leaves everything BELOW the
        // sentinel in `self.keys` and returns the rest, so swap the two halves back.
        let live = self.keys.split_off(&Stamped::bound(cutoff));
        let expired = std::mem::replace(&mut self.keys, live);
        let count = expired.len();
        if count == 0 {
            return 0;
        }

        if self.on_evict.is_none() {
            // Drain the map FIRST, moving every removed `Entry` into `removed`, and let the
            // values/keys drop only after the whole prefix has left `self.map` — exactly as
            // the callback branch below does. A per-iteration drop (dropping each `Entry`
            // inside the loop) would let a panicking `Drop` for `V` or `K` unwind with the
            // remaining stamps already detached from `self.keys` but their entries still in
            // `self.map`: orphaned rows, invisible to a later sweep yet still counted by
            // `len`. Collecting keeps `self.map` and `self.keys` in lockstep across a panic.
            let mut removed = Vec::with_capacity(count);
            for stamped in expired {
                // Invariant: `None` keys are only used as artificial range sentinels
                // in `evict()`/`retain_latest()` and are never inserted into `self.keys`.
                let key = stamped
                    .key
                    .expect("evicting: only artificial bounds are none");
                if let Some(entry) = self.map.remove(key.0.as_ref()) {
                    removed.push((key, entry));
                }
            }
            // Count before the drop: a `Drop` panic while `removed` unwinds must not leave
            // the counter short of what was actually pulled from the map.
            self.evictions
                .fetch_add(removed.len() as u64, AtomicOrdering::Relaxed);
            return count;
        }

        // With a callback configured, drain both structures FIRST and fire `on_evict` only
        // once every removal is done — as `retain` does. Firing mid-drain would let a
        // panicking callback unwind with the remaining stamps already detached from
        // `self.keys` but their entries still in `self.map`, orphaning them: invisible to a
        // later sweep yet still counted by `len`.
        let mut removed = Vec::with_capacity(count);
        for stamped in expired {
            let key = stamped
                .key
                .expect("evicting: only artificial bounds are none");
            if let Some(entry) = self.map.remove(key.0.as_ref()) {
                removed.push((key, entry));
            }
        }
        self.evictions
            .fetch_add(removed.len() as u64, AtomicOrdering::Relaxed);
        if let Some(on_evict) = &self.on_evict {
            for (key, entry) in &removed {
                on_evict(key.0.as_ref(), &entry.value);
            }
        }
        count
    }

    /// Retain only entries that are unexpired and satisfy `keep`.
    ///
    /// Removes every entry that is already TTL-expired **or** for which `keep`
    /// returns `false` — expired entries are removed without consulting `keep`.
    /// `on_evict` is called and the eviction counter incremented for each removed
    /// entry. Entries stored with no expiry (a zero or overflowing TTL, see
    /// [`set_with`](Self::set_with)) never expire, so they are removed only when
    /// `keep` returns `false`.
    ///
    /// Returns the number of entries removed: the count folds together entries `keep`
    /// rejected and entries swept for having already expired, since expiry removal is
    /// unconditional regardless of what `keep` returns. `retain` is deliberately not
    /// `#[must_use]`: discarding the count is a legitimate and common use, matching
    /// existing bare `cache.retain(...);` call sites.
    ///
    /// Not to be confused with [`retain_latest`](Self::retain_latest), which is a
    /// *size trim*: it drops the next-to-expire entries until at most `count` remain
    /// (optionally also sweeping expired ones) and returns how many it dropped. This
    /// method is a *predicate filter plus an expiry sweep*: it consults `keep` for
    /// every live entry, ignores [`max_size`](TtlSortedCacheBuilder::max_size), and
    /// does not reorder the expiry index.
    ///
    /// This matches [`TtlCache::retain`](crate::TtlCache::retain) and
    /// [`LruTtlCache::retain`](crate::LruTtlCache::retain); the plain
    /// [`LruCache::retain`](crate::LruCache::retain) has no expiry dimension and
    /// removes solely on the predicate.
    pub fn retain<F: FnMut(&K, &V) -> bool>(&mut self, mut keep: F) -> usize {
        // Sample the clock once so every entry is judged against the same instant.
        let now = Instant::now();
        // Disjoint field borrows: `map.retain` takes `&mut self.map` while the closure
        // holds `&mut self.keys` plus shared borrows of the callback and counter.
        let keys = &mut self.keys;
        // Drain both structures first and fire `on_evict` only once the pass is over.
        // Firing mid-pass would let a panicking callback unwind between the index
        // removal and the map removal, leaving `keys` short of `map` permanently:
        // the orphaned entry would be invisible to `evict`/`retain_latest` (their
        // `pop_first` walk never reaches it) yet still counted by `len`.
        let removed: Vec<_> = self
            .map
            .extract_if(|key, entry| {
                if entry.is_expired_at(now) || !keep(key, &entry.value) {
                    // `as_stamped` rebuilds the exact `Stamped` that was inserted (same
                    // expiry, same `CacheArc` key), so this cannot leave a stale index
                    // entry that a later `pop_first` would miscount as a drop.
                    keys.remove(&entry.as_stamped());
                    true
                } else {
                    false
                }
            })
            .collect();
        let count = removed.len();
        self.evictions
            .fetch_add(count as u64, AtomicOrdering::Relaxed);
        if let Some(on_evict) = &self.on_evict {
            for (key, entry) in &removed {
                on_evict(key, &entry.value);
            }
        }
        count
    }

    /// Retain only the latest `count` values, dropping the next values to expire.
    /// If `evict`, then also evict values that have expired.
    /// Returns number of dropped items.
    ///
    /// This is a *size trim*, not a predicate filter: entries are chosen purely by
    /// expiry order until at most `count` remain. Use [`retain`](Self::retain) to keep
    /// entries by a `FnMut(&K, &V) -> bool` predicate (which also sweeps expired entries
    /// regardless of the predicate, but ignores [`max_size`](TtlSortedCacheBuilder::max_size)
    /// and returns a `usize` count of its own).
    pub fn retain_latest(&mut self, count: usize, evict: bool) -> usize {
        self.retain_latest_at(count, evict.then(Instant::now))
    }

    /// [`retain_latest`](Self::retain_latest) with the expiry sweep driven by an explicit
    /// cutoff: `Some(cutoff)` is the `evict = true` sweep, `None` disables it. Lets a caller
    /// that already sampled the clock reuse its own `now`.
    ///
    /// Like [`evict_at`](Self::evict_at) this walks the front of the expiry index instead of
    /// pre-counting a range. The old code took `max(retain_drop_count, expired_count)` and
    /// popped that many; since the expired entries are exactly a front prefix, popping while
    /// `dropped < retain_drop_count || (evict && front_is_expired)` removes the same set.
    fn retain_latest_at(&mut self, count: usize, cutoff: Option<Instant>) -> usize {
        let retain_drop_count = self.map.len().saturating_sub(count);
        if retain_drop_count == 0 {
            // No size trim to do: this is either a pure expiry sweep (where the old
            // `max(0, expired_count)` is just the sweep count) or a complete no-op that must
            // leave the index untouched.
            return match cutoff {
                Some(cutoff) => self.evict_at(cutoff),
                None => 0,
            };
        }

        let mut dropped = 0;
        while let Some(stamped) = self.keys.pop_first() {
            if dropped >= retain_drop_count {
                // Size trim satisfied; keep going only while the front is expired.
                let expired = match cutoff {
                    Some(cutoff) => matches!(stamped.expiry, Some(expiry) if expiry < cutoff),
                    None => false,
                };
                if !expired {
                    self.keys.insert(stamped);
                    break;
                }
            }
            // Invariant: same as evict() — None keys are sentinel-only.
            let key = stamped
                .key
                .expect("retaining: only artificial bounds are none");
            if let Some(entry) = self.map.remove(key.0.as_ref()) {
                self.evictions.fetch_add(1, AtomicOrdering::Relaxed);
                if let Some(on_evict) = &self.on_evict {
                    on_evict(key.0.as_ref(), &entry.value);
                }
            }
            dropped += 1;
        }
        dropped
    }

    /// Set k/v pair without running eviction logic, using the cache's default TTL.
    ///
    /// The entry is inserted first. If [`max_size`](TtlSortedCacheBuilder::max_size) was
    /// configured and the insertion pushes the map over it, the soonest-to-expire entry is
    /// trimmed to restore the bound; this size-limit enforcement runs on every `set`,
    /// independent of the `.evict()` opt-in. The `.set_with(..).evict()` opt-in controls
    /// only the separate expiry sweep (dropping entries already past their TTL), not
    /// size-limit enforcement; see [`set_with`](Self::set_with) for per-entry TTL
    /// overrides and the opt-in sweep.
    ///
    /// If computing the expiry instant overflows (a TTL on the order of hundreds of
    /// years), the entry is stored with no expiry (never expires), matching
    /// [`cache_set`](crate::Cached::cache_set) on the other TTL stores.
    pub fn set(&mut self, key: K, value: V) -> Option<V> {
        self.set_inner(key, value, None, false, false).0
    }

    /// Start building a `set` call with an optional per-entry TTL override and/or
    /// opt-in eviction, e.g. `cache.set_with(k, v).ttl(Duration::from_secs(5)).evict().set()`.
    ///
    /// The entry is inserted first. If [`max_size`](TtlSortedCacheBuilder::max_size) was
    /// specified and capacity is exceeded, the next-to-expire entry is dropped after
    /// insertion. The eviction callback fires after insertion, not before. The terminal
    /// [`.set()`](TtlSortedSetBuilder::set) returns any existing unexpired value that was
    /// replaced.
    ///
    /// If computing the expiry instant overflows (a TTL on the order of hundreds of
    /// years), the entry is stored with no expiry (never expires), matching
    /// [`cache_set`](crate::Cached::cache_set) on the other TTL stores.
    #[must_use = "set_with does nothing until .set() is called"]
    pub fn set_with(&mut self, key: K, value: V) -> TtlSortedSetBuilder<'_, K, V, S> {
        TtlSortedSetBuilder {
            cache: self,
            key,
            value,
            ttl: None,
            evict: false,
        }
    }

    /// Shared insertion routine for [`set_with`](Self::set_with) / [`set`](Self::set) and the
    /// `cache_get_or_set_with_mut` paths.
    ///
    /// When the effective TTL (explicit `ttl` arg or `self.ttl`) is zero, the entry is
    /// stored with `expiry = None` (never expires) rather than being given an immediate
    /// expiry. Zero TTL means "disable expiry" for new inserts, consistent with the other
    /// TTL stores. In the (practically unreachable) case where `now + ttl` exceeds
    /// `Instant`'s representable range — a TTL on the order of hundreds of years — the
    /// entry is likewise stored with `expiry = None`, matching the never-expires-on-overflow
    /// behavior of `TtlCache` / `LruTtlCache` and the sharded TTL stores.
    ///
    /// `skip_size_eviction` defers size-limit enforcement to the caller
    /// (`set_and_get_mut` must protect the just-inserted entry before evicting).
    ///
    /// Returns the displaced (unexpired) value plus the `Stamped` that was written to the
    /// expiry index. Handing the stamp back lets `set_and_get_mut` protect and re-find the
    /// entry it just inserted without cloning the key again or re-hashing it; the stamp holds
    /// the very same `CacheArc` handle the stored `Entry` does (an `Arc`, so no deep clone).
    fn set_inner(
        &mut self,
        key: K,
        value: V,
        ttl: Option<Duration>,
        evict: bool,
        skip_size_eviction: bool,
    ) -> (Option<V>, Stamped<K>) {
        let effective_ttl = ttl.unwrap_or(self.ttl);

        // Sample the clock ONCE for the whole operation: the new expiry, the displaced
        // entry's expiry check, and any eager sweep below are all judged against this
        // instant instead of taking two or three separate `Instant::now()` readings.
        let now = Instant::now();

        // A zero TTL means "never expires": store expiry = None. `checked_add`
        // returning `None` on overflow lands on the same never-expires representation.
        let expiry = if effective_ttl.is_zero() {
            None
        } else {
            now.checked_add(effective_ttl)
        };

        // `entry` rather than `insert`: on the occupied path the existing `Entry` already owns
        // a `CacheArc` for this key, so reusing it is a refcount bump instead of an allocation
        // plus a deep `K::clone`. Only the vacant path allocates, and it clones the key out of
        // `VacantEntry::key` (the caller's `key` is moved into the map by `insert`).
        let (arc_key, old) = match self.map.entry(key) {
            std::collections::hash_map::Entry::Occupied(mut occupied) => {
                let arc_key = occupied.get().key.clone();
                let old = occupied.insert(Entry {
                    expiry,
                    key: arc_key.clone(),
                    value,
                });
                (arc_key, Some(old))
            }
            std::collections::hash_map::Entry::Vacant(vacant) => {
                let arc_key = CacheArc::new(vacant.key().clone());
                vacant.insert(Entry {
                    expiry,
                    key: arc_key.clone(),
                    value,
                });
                (arc_key, None)
            }
        };

        let new_stamped = Stamped {
            expiry,
            key: Some(arc_key),
        };
        self.keys.insert(new_stamped.clone());
        // The displaced entry's stamp carries the same (equal) key, so it differs from the new
        // one exactly when the expiry changed — comparing the two instants avoids rebuilding
        // `old.as_stamped()` on the (common) unchanged-expiry path.
        if let Some(old) = &old
            && old.expiry != expiry
        {
            self.keys.remove(&old.as_stamped());
        }
        let old_value = match old {
            // A displaced expired value is filtered from the return (matching the get paths), so
            // it is dropped silently from the caller's view; fire `on_evict` and count an
            // eviction so cleanup and metrics stay consistent with the other removal paths.
            Some(entry) if entry.is_expired_at(now) => {
                // Count BEFORE notifying: a panicking callback must never leave
                // an entry removed-but-uncounted.
                self.evictions.fetch_add(1, AtomicOrdering::Relaxed);
                if let Some(on_evict) = &self.on_evict {
                    on_evict(entry.key.0.as_ref(), &entry.value);
                }
                None
            }
            Some(entry) => Some(entry.value),
            None => None,
        };

        // Size-limit eviction is skipped only when the caller explicitly requests it
        // (`skip_size_eviction`) — e.g. `set_and_get_mut` must guarantee the just-inserted
        // entry is still present to return `&mut V` safely, regardless of the entry's TTL.
        // The sweeps reuse `now` rather than re-reading the clock.
        if !skip_size_eviction {
            if let Some(size_limit) = self.size_limit {
                if self.map.len() > size_limit {
                    self.retain_latest_at(size_limit, evict.then_some(now));
                }
            } else if evict {
                let _ = self.evict_at(now);
            }
        }

        (old_value, new_stamped)
    }

    /// Insert `key`/`value` and return a mutable reference to the stored value.
    ///
    /// When [`max_size`](TtlSortedCacheBuilder::max_size) is configured the just-inserted
    /// entry is protected from eviction: other entries are evicted in TTL order to restore
    /// capacity. Used by the `cache_get_or_set_with_mut` family.
    fn set_and_get_mut(&mut self, key: K, value: V) -> &mut V {
        // `skip_size_eviction = true` defers size enforcement to the block below,
        // where we can protect the just-inserted entry. `set_inner` hands back the exact
        // `Stamped` it indexed, so the key is moved in (no extra `K::clone`) and the
        // protect/re-find steps below reuse that stamp instead of re-deriving it from a
        // second map lookup.
        let (_, protected) = self.set_inner(key, value, None, false, true);

        if let Some(size_limit) = self.size_limit
            && self.map.len() > size_limit
        {
            // Temporarily unlink the just-inserted entry from the expiry index so
            // `retain_latest` cannot select it for eviction. Other entries are
            // dropped in TTL order until the map is back within `size_limit`.
            // The stamp is restored afterward so the index stays consistent.
            self.keys.remove(&protected);
            self.retain_latest(size_limit, false);
            self.keys.insert(protected.clone());
        }

        // The stamp shares the stored entry's key `Arc`, so this borrows the key rather than
        // cloning it. The `&mut V` is tied to `&mut self`, not to `protected`.
        let key: &K = protected
            .key
            .as_ref()
            .expect("set_and_get_mut: set_inner always stamps a real key")
            .0
            .as_ref();
        &mut self
            .map
            .get_mut(key)
            .expect("set_and_get_mut: the protected eviction path guarantees the entry is present")
            .value
    }

    fn remove_expired_entry<Q>(&mut self, key: &Q)
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        if let Some(entry) = self.map.remove(key) {
            self.keys.remove(&entry.as_stamped());
            // Count BEFORE notifying: a panicking callback must never leave an
            // entry removed-but-uncounted.
            self.evictions.fetch_add(1, AtomicOrdering::Relaxed);
            if let Some(on_evict) = &self.on_evict {
                on_evict(entry.key.0.as_ref(), &entry.value);
            }
        }
    }

    /// Remove all entries and fire the `on_evict` callback for each one, incrementing the
    /// evictions counter.
    ///
    /// Unlike [`cache_clear`](crate::Cached::cache_clear) (which removes entries silently),
    /// this method invokes `on_evict` for every removed entry (whether or not they had expired)
    /// and increments `evictions`. If no `on_evict` callback was configured, it falls back to
    /// the plain `cache_clear`.
    pub fn cache_clear_with_on_evict(&mut self) {
        if self.on_evict.is_none() {
            return self.cache_clear();
        }
        let entries: Vec<(K, Entry<K, V>)> = self.map.drain().collect();
        self.keys.clear();
        let count = entries.len() as u64;
        if count > 0 {
            self.evictions.fetch_add(count, AtomicOrdering::Relaxed);
        }
        if let Some(on_evict) = &self.on_evict {
            for (_k, entry) in &entries {
                on_evict(entry.key.0.as_ref(), &entry.value);
            }
        }
    }
}

/// Builder returned by [`TtlSortedCache::set_with`] for chaining a per-entry TTL override
/// and/or opt-in eviction before performing the insertion.
///
/// Nothing is inserted until the terminal [`.set()`](Self::set) is called — the `#[must_use]`
/// attribute flags a builder that is constructed and dropped without ever calling it.
#[cfg_attr(docsrs, doc(cfg(feature = "time_stores")))]
#[must_use = "set_with does nothing until .set() is called"]
pub struct TtlSortedSetBuilder<'a, K, V, S = DefaultHashBuilder> {
    cache: &'a mut TtlSortedCache<K, V, S>,
    key: K,
    value: V,
    ttl: Option<Duration>,
    evict: bool,
}

impl<'a, K: Hash + Eq + Ord + Clone, V, S: BuildHasher> TtlSortedSetBuilder<'a, K, V, S> {
    /// Override the store's default TTL for this entry only.
    ///
    /// If computing the expiry instant overflows (a TTL on the order of hundreds of
    /// years), the entry is stored with no expiry (never expires), matching
    /// [`cache_set`](crate::Cached::cache_set) on the other TTL stores. A `Duration::ZERO`
    /// TTL also means "never expires" for this entry, matching the store's zero-TTL
    /// convention.
    ///
    /// Overrides any previously set ttl/ttl_secs/ttl_millis on this builder.
    pub fn ttl(mut self, ttl: Duration) -> Self {
        self.ttl = Some(ttl);
        self
    }

    /// Override the store's default TTL for this entry only, in whole seconds.
    /// Equivalent to `ttl(Duration::from_secs(secs))`.
    ///
    /// Overrides any previously set ttl/ttl_secs/ttl_millis on this builder.
    pub fn ttl_secs(self, secs: u64) -> Self {
        self.ttl(Duration::from_secs(secs))
    }

    /// Override the store's default TTL for this entry only, in milliseconds.
    /// Equivalent to `ttl(Duration::from_millis(millis))`.
    ///
    /// Overrides any previously set ttl/ttl_secs/ttl_millis on this builder.
    pub fn ttl_millis(self, millis: u64) -> Self {
        self.ttl(Duration::from_millis(millis))
    }

    /// Opt into running eviction logic after this insertion.
    ///
    /// The entry is inserted first. If [`max_size`](TtlSortedCacheBuilder::max_size) was
    /// specified and capacity is exceeded, the next-to-expire entry is dropped after
    /// insertion, firing `on_evict` and counting an eviction.
    pub fn evict(mut self) -> Self {
        self.evict = true;
        self
    }

    /// Perform the insertion. Returns any existing unexpired value that was replaced,
    /// or `None` if the key was absent or the replaced entry had already expired.
    pub fn set(self) -> Option<V> {
        self.cache
            .set_inner(self.key, self.value, self.ttl, self.evict, false)
            .0
    }
}

impl<K: Hash + Eq + Ord + Clone, V, S: BuildHasher> Cached<K, V> for TtlSortedCache<K, V, S> {
    type Error = std::convert::Infallible;

    fn cache_get<Q>(&mut self, key: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        // Two lookups on the hit path: the first checks expiry (releasing the borrow via
        // `.map`), the second returns the reference. A single-lookup approach is not possible
        // SAFELY in stable Rust because returning `&'1 V` from inside an `if let` block ties
        // the borrow to lifetime `'1`, which prevents `remove_entry` (a mutable borrow) even on
        // the non-returning path. Polonius (nightly) would fix this. It IS possible with
        // `unsafe`: `TtlCache::cache_get` (see `src/stores/ttl.rs`, the `&entry.value as
        // *const V` reborrow) collapses this to one lookup behind a documented SAFETY comment.
        let is_expired = match self.map.get(key) {
            None => {
                self.misses.increment();
                return None;
            }
            Some(entry) => entry.is_expired(),
        };

        if is_expired {
            self.misses.increment();
            self.remove_expired_entry(key);
            return None;
        }

        self.hits.increment();
        self.map.get(key).map(|e| &e.value)
    }

    fn cache_get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        let is_expired = match self.map.get(key) {
            None => {
                self.misses.increment();
                return None;
            }
            Some(entry) => entry.is_expired(),
        };

        if is_expired {
            self.misses.increment();
            self.remove_expired_entry(key);
            return None;
        }

        self.hits.increment();
        self.map.get_mut(key).map(|e| &mut e.value)
    }

    fn cache_set(&mut self, key: K, value: V) -> Option<V> {
        self.set_inner(key, value, None, false, false).0
    }

    fn cache_get_or_set_with_mut<F: FnOnce() -> V>(&mut self, key: K, f: F) -> &mut V {
        // Check liveness WITHOUT removing the entry, then run the factory, then replace on
        // success. This mirrors `TtlCache` / `LruTtlCache`: a factory panic leaves the stale
        // entry in place instead of dropping it (and does not fire `on_evict` prematurely).
        // The borrow from `map.get` ends with the `matches!`, so the counters/`get_mut` below
        // are free to borrow `self` again (the Polonius limitation, see `cache_get`).
        let live = matches!(self.map.get(&key), Some(entry) if !entry.is_expired());
        if live {
            self.hits.increment();
            return self
                .map
                .get_mut(&key)
                .map(|entry| &mut entry.value)
                // Invariant: the liveness check above confirmed the entry exists and is not
                // expired. No other code path removes it between the check and this get_mut.
                .expect("cache entry vanished");
        }
        self.misses.increment();
        // Miss or expired entry: build the value first. `set_and_get_mut` inserts it and, when
        // it displaces an expired entry, fires `on_evict` and counts one eviction (insert_inner).
        // It never drops the value (it saturates an unrepresentable TTL instead of erroring), so
        // this path is panic-free once `f()` has produced a value.
        self.set_and_get_mut(key, f())
    }

    fn cache_try_get_or_set_with_mut<F: FnOnce() -> Result<V, E>, E>(
        &mut self,
        key: K,
        f: F,
    ) -> Result<&mut V, E> {
        // Same structure as `cache_get_or_set_with_mut`: check liveness without removing, run
        // the factory, replace only on `Ok`. On `Err` the (expired or absent) entry is left
        // exactly as it was and `on_evict` does not fire, matching `TtlCache` / `LruTtlCache`.
        let live = matches!(self.map.get(&key), Some(entry) if !entry.is_expired());
        if live {
            self.hits.increment();
            return Ok(self
                .map
                .get_mut(&key)
                .map(|entry| &mut entry.value)
                // Invariant: same as cache_get_or_set_with_mut above.
                .expect("cache entry vanished"));
        }
        self.misses.increment();
        let value = f()?;
        // `set_and_get_mut` never drops the value, so this path is panic-free.
        Ok(self.set_and_get_mut(key, value))
    }

    fn cache_remove<Q>(&mut self, key: &Q) -> Option<V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        match self.map.remove(key) {
            None => None,
            Some(removed) => {
                let expired = removed.is_expired();
                self.keys.remove(&removed.as_stamped());
                // The stored key `Arc` derefs to `&K` directly: no clone is needed to hand the
                // callback a reference (and the clone previously ran even without a callback).
                // Count BEFORE notifying: a panicking callback must never leave an entry
                // removed-but-uncounted.
                self.evictions.fetch_add(1, AtomicOrdering::Relaxed);
                if let Some(on_evict) = &self.on_evict {
                    on_evict(removed.key.0.as_ref(), &removed.value);
                }
                if expired { None } else { Some(removed.value) }
            }
        }
    }

    fn cache_remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        match self.map.remove(key) {
            None => None,
            Some(removed) => {
                self.keys.remove(&removed.as_stamped());
                let stored_k = (*removed.key.0).clone();
                // Count BEFORE notifying: a panicking callback must never leave an
                // entry removed-but-uncounted.
                self.evictions.fetch_add(1, AtomicOrdering::Relaxed);
                if let Some(on_evict) = &self.on_evict {
                    on_evict(&stored_k, &removed.value);
                }
                Some((stored_k, removed.value))
            }
        }
    }

    fn cache_clear(&mut self) {
        // Inline rather than delegate to a `self.clear()` shim — the `Cached`
        // short alias `clear` defaults to `cache_clear`, so going through it
        // would be circular.
        self.map.clear();
        self.keys.clear();
    }

    fn cache_reset(&mut self) {
        // Entries are dropped in-place; `on_evict` is NOT called for cleared entries.
        // Use clear + shrink_to to avoid needing S: Clone to rebuild the HashMap.
        // Shrink back to the build-time preallocation hint, not to zero, so the
        // configured capacity survives a reset (CORE-8), matching `TtlCache`.
        self.map.clear();
        self.map.shrink_to(self.initial_capacity.unwrap_or(0));
        self.keys = BTreeSet::new();
        self.min_instant = Instant::now();
        self.cache_reset_metrics();
    }

    fn cache_reset_metrics(&mut self) {
        self.misses.reset();
        self.hits.reset();
        self.evictions.store(0, AtomicOrdering::Relaxed);
    }

    /// Reports raw entry count without sweeping; the count may include
    /// expired entries. Run [`evict`](TtlSortedCache::evict) or
    /// [`retain_latest`](TtlSortedCache::retain_latest) first for an accurate
    /// post-sweep count.
    fn cache_size(&self) -> usize {
        self.map.len()
    }

    fn cache_hits(&self) -> Option<u64> {
        Some(self.hits.load())
    }

    fn cache_misses(&self) -> Option<u64> {
        Some(self.misses.load())
    }

    fn cache_evictions(&self) -> Option<u64> {
        Some(self.evictions.load(AtomicOrdering::Relaxed))
    }

    fn cache_capacity(&self) -> Option<usize> {
        self.size_limit
    }

    /// Returns `true` if the key is present and its entry has not expired.
    ///
    /// Uses `cache_peek` internally: no hit/miss counters are updated and no
    /// recency or TTL refresh occurs. Expired entries report `false`.
    fn cache_contains<Q>(&mut self, k: &Q) -> bool
    where
        K: std::borrow::Borrow<Q>,
        Q: std::hash::Hash + Eq + ?Sized,
    {
        crate::CachedPeek::cache_peek(self, k).is_some()
    }
}

impl<K: Hash + Eq + Ord, V, S: BuildHasher> CachedIter<K, V> for TtlSortedCache<K, V, S> {
    fn iter<'a>(&'a self) -> impl Iterator<Item = (&'a K, &'a V)> + 'a
    where
        K: 'a,
        V: 'a,
    {
        // The clock is read per item rather than hoisted into a construction-time snapshot:
        // this iterator is lazy and may be held across a long consumer loop, where a stale
        // snapshot would report entries that have since expired as live.
        self.map.iter().filter_map(|(k, entry)| {
            if entry.is_expired() {
                None
            } else {
                Some((k, &entry.value))
            }
        })
    }
}

impl<K: Hash + Eq + Ord, V, S: BuildHasher> CacheTtl for TtlSortedCache<K, V, S> {
    /// Returns the currently configured TTL, or `None` when expiry is disabled.
    ///
    /// When `ttl` is `Duration::ZERO`, expiry is disabled: entries inserted while zero is
    /// set never expire (they are stored with `expiry = None`). This resolves a zero TTL to
    /// `None`, consistent with `TtlCache` and `LruTtlCache`, rather than reporting the raw
    /// `Some(Duration::ZERO)`.
    fn ttl(&self) -> Option<Duration> {
        // A zero TTL means expiry is disabled.
        if self.ttl.is_zero() {
            None
        } else {
            Some(self.ttl)
        }
    }
    /// Set the global TTL for future inserts, returning the previous value (or `None` if
    /// expiry was already disabled).
    ///
    /// A zero `Duration` disables expiry for **future** inserts: entries inserted while the TTL
    /// is zero are stored with `expiry = None` and never expire. Pre-existing entries keep their
    /// original expiry and still expire on schedule. This is consistent with the other TTL stores
    /// (`TtlCache`, `LruTtlCache`). To restore expiry, call `set_ttl` with a non-zero duration.
    fn set_ttl(&mut self, ttl: Duration) -> Option<Duration> {
        let old = self.ttl;
        self.ttl = ttl;
        if old.is_zero() { None } else { Some(old) }
    }
    /// Disable expiry for future inserts by setting the TTL to `Duration::ZERO`.
    ///
    /// Equivalent to `set_ttl(Duration::ZERO)`: entries inserted after this call never expire.
    /// Pre-existing entries keep their original expiry. Returns the previous TTL, or `None` if
    /// expiry was already disabled, matching `TtlCache` / `LruTtlCache`.
    fn unset_ttl(&mut self) -> Option<Duration> {
        let old = self.ttl;
        self.ttl = Duration::ZERO;
        if old.is_zero() { None } else { Some(old) }
    }
    /// `TtlSortedCache` does not refresh entries on hit; always returns `false`.
    fn refresh_on_hit(&self) -> bool {
        false
    }
    /// `TtlSortedCache` does not support refresh-on-hit; this is a no-op and always returns `false`.
    fn set_refresh_on_hit(&mut self, _refresh: bool) -> bool {
        false
    }
}

impl<K: Hash + Eq + Ord, V, S: BuildHasher> CachedPeek<K, V> for TtlSortedCache<K, V, S> {
    fn cache_peek<Q>(&self, key: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        self.map.get(key).and_then(|entry| {
            if entry.is_expired() {
                None
            } else {
                Some(&entry.value)
            }
        })
    }
}

impl<K: Hash + Eq + Ord, V, S: BuildHasher> CachedRead<K, V> for TtlSortedCache<K, V, S> {
    fn cache_get_read<Q>(&self, key: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        if let Some(value) = self.cache_peek(key) {
            self.hits.increment();
            Some(value)
        } else {
            self.misses.increment();
            None
        }
    }
}

impl<K: Hash + Eq + Ord + Clone, V: Clone, S: BuildHasher + Clone> CloneCached<K, V>
    for TtlSortedCache<K, V, S>
{
    fn cache_get_with_expiry_status<Q>(&mut self, k: &Q) -> (Option<V>, bool)
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        match self.map.get(k) {
            None => {
                self.misses.increment();
                (None, false)
            }
            Some(entry) if entry.is_expired() => {
                self.misses.increment();
                (Some(entry.value.clone()), true)
            }
            Some(entry) => {
                self.hits.increment();
                (Some(entry.value.clone()), false)
            }
        }
    }

    /// Peek at the entry (including expired entries) without any read side effects.
    ///
    /// Returns `(Some(v), true)` for an expired entry, `(Some(v), false)` for a live
    /// entry, and `(None, false)` when the key is absent. Does not update hit/miss
    /// counters or renew the TTL.
    fn cache_peek_with_expiry_status<Q>(&self, k: &Q) -> (Option<V>, bool)
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
        V: Clone,
    {
        match self.map.get(k) {
            None => (None, false),
            Some(entry) if entry.is_expired() => (Some(entry.value.clone()), true),
            Some(entry) => (Some(entry.value.clone()), false),
        }
    }
}

#[cfg(feature = "async_core")]
#[cfg_attr(docsrs, doc(cfg(feature = "async_core")))]
impl<K, V, S> CachedGetOrSetAsync<K, V> for TtlSortedCache<K, V, S>
where
    K: Hash + Eq + Ord + Clone + Send + Sync,
    V: Send,
    S: BuildHasher + Send,
{
    fn async_cache_get_or_set_with_mut<'a, F, Fut>(
        &'a mut self,
        k: K,
        f: F,
    ) -> impl Future<Output = &'a mut V> + Send + 'a
    where
        K: 'a,
        V: Send + 'a,
        F: FnOnce() -> Fut + Send + 'a,
        Fut: Future<Output = V> + Send + 'a,
    {
        async move {
            // Same shape as the sync `cache_get_or_set_with_mut`: check liveness in place
            // (one lookup) instead of going through `cache_get`, which costs two lookups of
            // its own before this method's `get_mut` — three on an async hit. As in the sync
            // version, an expired entry is left in place for `set_and_get_mut` to displace
            // (which fires `on_evict` and counts the eviction) rather than being swept before
            // the future runs, so a dropped/panicking future leaves the stale entry alone.
            let live = matches!(self.map.get(&k), Some(entry) if !entry.is_expired());
            if live {
                self.hits.increment();
                return self
                    .map
                    .get_mut(&k)
                    .map(|entry| &mut entry.value)
                    // Invariant: the liveness check above confirmed the entry is present and
                    // unexpired, and nothing removes it in between.
                    .expect("cache entry vanished");
            }
            self.misses.increment();
            // `set_and_get_mut` never drops the value, so this path is panic-free.
            let value = f().await;
            self.set_and_get_mut(k, value)
        }
    }

    fn async_cache_try_get_or_set_with_mut<'a, F, Fut, E>(
        &'a mut self,
        k: K,
        f: F,
    ) -> impl Future<Output = Result<&'a mut V, E>> + Send + 'a
    where
        K: 'a,
        V: Send + 'a,
        E: 'a,
        F: FnOnce() -> Fut + Send + 'a,
        Fut: Future<Output = Result<V, E>> + Send + 'a,
    {
        async move {
            // Same shape as `async_cache_get_or_set_with_mut` / the sync fallible sibling:
            // check liveness in place (one lookup) instead of going through `cache_get`, which
            // would sweep an expired entry (and fire `on_evict`) before the factory is even
            // polled. An expired entry is left in place for `set_and_get_mut` to displace on
            // `Ok` (which fires `on_evict` and counts the eviction) rather than being swept
            // up front, so a factory `Err` or a dropped/cancelled future leaves the stale entry
            // alone.
            let live = matches!(self.map.get(&k), Some(entry) if !entry.is_expired());
            if live {
                self.hits.increment();
                return Ok(self
                    .map
                    .get_mut(&k)
                    .map(|entry| &mut entry.value)
                    // Invariant: the liveness check above confirmed the entry is present and
                    // unexpired, and nothing removes it in between.
                    .expect("cache entry vanished"));
            }
            self.misses.increment();
            let value = f().await?;
            // `set_and_get_mut` never drops the value, so this path is panic-free.
            Ok(self.set_and_get_mut(k, value))
        }
    }
}

impl<K: std::hash::Hash + Eq + Ord + Clone, V, S: BuildHasher> CacheEvict
    for TtlSortedCache<K, V, S>
{
    fn evict(&mut self) -> usize {
        TtlSortedCache::evict(self)
    }
}

#[cfg(test)]
mod test {
    use crate::stores::TtlSortedCache;
    use crate::time::Duration;
    use crate::{CacheTtl, Cached, CachedExt, CachedRead};
    use std::cmp::Ordering as CmpOrdering;
    use std::hash::{Hash, Hasher};
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    #[test]
    fn cache_set_over_expired_returns_none_fires_on_evict_and_counts() {
        let fired = Arc::new(AtomicUsize::new(0));
        let fired2 = fired.clone();
        let mut c: TtlSortedCache<u32, u32> = TtlSortedCache::builder()
            .ttl(Duration::from_millis(20))
            .on_evict(move |_k: &u32, _v: &u32| {
                fired2.fetch_add(1, Ordering::Relaxed);
            })
            .build()
            .unwrap();
        c.cache_set(1, 100);
        std::thread::sleep(std::time::Duration::from_millis(60));
        // The previous value has expired: overwriting filters it (None), fires on_evict once,
        // and counts one eviction.
        assert_eq!(c.cache_set(1, 200), None);
        assert_eq!(c.cache_evictions(), Some(1));
        assert_eq!(fired.load(Ordering::Relaxed), 1);
        // Overwriting the now-live value returns it, no on_evict and no new eviction.
        assert_eq!(c.cache_set(1, 300), Some(200));
        assert_eq!(c.cache_evictions(), Some(1));
        assert_eq!(fired.load(Ordering::Relaxed), 1);
    }

    #[test]
    fn entry_is_expired_at_uses_at_or_after_boundary() {
        // TtlSortedCache aligns with TtlCache / LruTtlCache: an entry is expired at-or-after its
        // deadline (`now >= expiry`), not strictly-after. The real monotonic clock never yields a
        // deterministic `now == expiry` tie, so exercise the boundary through `is_expired_at`.
        use super::{CacheArc, Entry};
        use crate::time::Instant;

        let deadline = Instant::now();
        let entry = Entry {
            expiry: Some(deadline),
            key: CacheArc::new(1u32),
            value: 42u32,
        };

        // Exactly at the deadline: expired (at-or-after). Strictly-after would report `false`.
        assert!(
            entry.is_expired_at(deadline),
            "entry must be expired at exactly its deadline (at-or-after)"
        );
        // One tick before: still live.
        assert!(
            !entry.is_expired_at(deadline - Duration::from_nanos(1)),
            "entry must be live just before its deadline"
        );
        // A never-expiring entry (zero-TTL sentinel) is never expired.
        let never = Entry {
            expiry: None,
            key: CacheArc::new(2u32),
            value: 7u32,
        };
        assert!(!never.is_expired_at(deadline));
    }

    #[test]
    fn set_with_ttl_overflow_stores_never_expiring_entry() {
        // set_with(..).ttl(..) with a Duration that would overflow Instant bounds stores
        // the entry with no expiry (never expires), matching cache_set on the other TTL
        // stores. No error surface: TtlSortedCache's Cached::Error is Infallible.
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        // Duration::MAX overflows Instant::now().checked_add -> None -> never expires.
        let prev = cache.set_with(1u32, 42u32).ttl(Duration::MAX).set();
        assert_eq!(prev, None);
        assert_eq!(cache.cache_size(), 1);
        assert_eq!(cache.cache_get(&1u32), Some(&42u32));
        // The entry survives an explicit eviction sweep (it never expires).
        assert_eq!(cache.evict(), 0);
        assert_eq!(cache.cache_get(&1u32), Some(&42u32));
    }

    #[derive(Clone, Debug)]
    struct CountingKey {
        label: &'static str,
        hash_calls: Arc<AtomicUsize>,
    }

    impl CountingKey {
        fn new(label: &'static str) -> Self {
            Self {
                label,
                hash_calls: Arc::new(AtomicUsize::new(0)),
            }
        }
    }

    impl Hash for CountingKey {
        fn hash<H: Hasher>(&self, state: &mut H) {
            self.hash_calls.fetch_add(1, Ordering::Relaxed);
            self.label.hash(state);
        }
    }

    impl PartialEq for CountingKey {
        fn eq(&self, other: &Self) -> bool {
            self.label == other.label
        }
    }

    impl Eq for CountingKey {}

    impl PartialOrd for CountingKey {
        fn partial_cmp(&self, other: &Self) -> Option<CmpOrdering> {
            Some(self.cmp(other))
        }
    }

    impl Ord for CountingKey {
        fn cmp(&self, other: &Self) -> CmpOrdering {
            self.label.cmp(other.label)
        }
    }

    #[test]
    fn new_returns_ready_cache_respecting_ttl() {
        use crate::CacheTtl;
        let mut c: TtlSortedCache<u32, u32> = TtlSortedCache::new(Duration::from_millis(50));
        assert_eq!(CacheTtl::ttl(&c), Some(Duration::from_millis(50)));
        c.cache_set(1, 100);
        assert_eq!(c.cache_get(&1), Some(&100));
        std::thread::sleep(std::time::Duration::from_millis(100));
        assert_eq!(c.cache_get(&1), None, "entry must expire after ttl");
        // No size bound from new().
        assert_eq!(c.cache_capacity(), None);
    }

    #[test]
    #[should_panic(expected = "non-zero ttl")]
    fn new_zero_ttl_panics() {
        let _c: TtlSortedCache<u32, u32> = TtlSortedCache::new(Duration::ZERO);
    }

    #[test]
    fn ttl_secs_and_ttl_millis_set_duration() {
        use crate::CacheTtl;
        let c: TtlSortedCache<u32, u32> = TtlSortedCache::builder().ttl_secs(7).build().unwrap();
        assert_eq!(CacheTtl::ttl(&c), Some(Duration::from_secs(7)));

        let c: TtlSortedCache<u32, u32> =
            TtlSortedCache::builder().ttl_millis(250).build().unwrap();
        assert_eq!(CacheTtl::ttl(&c), Some(Duration::from_millis(250)));
    }

    #[test]
    fn ttl_setters_override_last_writer_wins() {
        use crate::CacheTtl;
        // ttl(secs=10) then ttl_secs(5) -> 5s
        let c: TtlSortedCache<u32, u32> = TtlSortedCache::builder()
            .ttl(Duration::from_secs(10))
            .ttl_secs(5)
            .build()
            .unwrap();
        assert_eq!(CacheTtl::ttl(&c), Some(Duration::from_secs(5)));

        // ttl_secs then ttl_millis -> the millis value
        let c: TtlSortedCache<u32, u32> = TtlSortedCache::builder()
            .ttl_secs(10)
            .ttl_millis(500)
            .build()
            .unwrap();
        assert_eq!(CacheTtl::ttl(&c), Some(Duration::from_millis(500)));
    }

    #[test]
    fn borrow_keys() {
        let mut cache = TtlSortedCache::builder()
            .ttl(Duration::from_millis(100))
            .initial_capacity(100)
            .build()
            .unwrap();
        cache.set(String::from("a"), "a");
        assert_eq!(cache.get("a").unwrap(), &"a");

        let mut cache = TtlSortedCache::builder()
            .ttl(Duration::from_millis(100))
            .initial_capacity(100)
            .build()
            .unwrap();
        cache.set(vec![0], "a");
        assert_eq!(cache.get([0].as_slice()).unwrap(), &"a");
    }

    #[test]
    fn cache_get_live_hit_increments_hits() {
        let key = CountingKey::new("live");
        let mut cache = TtlSortedCache::builder()
            .ttl(Duration::from_secs(60))
            .initial_capacity(1)
            .build()
            .unwrap();
        cache.set(key.clone(), 10);

        assert_eq!(cache.cache_get(&key), Some(&10));
        assert_eq!(cache.cache_hits(), Some(1));
        assert_eq!(cache.cache_misses(), Some(0));
        assert_eq!(cache.cache_size(), 1);
        assert_eq!(cache.keys.len(), 1);
    }

    #[test]
    fn cache_get_mut_live_hit_updates_value() {
        let key = CountingKey::new("live-mut");
        let mut cache = TtlSortedCache::builder()
            .ttl(Duration::from_secs(60))
            .initial_capacity(1)
            .build()
            .unwrap();
        cache.set(key.clone(), 10);

        let value = cache.cache_get_mut(&key).expect("entry should be live");
        *value = 11;

        assert_eq!(cache.cache_hits(), Some(1));
        assert_eq!(cache.cache_misses(), Some(0));
        assert_eq!(cache.cache_get(&key), Some(&11));
    }

    #[test]
    fn cache_get_expired_hit_removes_map_and_ttl_index() {
        let evicted = Arc::new(AtomicUsize::new(0));
        let evicted_clone = evicted.clone();
        let mut cache = TtlSortedCache::builder()
            .ttl(Duration::from_secs(60))
            .on_evict(move |k: &&str, v: &u32| {
                assert_eq!(*k, "expired");
                assert_eq!(*v, 10);
                evicted_clone.fetch_add(1, Ordering::Relaxed);
            })
            .build()
            .expect("cache should build");

        // Use a very short but non-zero TTL (zero now means "never expires").
        cache
            .set_with("expired", 10)
            .ttl(Duration::from_millis(1))
            .set();
        assert_eq!(cache.cache_size(), 1);
        assert_eq!(cache.keys.len(), 1);

        // Wait for the TTL to elapse before querying.
        std::thread::sleep(std::time::Duration::from_millis(20));

        assert_eq!(cache.cache_get(&"expired"), None);

        assert_eq!(cache.cache_size(), 0);
        assert_eq!(cache.keys.len(), 0);
        assert_eq!(cache.cache_hits(), Some(0));
        assert_eq!(cache.cache_misses(), Some(1));
        assert_eq!(cache.cache_evictions(), Some(1));
        assert_eq!(evicted.load(Ordering::Relaxed), 1);
    }

    #[test]
    fn cache_get_mut_expired_hit_removes_map_and_ttl_index() {
        let evicted = Arc::new(AtomicUsize::new(0));
        let evicted_clone = evicted.clone();
        let mut cache = TtlSortedCache::builder()
            .ttl(Duration::from_secs(60))
            .on_evict(move |k: &&str, v: &u32| {
                assert_eq!(*k, "expired-mut");
                assert_eq!(*v, 20);
                evicted_clone.fetch_add(1, Ordering::Relaxed);
            })
            .build()
            .expect("cache should build");

        // Use a very short but non-zero TTL (zero now means "never expires").
        cache
            .set_with("expired-mut", 20)
            .ttl(Duration::from_millis(1))
            .set();
        assert_eq!(cache.cache_size(), 1);
        assert_eq!(cache.keys.len(), 1);

        // Wait for the TTL to elapse before querying.
        std::thread::sleep(std::time::Duration::from_millis(20));

        assert_eq!(cache.cache_get_mut(&"expired-mut"), None);

        assert_eq!(cache.cache_size(), 0);
        assert_eq!(cache.keys.len(), 0);
        assert_eq!(cache.cache_hits(), Some(0));
        assert_eq!(cache.cache_misses(), Some(1));
        assert_eq!(cache.cache_evictions(), Some(1));
        assert_eq!(evicted.load(Ordering::Relaxed), 1);
    }

    #[test]
    fn kitchen_sink() {
        let mut cache = TtlSortedCache::builder()
            .ttl(Duration::from_millis(100))
            .initial_capacity(100)
            .build()
            .unwrap();
        assert_eq!(0, cache.evict());
        assert_eq!(0, cache.retain_latest(100, true));
        assert!(cache.get("a").is_none());

        cache.set("a".to_string(), "A".to_string());
        assert_eq!(cache.get("a"), Some("A".to_string()).as_ref());
        assert_eq!(cache.len(), 1);
        std::thread::sleep(Duration::from_millis(200));
        assert_eq!(1, cache.evict());
        assert!(cache.get("a").is_none());
        assert_eq!(cache.len(), 0);

        cache.set("a".to_string(), "A".to_string());
        assert_eq!(cache.get("a"), Some("A".to_string()).as_ref());
        assert_eq!(cache.len(), 1);
        std::thread::sleep(Duration::from_millis(200));
        assert_eq!(0, cache.retain_latest(1, false));
        // Expired-but-not-yet-evicted: use the non-mutating read so the entry
        // stays in the map (the next assertion verifies it's still counted).
        assert_eq!(cache.cache_get_read("a"), None);
        // in size until eviction
        assert_eq!(cache.len(), 1);
        assert_eq!(1, cache.retain_latest(1, true));
        assert!(cache.get("a").is_none());
        assert_eq!(cache.len(), 0);

        cache.set("a".to_string(), "a".to_string());
        cache.set("b".to_string(), "b".to_string());
        cache.set("c".to_string(), "c".to_string());
        cache.set("d".to_string(), "d".to_string());
        cache.set("e".to_string(), "e".to_string());
        assert_eq!(3, cache.retain_latest(2, false));
        assert_eq!(2, cache.len());
        assert_eq!(cache.get("a"), None);
        assert_eq!(cache.get("b"), None);
        assert_eq!(cache.get("c"), None);
        assert_eq!(cache.get("d"), Some("d".to_string()).as_ref());
        assert_eq!(cache.get("e"), Some("e".to_string()).as_ref());

        cache.set("a".to_string(), "a".to_string());
        cache.set("a".to_string(), "a".to_string());
        cache.set("b".to_string(), "b".to_string());
        cache.set("b".to_string(), "b".to_string());
        assert_eq!(4, cache.len());

        assert_eq!(2, cache.retain_latest(2, false));
        assert_eq!(cache.get("d"), None);
        assert_eq!(cache.get("e"), None);
        assert_eq!(cache.get("a"), Some("a".to_string()).as_ref());
        assert_eq!(cache.get("b"), Some("b".to_string()).as_ref());
        assert_eq!(2, cache.len());

        std::thread::sleep(Duration::from_millis(200));
        assert_eq!(cache.remove("a"), None);
        // trying to get something expired will expire values
        assert_eq!(1, cache.len());

        cache.set("a".to_string(), "a".to_string());
        assert_eq!(cache.remove("a"), Some("a".to_string()));
        // we haven't done anything to evict "b" so there's still one entry
        assert_eq!(1, cache.len());

        assert_eq!(1, cache.evict());
        assert_eq!(0, cache.len());

        // default ttl is 100ms
        cache
            .set_with("a".to_string(), "a".to_string())
            .ttl(Duration::from_millis(300))
            .set();
        std::thread::sleep(Duration::from_millis(200));
        assert_eq!(cache.get("a"), Some("a".to_string()).as_ref());
        assert_eq!(1, cache.len());

        std::thread::sleep(Duration::from_millis(200));
        cache
            .set_with("b".to_string(), "b".to_string())
            .ttl(Duration::from_millis(300))
            .evict()
            .set();
        // a should now be evicted
        assert_eq!(1, cache.len());
        assert_eq!(cache.get("a"), None);
    }

    #[test]
    fn set_max_size() {
        let mut cache = TtlSortedCache::builder()
            .ttl(Duration::from_millis(100))
            .initial_capacity(100)
            .build()
            .unwrap();
        cache.set_max_size(2);
        assert_eq!(0, cache.evict());
        assert_eq!(0, cache.retain_latest(100, true));
        assert!(cache.get("a").is_none());

        cache.set("a".to_string(), "A".to_string());
        assert_eq!(cache.get("a"), Some("A".to_string()).as_ref());
        assert_eq!(cache.len(), 1);
        cache.set("b".to_string(), "B".to_string());
        assert_eq!(cache.get("b"), Some("B".to_string()).as_ref());
        assert_eq!(cache.len(), 2);
        cache.set("c".to_string(), "C".to_string());
        assert_eq!(cache.len(), 2);
        assert_eq!(cache.get("b"), Some("B".to_string()).as_ref());
        assert_eq!(cache.get("c"), Some("C".to_string()).as_ref());
        assert_eq!(cache.get("a"), None);
    }

    #[test]
    fn capacity_returns_bound_not_live_size() {
        let cache: TtlSortedCache<u32, u32> = TtlSortedCache::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        assert_eq!(cache.capacity(), None);
        assert_eq!(cache.capacity(), cache.cache_capacity());

        let mut cache = TtlSortedCache::builder()
            .ttl(Duration::from_secs(60))
            .max_size(3)
            .build()
            .unwrap();
        assert_eq!(cache.capacity(), Some(3));
        assert_eq!(cache.capacity(), cache.cache_capacity());

        cache.cache_set(1, 10);
        cache.cache_set(2, 20);
        assert_eq!(
            cache.capacity(),
            Some(3),
            "capacity tracks the bound, not live entries"
        );
        assert_eq!(cache.len(), 2);

        cache.set_max_size(5);
        assert_eq!(cache.capacity(), Some(5));
        assert_eq!(cache.capacity(), cache.cache_capacity());
    }

    #[test]
    fn updating_existing_key_at_size_limit_does_not_evict_another_key() {
        let mut cache = TtlSortedCache::builder()
            .ttl(Duration::from_millis(1_000))
            .initial_capacity(2)
            .build()
            .unwrap();
        cache.set_max_size(2);

        cache.set("a".to_string(), "A".to_string());
        cache.set("b".to_string(), "B".to_string());
        assert_eq!(cache.len(), 2);

        assert_eq!(
            cache.set("a".to_string(), "A2".to_string()),
            Some("A".to_string())
        );
        assert_eq!(cache.len(), 2);
        assert_eq!(cache.get("a"), Some(&"A2".to_string()));
        assert_eq!(cache.get("b"), Some(&"B".to_string()));
        assert_eq!(cache.cache_evictions(), Some(0));
    }

    #[test]
    fn builder_rejects_zero_size_limit() {
        let cache = TtlSortedCache::<String, String>::builder()
            .ttl(Duration::from_millis(1_000))
            .max_size(0)
            .build();
        match cache {
            Ok(_) => panic!("zero size limit should fail"),
            Err(error) => assert!(
                matches!(error, crate::stores::BuildError::InvalidValue { .. }),
                "expected InvalidValue, got {error:?}"
            ),
        }
    }

    #[test]
    fn try_set_max_size_rejects_zero() {
        let mut cache = TtlSortedCache::<String, String>::builder()
            .ttl(Duration::from_millis(1_000))
            .build()
            .unwrap();
        assert_eq!(
            cache.try_set_max_size(0),
            Err(super::super::SetMaxSizeError::ZeroMaxSize)
        );
        assert_eq!(cache.try_set_max_size(5).unwrap(), None);
    }

    #[test]
    #[should_panic(expected = "max_size must be greater than zero")]
    fn set_max_size_zero_panics() {
        let mut cache = TtlSortedCache::<String, String>::builder()
            .ttl(Duration::from_millis(1_000))
            .build()
            .unwrap();
        cache.set_max_size(0);
    }

    // CORE-1: a capacity-overflowing `max_size` must return `Err(BuildError)`
    // from the fallible `try_reserve`, not abort, matching the LRU-family stores.
    #[test]
    fn build_rejects_capacity_overflowing_max_size() {
        let cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .max_size(usize::MAX)
            .build();
        match cache {
            Ok(_) => panic!("usize::MAX max_size should fail to allocate"),
            Err(error) => assert!(
                matches!(error, crate::stores::BuildError::InvalidValue { .. }),
                "expected InvalidValue, got {error:?}"
            ),
        }
    }

    #[test]
    fn build_rejects_capacity_overflowing_initial_capacity() {
        let cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .initial_capacity(usize::MAX)
            .build();
        assert!(
            matches!(cache, Err(crate::stores::BuildError::InvalidValue { .. })),
            "usize::MAX initial_capacity should return Err(InvalidValue)"
        );
    }

    // CORE-1: `set_max_size` grows on demand (no pre-reserve), so even a huge
    // bound cannot panic; the documented panic-free `try_set_max_size` holds.
    #[test]
    fn try_set_max_size_huge_does_not_panic() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        assert_eq!(cache.try_set_max_size(usize::MAX).unwrap(), None);
    }

    #[test]
    fn explicit_capacity_takes_precedence_over_max_size_preallocation() {
        // Regression for #266: an explicit, smaller `capacity` must not be defeated
        // by `max_size`'s `size + 1` preallocation (HashMap::reserve does not reduce
        // an existing allocation).
        let cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(300))
            .max_size(65_536)
            .initial_capacity(16)
            .build()
            .unwrap();
        // The backing map must not have taken the max_size path, which would reserve
        // for max_size + 1 (= 65_537) entries.
        assert!(
            cache.map.capacity() < 65_537,
            "expected the explicit initial_capacity(16) to take precedence, got {}",
            cache.map.capacity()
        );
        assert!(cache.map.capacity() >= 16);
        // The eviction bound still reflects max_size.
        assert_eq!(cache.size_limit, Some(65_536));
    }

    #[test]
    fn max_size_alone_preallocates() {
        // Without an explicit capacity, max_size still drives preallocation.
        let cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(300))
            .max_size(64)
            .build()
            .unwrap();
        assert!(cache.map.capacity() >= 65);
    }

    #[test]
    fn get_or_set_with_max_size_limit_short_ttl_does_not_panic() {
        // Regression: when the just-inserted entry expires before existing entries,
        // `retain_latest` must evict the existing entry, not the one we're returning.
        let mut cache = TtlSortedCache::builder()
            .ttl(Duration::from_millis(1))
            .build()
            .unwrap();
        cache.set_max_size(1);
        cache
            .set_with("long", 1u32)
            .ttl(Duration::from_secs(60))
            .set();
        // Must not panic; "long" should be evicted to make room for "short".
        let v = cache.cache_get_or_set_with("short", || 2u32);
        assert_eq!(*v, 2);
        // Size limit must be respected after the call.
        assert_eq!(cache.cache_size(), 1);
        // "short" is the entry that survived; "long" was evicted.
        assert_eq!(cache.cache_get("short"), Some(&2u32));
    }

    #[test]
    fn try_get_or_set_with_max_size_limit_short_ttl_does_not_panic() {
        // Regression: same scenario as `get_or_set_with_max_size_limit_short_ttl_does_not_panic`
        // but via the fallible `cache_try_get_or_set_with` path, which also routes through
        // `set_and_get_mut`.
        let mut cache = TtlSortedCache::builder()
            .ttl(Duration::from_millis(1))
            .build()
            .unwrap();
        cache.set_max_size(1);
        cache
            .set_with("long", 1u32)
            .ttl(Duration::from_secs(60))
            .set();
        let v: &mut u32 = cache
            .cache_try_get_or_set_with_mut("short", || Ok::<u32, ()>(2))
            .unwrap();
        assert_eq!(*v, 2);
        assert_eq!(cache.cache_size(), 1);
        assert_eq!(cache.cache_get("short"), Some(&2u32));
    }

    #[test]
    fn shared_ref_get_or_set_with_wrapper_delegates_to_mut() {
        // The `&V`-returning `cache_get_or_set_with` / `cache_try_get_or_set_with`
        // are provided as defaults that delegate to the `_mut` variants. Exercise
        // them directly (not the `_mut` methods) so the delegation stays covered.
        let mut cache: TtlSortedCache<&str, u32> = TtlSortedCache::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();

        let v: &u32 = cache.cache_get_or_set_with("a", || 1u32);
        assert_eq!(*v, 1);

        let v: &u32 = cache
            .cache_try_get_or_set_with("b", || Ok::<u32, ()>(2))
            .unwrap();
        assert_eq!(*v, 2);

        // Hit path: the closure must not run, and the stored value is returned by `&V`.
        let v: &u32 = cache.cache_get_or_set_with("a", || 99u32);
        assert_eq!(*v, 1);
        assert_eq!(cache.cache_size(), 2);
    }

    #[cfg(feature = "async")]
    #[tokio::test]
    async fn async_cache_get_or_set_with_max_size_limit_short_ttl_does_not_panic() {
        use crate::CachedGetOrSetAsync;
        let mut cache = TtlSortedCache::builder()
            .ttl(Duration::from_millis(1))
            .build()
            .unwrap();
        cache.set_max_size(1);
        cache
            .set_with("long", 1u32)
            .ttl(Duration::from_secs(60))
            .set();
        let v = cache
            .async_cache_get_or_set_with("short", || async { 2u32 })
            .await;
        assert_eq!(*v, 2);
        assert_eq!(cache.cache_size(), 1);
        // "long" was evicted by the size limit (not by TTL expiry); verify it is gone.
        // Asserting cache_get("short") would be racy: the 1ms TTL can expire between
        // the .await resumption and this line under a loaded CI runner.
        assert_eq!(
            cache.cache_get("long"),
            None,
            "long entry should have been evicted"
        );
    }

    #[test]
    fn cache_clear_with_on_evict_fires_for_all_entries() {
        let count = Arc::new(AtomicUsize::new(0));
        let count2 = count.clone();
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .on_evict(move |_k: &u32, _v: &u32| {
                count2.fetch_add(1, Ordering::Relaxed);
            })
            .build()
            .unwrap();
        cache.cache_set(1, 10);
        cache.cache_set(2, 20);
        cache.cache_set(3, 30);
        cache.cache_clear_with_on_evict();
        assert_eq!(cache.cache_size(), 0);
        assert_eq!(cache.keys.len(), 0);
        assert_eq!(count.load(Ordering::Relaxed), 3);
        assert_eq!(cache.cache_evictions(), Some(3));
    }

    #[test]
    fn cache_clear_does_not_fire_on_evict() {
        let count = Arc::new(AtomicUsize::new(0));
        let count2 = count.clone();
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .on_evict(move |_k: &u32, _v: &u32| {
                count2.fetch_add(1, Ordering::Relaxed);
            })
            .build()
            .unwrap();
        cache.cache_set(1, 10);
        cache.cache_set(2, 20);
        cache.cache_clear();
        assert_eq!(cache.cache_size(), 0);
        assert_eq!(
            count.load(Ordering::Relaxed),
            0,
            "cache_clear must not fire on_evict"
        );
    }

    #[test]
    fn cache_reset_preserves_configuration() {
        use std::sync::Arc;
        use std::sync::atomic::{AtomicU64, Ordering};

        let evicted = Arc::new(AtomicU64::new(0));
        let evicted_clone = evicted.clone();

        let mut cache = TtlSortedCache::<u8, u8>::builder()
            .ttl(Duration::from_secs(60))
            .max_size(2)
            .on_evict(move |_k: &u8, _v: &u8| {
                evicted_clone.fetch_add(1, Ordering::Relaxed);
            })
            .build()
            .expect("build failed");

        cache.cache_set(1, 1);
        cache.cache_set(2, 2);
        cache.cache_reset();
        assert_eq!(0, cache.cache_size(), "reset should clear all entries");

        // After reset, size_limit and on_evict must still be active.
        cache.cache_set(3, 3);
        cache.cache_set(4, 4);
        cache.cache_set(5, 5); // capacity-2 → evicts one entry
        assert_eq!(2, cache.cache_size(), "size limit should still be enforced");
        assert_eq!(
            1,
            evicted.load(Ordering::Relaxed),
            "on_evict should still fire after reset"
        );
    }

    // CORE-8: `cache_reset` shrinks back to the build-time preallocation hint,
    // not to zero, so the configured capacity survives a reset.
    #[test]
    fn cache_reset_preserves_initial_capacity() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .initial_capacity(128)
            .build()
            .unwrap();
        for i in 0..64 {
            cache.cache_set(i, i);
        }
        cache.cache_reset();
        assert!(
            cache.map.capacity() >= 128,
            "reset should retain the initial_capacity hint, got {}",
            cache.map.capacity()
        );
    }

    #[test]
    fn test_diagnostics_and_traits() {
        let mut cache = TtlSortedCache::builder()
            .ttl(Duration::from_secs(60))
            .max_size(3)
            .build()
            .unwrap();
        cache.cache_set(1, 100);
        cache.cache_set(2, 200);

        // Debug
        let debug_str = format!("{:?}", cache);
        assert!(debug_str.contains("TtlSortedCache"));
        assert!(debug_str.contains("ttl"));
        assert!(debug_str.contains("size_limit"));
        assert!(debug_str.contains("hits"));
        assert!(debug_str.contains("misses"));

        // Clone
        let mut cloned = cache.clone();
        assert_eq!(cloned.cache_get(&1), Some(&100));
        assert_eq!(cloned.cache_get(&2), Some(&200));

        // Builder build errors
        let builder = TtlSortedCache::<u32, u32>::builder();
        let built = builder.build();
        assert!(built.is_err()); // Missing required ttl

        let builder = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .max_size(0);
        let built = builder.build();
        assert!(built.is_err()); // Size limit 0 is invalid

        let builder = TtlSortedCache::<u32, u32>::builder().ttl(Duration::ZERO);
        let built = builder.build();
        assert!(built.is_err()); // Zero ttl is invalid
    }

    #[test]
    fn cache_remove_entry_returns_some_for_live_entry() {
        let mut c = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        c.cache_set(1u32, 100u32);
        let removed = c.cache_remove_entry(&1u32);
        assert_eq!(removed, Some((1u32, 100u32)));
        assert_eq!(c.cache_size(), 0);
    }

    #[test]
    fn cache_remove_entry_returns_some_for_expired_entry() {
        let mut c = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(50))
            .build()
            .unwrap();
        c.cache_set(1u32, 100u32);
        std::thread::sleep(std::time::Duration::from_millis(100));

        // cache_remove returns None for expired.
        assert_eq!(c.cache_remove(&1u32), None);

        // cache_remove_entry returns Some even for expired.
        c.cache_set(2u32, 200u32);
        std::thread::sleep(std::time::Duration::from_millis(100));
        let removed = c.cache_remove_entry(&2u32);
        assert_eq!(
            removed.expect("cache_remove_entry must return Some for expired entry"),
            (2u32, 200u32)
        );
    }

    #[test]
    fn cache_delete_returns_true_for_expired_entry() {
        let mut c = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(50))
            .build()
            .unwrap();
        c.cache_set(1u32, 100u32);
        std::thread::sleep(std::time::Duration::from_millis(100));
        assert!(
            c.cache_delete(&1u32),
            "cache_delete must return true even for expired entry"
        );
        assert!(
            !c.cache_delete(&1u32),
            "cache_delete returns false when absent"
        );
    }

    #[test]
    fn cache_remove_entry_fires_on_evict_for_expired() {
        let count = Arc::new(AtomicUsize::new(0));
        let count2 = count.clone();
        let mut c = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(50))
            .on_evict(move |_k, _v| {
                count2.fetch_add(1, Ordering::Relaxed);
            })
            .build()
            .unwrap();
        c.cache_set(1u32, 10u32);
        std::thread::sleep(std::time::Duration::from_millis(100));

        let _ = c.cache_remove_entry(&1u32);
        assert_eq!(
            count.load(Ordering::Relaxed),
            1,
            "on_evict fires for expired entries"
        );

        let _ = c.cache_remove_entry(&999u32);
        assert_eq!(count.load(Ordering::Relaxed), 1, "no fire for absent key");
    }

    #[test]
    fn cache_remove_entry_with_panicking_on_evict_still_counts_eviction() {
        // The entry is popped from the map and index and counted BEFORE `on_evict`
        // runs, so a panicking callback must not leave the removed entry uncounted.
        use std::panic::{AssertUnwindSafe, catch_unwind};
        let mut c = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .on_evict(|_k: &u32, _v: &u32| panic!("boom"))
            .build()
            .unwrap();
        c.cache_set(1u32, 10u32);
        let r = catch_unwind(AssertUnwindSafe(|| c.cache_remove_entry(&1u32)));
        assert!(r.is_err(), "on_evict should have panicked");
        assert_eq!(c.cache_get(&1u32), None, "entry must still be removed");
        assert_eq!(
            c.cache_evictions(),
            Some(1),
            "eviction must be counted even though on_evict panicked"
        );
    }

    #[test]
    fn cache_remove_with_panicking_on_evict_still_counts_eviction() {
        use std::panic::{AssertUnwindSafe, catch_unwind};
        let mut c = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .on_evict(|_k: &u32, _v: &u32| panic!("boom"))
            .build()
            .unwrap();
        c.cache_set(1u32, 10u32);
        let r = catch_unwind(AssertUnwindSafe(|| c.cache_remove(&1u32)));
        assert!(r.is_err(), "on_evict should have panicked");
        assert_eq!(c.cache_get(&1u32), None, "entry must still be removed");
        assert_eq!(
            c.cache_evictions(),
            Some(1),
            "eviction must be counted even though on_evict panicked"
        );
    }

    #[test]
    fn cache_get_lazy_sweep_with_panicking_on_evict_still_counts_eviction() {
        // `remove_expired_entry` (the lazy-sweep path used by `cache_get`/`cache_get_mut`)
        // removes the entry from the map and index and counts the eviction BEFORE
        // `on_evict` runs.
        use std::panic::{AssertUnwindSafe, catch_unwind};
        let mut c = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(20))
            .on_evict(|_k: &u32, _v: &u32| panic!("boom"))
            .build()
            .unwrap();
        c.cache_set(1u32, 10u32);
        std::thread::sleep(std::time::Duration::from_millis(80));
        let r = catch_unwind(AssertUnwindSafe(|| {
            let _ = c.cache_get(&1u32);
        }));
        assert!(r.is_err(), "on_evict should have panicked");
        assert_eq!(
            c.cache_evictions(),
            Some(1),
            "eviction must be counted even though on_evict panicked"
        );
    }

    #[test]
    fn cache_set_over_expired_with_panicking_on_evict_still_counts_eviction() {
        // `set_inner`'s displaced-expired-value branch counts BEFORE notifying.
        use std::panic::{AssertUnwindSafe, catch_unwind};
        let mut c = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(20))
            .on_evict(|_k: &u32, _v: &u32| panic!("boom"))
            .build()
            .unwrap();
        c.cache_set(1u32, 10u32);
        std::thread::sleep(std::time::Duration::from_millis(80));
        let r = catch_unwind(AssertUnwindSafe(|| c.cache_set(1u32, 20u32)));
        assert!(r.is_err(), "on_evict should have panicked");
        assert_eq!(
            c.cache_evictions(),
            Some(1),
            "eviction must be counted even though on_evict panicked"
        );
    }

    #[test]
    fn cache_remove_entry_absent_returns_none() {
        let mut c = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        assert_eq!(c.cache_remove_entry(&42u32), None);
    }

    #[test]
    fn cache_remove_entry_increments_eviction_counter() {
        let mut c = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(10))
            .build()
            .unwrap();
        c.cache_set(1u32, 10u32);
        std::thread::sleep(std::time::Duration::from_millis(100));
        let before = c.cache_evictions().expect("evictions are always tracked");
        let _ = c.cache_remove_entry(&1u32); // expired but present — must increment
        let _ = c.cache_remove_entry(&999u32); // absent — must not increment
        assert_eq!(
            c.cache_evictions().expect("evictions are always tracked") - before,
            1,
            "cache_remove_entry must increment evictions for present key only"
        );
    }

    // ── Item 3: set_ttl(0) = "never expires" behavioral tests ─────────────

    /// Zero TTL at insert time means entries NEVER expire (not "expire immediately").
    #[test]
    fn set_ttl_zero_entries_never_expire() {
        use crate::CacheTtl;
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(50))
            .build()
            .unwrap();
        // Switch to zero TTL before inserting.
        cache.set_ttl(Duration::ZERO);
        cache.cache_set(1u32, 10u32);
        // Wait well past the original 50ms TTL.
        std::thread::sleep(std::time::Duration::from_millis(150));
        // Entry must still be present (never expires).
        assert_eq!(
            cache.cache_get(&1u32),
            Some(&10u32),
            "entry inserted with zero TTL must never expire"
        );
        // ttl() resolves the zero TTL to None (expiry disabled), like the sibling stores.
        assert_eq!(CacheTtl::ttl(&cache), None);
    }

    /// Switching set_ttl to zero only affects entries inserted AFTER the change.
    /// Pre-existing finite-expiry entries still expire on their original schedule.
    #[test]
    fn set_ttl_zero_only_affects_future_inserts() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(80))
            .build()
            .unwrap();
        // Insert with the current finite TTL.
        cache.cache_set(1u32, 100u32);
        // Switch to zero TTL (never-expires) for future inserts.
        cache.set_ttl(Duration::ZERO);
        cache.cache_set(2u32, 200u32);
        // Wait past the finite TTL for key 1.
        std::thread::sleep(std::time::Duration::from_millis(150));
        // Key 1 (finite TTL) must be expired.
        assert_eq!(
            cache.cache_get(&1u32),
            None,
            "pre-existing finite-TTL entry must expire"
        );
        // Key 2 (inserted with zero TTL = never expires) must still be present.
        assert_eq!(
            cache.cache_get(&2u32),
            Some(&200u32),
            "entry inserted with zero TTL must never expire"
        );
    }

    /// Under size pressure, never-expiring entries (None expiry) are evicted LAST —
    /// after all finite-expiry entries have been dropped.
    #[test]
    fn set_ttl_zero_never_expire_entries_evicted_last_under_size_pressure() {
        // Build with max_size = 2.
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(10))
            .max_size(2)
            .build()
            .unwrap();

        // Insert one never-expiring entry.
        cache.set_ttl(Duration::ZERO);
        cache.cache_set(1u32, 10u32);

        // Insert two finite-TTL entries (these must be evicted before the never-expiring one).
        cache.set_ttl(Duration::from_millis(500));
        cache.cache_set(2u32, 20u32);
        cache.cache_set(3u32, 30u32);
        // At this point the cache has 3 entries and max_size = 2; key 1 (never-expiring, None
        // expiry, sorts greatest) must be the survivor along with the later finite entry.
        // Actually, retain_latest evicts the soonest-expiring first: key 2 and key 3 have
        // Some(expiry) and key 1 has None (greatest). So one of key 2/3 was evicted, and
        // key 1 (never-expires) survives.
        assert_eq!(cache.cache_size(), 2, "max_size must be enforced");
        assert_eq!(
            cache.cache_get(&1u32),
            Some(&10u32),
            "never-expiring entry must survive size eviction"
        );

        // Now insert one more to push out the remaining finite-expiry entry.
        cache.cache_set(4u32, 40u32);
        assert_eq!(cache.cache_size(), 2);
        assert_eq!(
            cache.cache_get(&1u32),
            Some(&10u32),
            "never-expiring entry must still survive"
        );
    }

    /// unset_ttl is equivalent to set_ttl(Duration::ZERO): future inserts never expire.
    #[test]
    fn unset_ttl_makes_future_inserts_never_expire() {
        use crate::CacheTtl;
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(50))
            .build()
            .unwrap();
        cache.unset_ttl();
        assert_eq!(
            CacheTtl::ttl(&cache),
            None,
            "unset_ttl disables expiry, so ttl() resolves to None"
        );
        cache.cache_set(1u32, 99u32);
        std::thread::sleep(std::time::Duration::from_millis(120));
        assert_eq!(
            cache.cache_get(&1u32),
            Some(&99u32),
            "entry inserted after unset_ttl must never expire"
        );
    }

    /// Evict must not sweep never-expiring (None expiry) entries.
    #[test]
    fn evict_does_not_remove_never_expiring_entries() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(20))
            .build()
            .unwrap();
        // Insert a finite-TTL entry.
        cache.cache_set(1u32, 10u32);
        // Switch to zero TTL and insert a never-expiring entry.
        cache.set_ttl(Duration::ZERO);
        cache.cache_set(2u32, 20u32);
        // Wait for the finite entry to expire.
        std::thread::sleep(std::time::Duration::from_millis(80));
        let evicted = cache.evict();
        // Only the finite-TTL entry should be swept.
        assert_eq!(
            evicted, 1,
            "evict must sweep only expired finite-TTL entries"
        );
        assert_eq!(cache.cache_size(), 1, "never-expiring entry must remain");
        assert_eq!(cache.cache_get(&2u32), Some(&20u32));
    }

    /// A `Drop` panic mid-sweep must not orphan entries. In the no-callback branch of
    /// `evict_at`, the whole expired prefix is detached from `self.keys` up front (one
    /// `split_off`); the map rows are then removed one at a time. If a value's (or key's)
    /// `Drop` panics before every row has left `self.map`, the not-yet-removed rows are
    /// orphaned: their stamps are already gone from `self.keys`, so a later sweep never
    /// reaches them, yet `len` still counts them. The fix drains `self.map` fully into a
    /// local `Vec` and lets the values drop only after the drain, matching the callback
    /// branch, so `self.map` and `self.keys` stay in lockstep and the eviction counter
    /// reflects exactly what was pulled from the map — even across a panicking `Drop`.
    #[test]
    fn evict_no_callback_keeps_map_and_keys_in_lockstep_on_drop_panic() {
        use std::panic::{AssertUnwindSafe, catch_unwind};

        // A value whose `Drop` panics only when armed. Exactly ONE instance is armed so
        // the unwinding drop of the drain `Vec` (a slice drop continues dropping the
        // remaining elements after one panics) never double-panics into a process abort.
        struct PanicOnDrop {
            armed: bool,
        }
        impl Drop for PanicOnDrop {
            fn drop(&mut self) {
                if self.armed {
                    panic!("PanicOnDrop::drop fired");
                }
            }
        }

        // No `on_evict` configured, so `evict()` takes the no-callback branch of `evict_at`.
        let mut cache: TtlSortedCache<u32, PanicOnDrop> = TtlSortedCache::builder()
            .ttl(Duration::from_millis(5))
            .build()
            .unwrap();

        // Insert the armed value FIRST: sequential inserts give ascending expiry stamps and
        // `Stamped` orders by (expiry, key), so key 0 is the earliest in the sweep and its
        // `Drop` runs before the two later rows have left `self.map`. On the pre-fix
        // per-iteration-drop code that panic orphans keys 1 and 2 in `self.map` while their
        // stamps are already gone from `self.keys`.
        cache.set_with(0u32, PanicOnDrop { armed: true }).set();
        cache.set_with(1u32, PanicOnDrop { armed: false }).set();
        cache.set_with(2u32, PanicOnDrop { armed: false }).set();
        assert_eq!(cache.map.len(), 3);
        assert_eq!(cache.keys.len(), 3);

        // Let the whole 5ms-TTL prefix expire, then sweep. The armed `Drop` panics mid-sweep.
        std::thread::sleep(std::time::Duration::from_millis(40));
        let result = catch_unwind(AssertUnwindSafe(|| {
            let _ = cache.evict();
        }));
        assert!(
            result.is_err(),
            "the armed value's Drop must have panicked during the sweep"
        );

        // Lockstep is the core property: whatever left `self.keys` must also have left
        // `self.map`. Pre-fix this failed (map.len() == 2 vs keys.len() == 0: two orphans).
        assert_eq!(
            cache.map.len(),
            cache.keys.len(),
            "map and keys must stay in lockstep across a Drop panic (no orphaned rows)"
        );
        // Every entry had expired, so the sweep drained the whole prefix from both.
        assert_eq!(
            cache.map.len(),
            0,
            "all expired entries must have left the map"
        );
        assert_eq!(cache.cache_size(), 0);
        // The counter is incremented before the drain `Vec` drops, so it reflects exactly
        // the three rows pulled from the map. Pre-fix the batched `fetch_add` after the loop
        // was skipped by the panic, leaving it at 0.
        assert_eq!(
            cache.cache_evictions(),
            Some(3),
            "eviction counter must match the rows actually removed from the map"
        );
    }

    /// `set_with(..).ttl(..)` called with an EXPLICIT `Duration::ZERO` (not the cache-level
    /// `set_ttl`) must store `expiry = None` (never expires), not `Some(now)`
    /// (immediate). The cache's default TTL stays finite the whole time.
    #[test]
    fn set_with_ttl_explicit_zero_never_expires() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(20))
            .build()
            .unwrap();
        // Explicit zero TTL on this one entry — default ttl remains 20ms.
        cache.set_with(1u32, 10u32).ttl(Duration::ZERO).set();
        // The entry's internal expiry must be None (never), not Some(now).
        assert!(
            cache
                .map
                .get(&1u32)
                .expect("entry present")
                .expiry
                .is_none(),
            "explicit Duration::ZERO must store expiry = None (never expires)"
        );
        // Wait far past the default 20ms TTL.
        std::thread::sleep(std::time::Duration::from_millis(80));
        assert_eq!(
            cache.cache_get(&1u32),
            Some(&10u32),
            "entry inserted with explicit zero TTL must never expire"
        );
        // A sibling inserted with the finite default TTL must still expire.
        cache.cache_set(2u32, 20u32);
        std::thread::sleep(std::time::Duration::from_millis(80));
        assert_eq!(
            cache.cache_get(&2u32),
            None,
            "finite-TTL sibling must expire"
        );
        assert_eq!(cache.cache_get(&1u32), Some(&10u32));
    }

    /// `set_with(..).ttl(..).evict()` with explicit `Duration::ZERO` also stores `None`,
    /// and the never-expiring entry is not swept by the eviction pass it triggers.
    #[test]
    fn set_with_ttl_evict_explicit_zero_never_expires_and_survives_evict() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(10))
            .build()
            .unwrap();
        // A finite, soon-to-expire entry.
        cache.cache_set(1u32, 10u32);
        std::thread::sleep(std::time::Duration::from_millis(40));
        // Insert a never-expiring entry AND run the eviction pass in the same call.
        cache
            .set_with(2u32, 20u32)
            .ttl(Duration::ZERO)
            .evict()
            .set();
        assert!(
            cache
                .map
                .get(&2u32)
                .expect("entry present")
                .expiry
                .is_none(),
            "explicit zero TTL must be None"
        );
        // The expired finite entry was swept; the never-expiring one survives.
        assert_eq!(cache.cache_get(&1u32), None, "expired entry swept by evict");
        assert_eq!(
            cache.cache_get(&2u32),
            Some(&20u32),
            "never-expiring entry must survive its own evict pass"
        );
    }

    /// `set_with(k, v).set()` with no `.ttl()`/`.evict()` calls must behave exactly like
    /// plain `set`: same default TTL, same displaced-value return, no eviction sweep.
    #[test]
    fn set_with_default_matches_plain_set() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(200))
            .build()
            .unwrap();

        // First insert via the builder with no overrides: no previous value.
        assert_eq!(cache.set_with(1u32, 10u32).set(), None);
        assert_eq!(cache.cache_get(&1u32), Some(&10u32));

        // Overwriting the still-live entry returns the displaced value, matching `set`.
        assert_eq!(cache.set_with(1u32, 11u32).set(), Some(10u32));
        assert_eq!(cache.cache_get(&1u32), Some(&11u32));

        // The entry uses the cache's default TTL (no override was applied): it expires
        // after the configured 200ms, exactly like a plain `set`.
        std::thread::sleep(std::time::Duration::from_millis(260));
        assert_eq!(
            cache.cache_get(&1u32),
            None,
            "set_with with no .ttl() override must use the cache's default TTL"
        );
    }

    /// `.ttl(d)` overrides the store's default TTL for that single entry: a shorter
    /// override expires before the cache default would, and a longer override survives
    /// past the point a default-TTL sibling has already expired.
    #[test]
    fn set_with_ttl_overrides_default_ttl() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(500))
            .build()
            .unwrap();

        // Shorter override: expires well before the 500ms default would.
        cache
            .set_with(1u32, 10u32)
            .ttl(Duration::from_millis(20))
            .set();
        std::thread::sleep(std::time::Duration::from_millis(80));
        assert_eq!(
            cache.cache_get(&1u32),
            None,
            "a shorter .ttl() override must expire before the cache default TTL"
        );

        // Longer override: still live after the cache's own default would have expired
        // a plain entry (proving the per-entry override, not the default, is in effect).
        cache
            .set_with(2u32, 20u32)
            .ttl(Duration::from_secs(60))
            .set();
        cache.cache_set(3u32, 30u32); // default-TTL sibling, 500ms
        std::thread::sleep(std::time::Duration::from_millis(600));
        assert_eq!(
            cache.cache_get(&3u32),
            None,
            "default-TTL sibling must have expired by now"
        );
        assert_eq!(
            cache.cache_get(&2u32),
            Some(&20u32),
            "a longer .ttl() override must survive past the default TTL window"
        );
    }

    /// `.evict()` opts into running the size-limit eviction pass after insertion: once the
    /// bound is exceeded, the next-to-expire entry is dropped as part of the `.set()` call
    /// that requested it. Without `.evict()`, `set_with` still enforces `size_limit`
    /// (size-limit enforcement is unconditional in `set_inner`; `.evict()` only affects the
    /// TTL-sweep-when-no-size-limit path), so this test also checks the no-size-limit case
    /// via `evict()` triggering a plain expiry sweep.
    #[test]
    fn set_with_evict_triggers_eviction() {
        // Case 1: no size_limit configured — `.evict()` runs a TTL sweep as part of `.set()`.
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(10))
            .build()
            .unwrap();
        cache.cache_set(1u32, 10u32);
        std::thread::sleep(std::time::Duration::from_millis(40));
        assert_eq!(cache.cache_evictions(), Some(0));
        // Insert a second entry and opt into eviction: the expired entry (key 1) must be
        // swept as part of this call.
        cache.set_with(2u32, 20u32).evict().set();
        assert_eq!(
            cache.cache_evictions(),
            Some(1),
            ".evict() must run the TTL sweep as part of set_with(..).set()"
        );

        // Case 2: with a size_limit configured, exceeding it evicts the next-to-expire
        // entry regardless of `.evict()`; migrated from the historical `set_evict` coverage
        // (kitchen_sink's "a"/"b" scenario), isolated here for the builder specifically.
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .max_size(1)
            .build()
            .unwrap();
        cache.set_with(1u32, 10u32).set();
        assert_eq!(cache.cache_size(), 1);
        cache.set_with(2u32, 20u32).evict().set();
        assert_eq!(
            cache.cache_size(),
            1,
            "size_limit must still cap entries when inserting via set_with(..).evict()"
        );
        assert_eq!(cache.cache_get(&1u32), None, "next-to-expire entry evicted");
        assert_eq!(cache.cache_get(&2u32), Some(&20u32));
    }

    /// The terminal `.set()` returns the displaced unexpired value on overwrite (`Some`),
    /// and `None` when the previous entry was absent or had already expired.
    #[test]
    fn set_with_set_returns_displaced_value() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(50))
            .build()
            .unwrap();

        // Absent key: None.
        assert_eq!(cache.set_with(1u32, 10u32).set(), None);

        // Overwrite of a live entry: Some(previous value).
        assert_eq!(cache.set_with(1u32, 11u32).set(), Some(10u32));

        // Let the entry expire, then overwrite: the expired value is filtered to None.
        std::thread::sleep(std::time::Duration::from_millis(100));
        assert_eq!(
            cache.set_with(1u32, 12u32).set(),
            None,
            "overwriting an expired entry must return None, not the stale value"
        );
        assert_eq!(cache.cache_get(&1u32), Some(&12u32));
    }

    /// `retain_latest` over a MIX of never-expires (`None`) and finite (`Some`) entries:
    /// finite entries are popped first (soonest-expiry order); `None` entries are retained
    /// last regardless of insertion order. Verified across several `count` values.
    #[test]
    fn retain_latest_keeps_never_expiring_entries_last() {
        // Insertion order deliberately interleaves never/finite to prove that ordering,
        // not insertion order, decides eviction.
        fn fresh() -> TtlSortedCache<u32, u32> {
            let mut cache = TtlSortedCache::<u32, u32>::builder()
                .ttl(Duration::from_secs(60))
                .build()
                .unwrap();
            // finite (soonest)
            cache.set_ttl(Duration::from_millis(100));
            cache.cache_set(1u32, 10u32);
            // never
            cache.set_ttl(Duration::ZERO);
            cache.cache_set(2u32, 20u32);
            // finite (later than key 1)
            cache.set_ttl(Duration::from_secs(60));
            cache.cache_set(3u32, 30u32);
            // never
            cache.set_ttl(Duration::ZERO);
            cache.cache_set(4u32, 40u32);
            cache
        }

        // count = 2: the two finite entries (1, 3) are dropped, both nevers (2, 4) kept.
        let mut cache = fresh();
        let dropped = cache.retain_latest(2, false);
        assert_eq!(dropped, 2);
        assert_eq!(cache.cache_get(&1u32), None, "soonest finite dropped");
        assert_eq!(cache.cache_get(&3u32), None, "later finite dropped");
        assert_eq!(cache.cache_get(&2u32), Some(&20u32), "never-expires kept");
        assert_eq!(cache.cache_get(&4u32), Some(&40u32), "never-expires kept");

        // count = 3: only the soonest finite (key 1) is dropped; key 3 and both nevers kept.
        let mut cache = fresh();
        let dropped = cache.retain_latest(3, false);
        assert_eq!(dropped, 1);
        assert_eq!(cache.cache_get(&1u32), None, "soonest finite dropped first");
        assert_eq!(cache.cache_get(&3u32), Some(&30u32));
        assert_eq!(cache.cache_get(&2u32), Some(&20u32));
        assert_eq!(cache.cache_get(&4u32), Some(&40u32));

        // count = 1: both finite dropped, then ONE never must be dropped. The surviving
        // entry must be a never-expires entry (key 2 or key 4), never a finite one.
        let mut cache = fresh();
        let dropped = cache.retain_latest(1, false);
        assert_eq!(dropped, 3);
        assert_eq!(cache.cache_size(), 1);
        assert_eq!(cache.cache_get(&1u32), None);
        assert_eq!(cache.cache_get(&3u32), None);
        let survivor_is_never =
            cache.cache_get(&2u32).is_some() || cache.cache_get(&4u32).is_some();
        assert!(
            survivor_is_never,
            "the last-retained entry must be a never-expires entry, not a finite one"
        );

        // count = 0: everything dropped.
        let mut cache = fresh();
        let dropped = cache.retain_latest(0, false);
        assert_eq!(dropped, 4);
        assert_eq!(cache.cache_size(), 0);
    }

    /// Max-size eviction with never-expires and finite entries interleaved in insertion
    /// order: finite entries are always evicted before never-expires entries, regardless
    /// of when the never-expires entries were inserted.
    #[test]
    fn max_size_eviction_evicts_finite_before_never_interleaved() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .max_size(3)
            .build()
            .unwrap();
        // Insert a never-expires entry FIRST (oldest by insertion order).
        cache.set_ttl(Duration::ZERO);
        cache.cache_set(1u32, 10u32);
        // Then finite entries.
        cache.set_ttl(Duration::from_secs(30));
        cache.cache_set(2u32, 20u32);
        cache.cache_set(3u32, 30u32);
        assert_eq!(cache.cache_size(), 3);
        // A 4th finite insert exceeds max_size=3 -> evict the soonest-expiring (a finite one).
        cache.cache_set(4u32, 40u32);
        assert_eq!(cache.cache_size(), 3);
        assert_eq!(
            cache.cache_get(&1u32),
            Some(&10u32),
            "the oldest-inserted never-expires entry must not be evicted"
        );
        // The evicted one must be a finite entry (key 2 was the soonest of the finites).
        assert_eq!(cache.cache_get(&2u32), None, "soonest finite evicted");
        // Push more finite inserts; the never-expires entry must keep surviving.
        cache.cache_set(5u32, 50u32);
        cache.cache_set(6u32, 60u32);
        assert_eq!(cache.cache_size(), 3);
        assert_eq!(
            cache.cache_get(&1u32),
            Some(&10u32),
            "never-expires entry survives repeated finite-driven eviction"
        );
    }

    /// `cache_get_or_set_with` when the cache TTL is zero: the just-inserted entry is
    /// retrievable immediately and never expires (stored with expiry = None).
    #[test]
    fn get_or_set_with_zero_ttl_inserts_never_expiring_entry() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(10))
            .build()
            .unwrap();
        cache.set_ttl(Duration::ZERO);
        // Miss path computes and inserts; value retrievable immediately.
        let v = cache.cache_get_or_set_with(1u32, || 42u32);
        assert_eq!(*v, 42);
        assert!(
            cache
                .map
                .get(&1u32)
                .expect("entry present")
                .expiry
                .is_none(),
            "zero-ttl get_or_set must store expiry = None"
        );
        // Persists well past the former 10ms TTL.
        std::thread::sleep(std::time::Duration::from_millis(60));
        assert_eq!(
            cache.cache_get(&1u32),
            Some(&42u32),
            "zero-ttl get_or_set entry must never expire"
        );
        // Hit path: closure must not run.
        let v = cache.cache_get_or_set_with(1u32, || 999u32);
        assert_eq!(*v, 42, "existing never-expiring entry returned on hit");
    }

    /// `cache_try_get_or_set_with` when the cache TTL is zero: same contract via the
    /// fallible path. The entry is retrievable immediately and never expires.
    #[test]
    fn try_get_or_set_with_zero_ttl_inserts_never_expiring_entry() {
        let mut cache = TtlSortedCache::<&str, u32>::builder()
            .ttl(Duration::from_millis(10))
            .build()
            .unwrap();
        cache.set_ttl(Duration::ZERO);
        let v: &u32 = cache
            .cache_try_get_or_set_with("k", || Ok::<u32, ()>(7))
            .unwrap();
        assert_eq!(*v, 7);
        assert!(
            cache.map.get("k").expect("entry present").expiry.is_none(),
            "zero-ttl try_get_or_set must store expiry = None"
        );
        std::thread::sleep(std::time::Duration::from_millis(60));
        assert_eq!(
            cache.cache_get("k"),
            Some(&7u32),
            "zero-ttl try_get_or_set entry must never expire"
        );
    }

    // --- custom hasher tests ---

    #[test]
    fn custom_hasher_get_set_round_trip() {
        use crate::stores::Cached;
        use std::collections::hash_map::RandomState;
        let mut c = TtlSortedCache::<u32, u32>::builder()
            .ttl_secs(60)
            .hasher(RandomState::new())
            .build()
            .unwrap();
        assert_eq!(c.cache_set(1, 100), None);
        assert_eq!(c.cache_set(2, 200), None);
        assert_eq!(c.cache_get(&1), Some(&100));
        assert_eq!(c.cache_get(&2), Some(&200));
        assert_eq!(c.cache_hits(), Some(2));
        assert_eq!(c.cache_misses(), Some(0));
        assert_eq!(c.cache_get(&99), None);
        assert_eq!(c.cache_misses(), Some(1));
    }

    #[test]
    fn default_constructor_still_works() {
        use crate::stores::Cached;
        let mut c: TtlSortedCache<u32, u32> = TtlSortedCache::new(Duration::from_secs(60));
        c.cache_set(1, 10);
        assert_eq!(c.cache_get(&1), Some(&10));
    }

    #[test]
    fn custom_hasher_respects_ttl_expiry() {
        use crate::stores::Cached;
        use std::collections::hash_map::RandomState;
        let mut c = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(50))
            .hasher(RandomState::new())
            .build()
            .unwrap();
        c.cache_set(1, 10);
        assert_eq!(c.cache_get(&1), Some(&10));
        std::thread::sleep(std::time::Duration::from_millis(100));
        // After TTL, entry should expire (lazy removal on cache_get).
        assert_eq!(c.cache_get(&1), None, "entry must expire after ttl");
    }

    #[test]
    fn builder_initial_capacity_method_exists_and_preallocates() {
        // Verifies the renamed builder method: initial_capacity() sets a preallocation hint.
        let cache = TtlSortedCache::<u32, u32>::builder()
            .ttl_secs(60)
            .initial_capacity(32)
            .build()
            .unwrap();
        // The backing map must have at least the requested capacity.
        assert!(cache.map.capacity() >= 32);
    }

    // ── Adversarial coverage for the `set_with` builder (outside-in review) ─────

    /// `.ttl()` combined with a size cap: a shorter per-entry override moves an entry
    /// to the FRONT of the eviction order even though it was inserted after entries using
    /// the (longer) cache default TTL. This proves eviction ordering is driven by the
    /// effective (overridden) expiry, not by the cache-level default or insertion order.
    #[test]
    fn set_with_ttl_override_shorter_than_existing_evicts_first_under_size_cap() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .max_size(2)
            .build()
            .unwrap();

        // key 1: very short override, inserted FIRST.
        cache
            .set_with(1u32, 10u32)
            .ttl(Duration::from_millis(5))
            .set();
        // key 2: cache default (60s), inserted SECOND.
        cache.set_with(2u32, 20u32).set();
        assert_eq!(cache.cache_size(), 2, "at cap, no eviction yet");

        // key 3: also cache default. Exceeding the cap must evict the entry with the
        // soonest effective expiry — key 1 (short override) — not key 2, even though
        // key 1 was inserted earlier and key 2 uses the (longer) cache default.
        cache.set_with(3u32, 30u32).evict().set();
        assert_eq!(cache.cache_size(), 2, "size cap must still be enforced");
        assert_eq!(
            cache.cache_get(&1u32),
            None,
            "shorter .ttl() override must be evicted first despite earlier insertion"
        );
        assert_eq!(cache.cache_get(&2u32), Some(&20u32));
        assert_eq!(cache.cache_get(&3u32), Some(&30u32));
    }

    /// `.ttl(Duration::ZERO)` (never-expires) interacts with size-cap eviction ordering
    /// through the BUILDER path specifically (not `set_ttl` + plain `set`): a never-expiring
    /// entry set via `.ttl(Duration::ZERO)` must be evicted LAST, after finite entries —
    /// even a finite entry using a *shorter-than-default* override inserted later.
    #[test]
    fn set_with_ttl_zero_vs_dated_entries_eviction_order_under_size_cap() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .max_size(2)
            .build()
            .unwrap();

        // key 1: never-expires, via the builder's `.ttl(Duration::ZERO)`.
        cache.set_with(1u32, 10u32).ttl(Duration::ZERO).set();
        // key 2: finite, shorter than the cache default.
        cache
            .set_with(2u32, 20u32)
            .ttl(Duration::from_secs(30))
            .set();
        // key 3: finite, cache default (60s) — plain `set_with` with no `.ttl()` override.
        // This unconditionally trims to the cap (size-limit enforcement is unconditional
        // in `set_inner`), evicting the soonest-to-expire live entry: key 2.
        cache.set_with(3u32, 30u32).set();

        assert_eq!(cache.cache_size(), 2, "size cap must be enforced");
        assert_eq!(
            cache.cache_get(&1u32),
            Some(&10u32),
            "never-expiring entry set via the builder must survive size eviction"
        );
        assert_eq!(
            cache.cache_get(&2u32),
            None,
            "the shorter finite entry must be evicted before the never-expiring one"
        );
        assert_eq!(cache.cache_get(&3u32), Some(&30u32));
    }

    /// Displaced-value semantics of the terminal `.set()` across every entry state
    /// (absent, live, expired), each exercised WITH `.evict()` chained. The eviction
    /// pass must not change what `.set()` returns for the just-overwritten key — that
    /// return value reflects only the entry displaced at THIS key, computed before the
    /// (possibly unrelated) eviction sweep runs.
    #[test]
    fn set_with_evict_returns_displaced_value_in_all_states() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(50))
            .build()
            .unwrap();

        // Absent key, `.evict()` chained: None, and the (no-op) eviction pass does not error.
        assert_eq!(cache.set_with(1u32, 10u32).evict().set(), None);

        // Live key, `.evict()` chained: displaced value returned regardless of the sweep.
        assert_eq!(cache.set_with(1u32, 11u32).evict().set(), Some(10u32));
        assert_eq!(cache.cache_get(&1u32), Some(&11u32));

        // Let it expire, then overwrite with `.evict()` chained: displaced value is
        // filtered to None (matching the non-evict path), not the stale value.
        std::thread::sleep(std::time::Duration::from_millis(100));
        assert_eq!(
            cache.set_with(1u32, 12u32).evict().set(),
            None,
            "overwriting an expired entry must return None even with .evict() chained"
        );
        assert_eq!(cache.cache_get(&1u32), Some(&12u32));
    }

    /// A just-inserted entry can itself be selected for size-limit eviction if it is the
    /// soonest-to-expire entry once the cap is exceeded — proving eviction runs strictly
    /// AFTER insertion (the code checks `map.len() > size_limit` post-insert), not before.
    /// If eviction ran before insertion, the cap would be violated (2 entries with a
    /// cap of 1). `.set()`'s displaced-value return (`None`, a new key) does not reflect
    /// this immediate self-eviction — a real gotcha for callers relying on the return value
    /// alone to know whether their write "stuck".
    #[test]
    fn set_with_evict_may_evict_the_entry_just_inserted() {
        let events = Arc::new(std::sync::Mutex::new(Vec::<(u32, u32)>::new()));
        let events2 = events.clone();
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .max_size(1)
            .on_evict(move |k: &u32, v: &u32| {
                events2.lock().unwrap().push((*k, *v));
            })
            .build()
            .unwrap();

        // Long-lived entry occupies the sole capacity slot.
        cache.set_with(1u32, 100u32).set();
        assert_eq!(cache.cache_size(), 1);
        assert_eq!(cache.cache_evictions(), Some(0));

        // Insert a NEW key with a much shorter TTL than the existing entry. Post-insert the
        // map has 2 entries against a cap of 1; the soonest-to-expire (the entry just
        // inserted) is evicted immediately — its own insertion is what triggered the check.
        let displaced = cache
            .set_with(2u32, 200u32)
            .ttl(Duration::from_millis(1))
            .evict()
            .set();
        assert_eq!(
            displaced, None,
            "the terminal .set() return reflects only same-key displacement, not self-eviction"
        );

        assert_eq!(cache.cache_size(), 1, "size cap must still be enforced");
        assert_eq!(
            cache.cache_get(&1u32),
            Some(&100u32),
            "the long-lived entry must survive"
        );
        assert_eq!(
            cache.cache_get(&2u32),
            None,
            "the just-inserted, soonest-to-expire entry must have been evicted"
        );

        // on_evict fired exactly once, for the evicted key/value — proving the callback
        // observes the entry as already inserted-and-then-removed, not skipped pre-insert.
        let fired = events.lock().unwrap().clone();
        assert_eq!(
            fired,
            vec![(2u32, 200u32)],
            "on_evict must fire exactly once, for the just-inserted key that was evicted"
        );
        assert_eq!(cache.cache_evictions(), Some(1));
    }

    /// `on_evict` fires via the builder path for a size-limit eviction with the correct
    /// (k, v) of the evicted (displaced-by-cap) entry, and the eviction counter reflects
    /// exactly one eviction — isolating the callback-content assertion (as distinct from
    /// `set_with_evict_triggers_eviction`, which only checks the counter/survivorship).
    #[test]
    fn set_with_evict_on_evict_fires_with_correct_kv_for_size_eviction() {
        let events = Arc::new(std::sync::Mutex::new(Vec::<(u32, u32)>::new()));
        let events2 = events.clone();
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .max_size(1)
            .on_evict(move |k: &u32, v: &u32| {
                events2.lock().unwrap().push((*k, *v));
            })
            .build()
            .unwrap();

        cache.set_with(1u32, 111u32).set();
        assert!(events.lock().unwrap().is_empty());

        // key 2 uses the default (longer-lived by construction order) TTL; key 1 is the
        // sole occupant and thus the only candidate to evict when the cap is exceeded.
        cache.set_with(2u32, 222u32).evict().set();

        let fired = events.lock().unwrap().clone();
        assert_eq!(
            fired,
            vec![(1u32, 111u32)],
            "on_evict must report the evicted entry's own key/value, not the new one"
        );
        assert_eq!(cache.cache_evictions(), Some(1));
        assert_eq!(cache.cache_get(&2u32), Some(&222u32));
    }

    /// Assert the `map` / `keys` lockstep invariant of `TtlSortedCache`: the expiry-ordered
    /// `BTreeSet` index holds exactly one `Stamped` per stored map entry, each rebuildable
    /// via `Entry::as_stamped`, and never a `key: None` sentinel (which `evict()` /
    /// `retain_latest()` would `expect`-panic on).
    fn assert_index_lockstep<K, V, S>(cache: &TtlSortedCache<K, V, S>, ctx: &str)
    where
        K: Hash + Eq + Ord + std::fmt::Debug,
    {
        assert_eq!(
            cache.keys.len(),
            cache.map.len(),
            "{ctx}: BTreeSet index length must equal map length"
        );
        for stamped in &cache.keys {
            assert!(
                stamped.key.is_some(),
                "{ctx}: only artificial range bounds may have a None key"
            );
        }
        for (k, entry) in &cache.map {
            assert!(
                cache.keys.contains(&entry.as_stamped()),
                "{ctx}: map entry {k:?} is missing from the BTreeSet index"
            );
        }
    }

    #[test]
    fn retain_keeps_btreeset_index_in_lockstep_with_map() {
        // A mixed population: normal TTL entries plus never-expiring ones (zero-TTL path
        // stores `expiry = None`, which sorts last in the index).
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        for k in 0u32..6 {
            cache.set(k, k * 10);
        }
        // 100/101 never expire.
        cache.set_with(100u32, 1000u32).ttl(Duration::ZERO).set();
        cache.set_with(101u32, 1010u32).ttl(Duration::ZERO).set();
        assert_index_lockstep(&cache, "before retain");

        // Drop the odd keys (including a never-expiring one) from a live population.
        cache.retain(|k, _v| k % 2 == 0);

        assert_index_lockstep(&cache, "after predicate retain");
        assert_eq!(cache.map.len(), 4, "0, 2, 4 and 100 survive, nothing else");
        assert!(cache.map.contains_key(&100u32));
        assert!(!cache.map.contains_key(&101u32));
    }

    #[test]
    fn retain_index_lockstep_when_expired_entries_are_swept() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(20))
            .build()
            .unwrap();
        cache.set(1u32, 10u32);
        cache.set(2u32, 20u32);
        // Never expires, so it must survive the expiry sweep.
        cache.set_with(3u32, 30u32).ttl(Duration::ZERO).set();
        std::thread::sleep(std::time::Duration::from_millis(60));
        // Long-lived entry added after the sleep.
        cache
            .set_with(4u32, 40u32)
            .ttl(Duration::from_secs(60))
            .set();
        assert_index_lockstep(&cache, "before expiry retain");

        // Keep-everything predicate: only the expired entries may be removed.
        cache.retain(|_k, _v| true);

        assert_index_lockstep(&cache, "after expiry retain");
        assert_eq!(cache.map.len(), 2);
        assert!(cache.map.contains_key(&3u32), "never-expiring entry stays");
        assert!(cache.map.contains_key(&4u32), "live entry stays");

        // A stale index entry would make `pop_first` count a drop for a key that is no
        // longer in the map; the swept entries must not be counted again here.
        assert_eq!(cache.evict(), 0, "no stale index entries left to sweep");
        assert_index_lockstep(&cache, "after post-retain evict");
    }

    /// `retain` deliberately ignores `size_limit`, so the cap is restored by the *next*
    /// insert. That insert goes through `retain_latest`, which pops the expiry index: a
    /// stale stamp left behind by `retain` would be popped first, counted as a drop even
    /// though its map entry is gone, and leave the cache over its cap.
    #[test]
    fn retain_index_lockstep_when_a_size_limited_insert_follows() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .max_size(3)
            .build()
            .unwrap();
        for k in 1u32..=3 {
            cache
                .set_with(k, k * 10)
                .ttl(Duration::from_secs(10 * u64::from(k)))
                .set();
        }
        assert_index_lockstep(&cache, "before retain");

        // Drop the soonest-to-expire entry -- the one a stale stamp would make the
        // phantom victim of the next size-limit eviction.
        cache.retain(|k, _v| *k != 1);
        assert_index_lockstep(&cache, "after retain");
        assert_eq!(cache.map.len(), 2, "retain does not enforce size_limit");

        // Back to the cap exactly: still no eviction (the check is `len > size_limit`).
        cache
            .set_with(4u32, 40u32)
            .ttl(Duration::from_secs(40))
            .set();
        assert_eq!(cache.map.len(), 3);
        assert_index_lockstep(&cache, "at the cap");

        // Over the cap: the victim must be key 2, the soonest-to-expire *survivor*.
        cache
            .set_with(5u32, 50u32)
            .ttl(Duration::from_secs(50))
            .set();
        assert_eq!(cache.map.len(), 3, "the cap is restored by the insert");
        assert!(
            !cache.map.contains_key(&2u32),
            "the soonest-to-expire survivor is the victim"
        );
        assert!(cache.map.contains_key(&3u32));
        assert!(cache.map.contains_key(&4u32));
        assert!(cache.map.contains_key(&5u32));
        assert_index_lockstep(&cache, "after size-limit eviction");
    }

    /// `set_and_get_mut` temporarily unlinks the just-inserted `Stamped` from the index
    /// before running `retain_latest`, then relinks it. Interleaving that protected
    /// eviction path with `retain` must leave the index in lockstep and evict the correct
    /// victim.
    #[test]
    fn retain_index_lockstep_with_protected_get_or_set_with_mut() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .max_size(2)
            .build()
            .unwrap();
        cache
            .set_with(1u32, 10u32)
            .ttl(Duration::from_secs(10))
            .set();
        cache
            .set_with(2u32, 20u32)
            .ttl(Duration::from_secs(20))
            .set();
        cache.retain(|k, _v| *k != 1);
        assert_index_lockstep(&cache, "after retain");

        // Below the cap: the protected-eviction block is skipped entirely.
        assert_eq!(*cache.cache_get_or_set_with_mut(3u32, || 30), 30);
        assert_index_lockstep(&cache, "after unprotected insert");

        // Over the cap: the just-inserted key 4 is unlinked, so key 2 (soonest to
        // expire of the rest) is the victim, and key 4's stamp must be relinked.
        {
            let v = cache.cache_get_or_set_with_mut(4u32, || 40);
            *v += 1;
        }
        assert_eq!(cache.map.len(), 2);
        assert!(cache.map.contains_key(&4u32), "the new entry is protected");
        assert!(!cache.map.contains_key(&2u32));
        assert_index_lockstep(&cache, "after protected eviction");

        // The relinked stamp must be usable by a later index-driven trim.
        assert_eq!(cache.retain_latest(0, false), 2);
        assert!(cache.map.is_empty());
        assert_index_lockstep(&cache, "after full trim");
    }

    /// Same protected path, reached through the fallible factory. An `Err` factory must
    /// not touch either structure.
    #[test]
    fn retain_index_lockstep_with_protected_try_get_or_set_with_mut() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .max_size(2)
            .build()
            .unwrap();
        cache
            .set_with(1u32, 10u32)
            .ttl(Duration::from_secs(10))
            .set();
        cache
            .set_with(2u32, 20u32)
            .ttl(Duration::from_secs(20))
            .set();
        cache.retain(|k, _v| *k != 1);

        let err: Result<&mut u32, &str> = cache.cache_try_get_or_set_with_mut(9u32, || Err("no"));
        assert_eq!(err, Err("no"));
        assert_eq!(cache.map.len(), 1, "a failed factory inserts nothing");
        assert_index_lockstep(&cache, "after failed factory");

        let ok: Result<&mut u32, &str> = cache.cache_try_get_or_set_with_mut(3u32, || Ok(30));
        assert_eq!(ok, Ok(&mut 30));
        let ok: Result<&mut u32, &str> = cache.cache_try_get_or_set_with_mut(4u32, || Ok(40));
        assert_eq!(ok, Ok(&mut 40));
        assert_eq!(cache.map.len(), 2);
        assert!(cache.map.contains_key(&4u32));
        assert!(!cache.map.contains_key(&2u32), "key 2 expires soonest");
        assert_index_lockstep(&cache, "after protected eviction");
    }

    /// `Clone` copies `map` and `keys` separately, so a lockstep violation introduced by
    /// `retain` would be duplicated silently into the clone.
    #[test]
    fn retain_index_lockstep_survives_clone() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        for k in 0u32..6 {
            cache
                .set_with(k, k * 10)
                .ttl(Duration::from_secs(10 + u64::from(k)))
                .set();
        }
        cache.set_with(100u32, 1000u32).ttl(Duration::ZERO).set();
        cache.retain(|k, _v| k % 2 == 0);

        let mut clone = cache.clone();
        assert_index_lockstep(&clone, "clone of a retained cache");
        assert_eq!(clone.map.len(), cache.map.len());

        // The clone's index must drive its own trims correctly: 0, 2, 4 and the
        // never-expiring 100 survived, so trimming to 1 drops exactly three.
        assert_eq!(clone.retain_latest(1, false), 3);
        assert!(
            clone.map.contains_key(&100u32),
            "the never-expiring entry sorts last and is kept"
        );
        assert_index_lockstep(&clone, "clone after trim");
        // The original is untouched by the clone's trim.
        assert_eq!(cache.map.len(), 4);
        assert_index_lockstep(&cache, "original after clone trim");

        // A retain on the clone must not disturb the original either.
        clone.retain(|_k, _v| false);
        assert!(clone.map.is_empty());
        assert_eq!(cache.map.len(), 4);
        assert_index_lockstep(&cache, "original after clone retain");
    }

    /// A predicate that rejects everything must drain both structures to zero, leaving a
    /// reusable cache (not one whose index still holds phantom stamps).
    #[test]
    fn retain_rejecting_everything_drains_both_structures() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        for k in 0u32..4 {
            cache.set(k, k);
        }
        cache.set_with(100u32, 100u32).ttl(Duration::ZERO).set();

        cache.retain(|_k, _v| false);
        assert!(cache.map.is_empty());
        assert!(
            cache.keys.is_empty(),
            "the expiry index must be drained with the map"
        );
        assert_index_lockstep(&cache, "after draining retain");
        assert_eq!(cache.evict(), 0);
        assert_eq!(cache.retain_latest(0, true), 0);

        // Retaining an empty cache is a no-op under either predicate.
        cache.retain(|_k, _v| false);
        cache.retain(|_k, _v| true);
        assert_index_lockstep(&cache, "after empty-cache retain");

        // And the cache still works.
        cache.set(7u32, 77u32);
        assert_index_lockstep(&cache, "after reinsert");
        assert_eq!(cache.cache_get(&7u32), Some(&77u32));
    }

    /// A panicking `on_evict` must not desynchronize the map from the expiry index.
    /// `retain` therefore drains both structures before firing any callback: unwinding
    /// out of a callback that ran mid-pass would strand entries in `map` with no
    /// matching `keys` stamp, making them unreachable to `evict` / `retain_latest`
    /// (whose `pop_first` walk never sees them) while `len` still counts them.
    #[test]
    fn retain_on_evict_panic_leaves_the_index_in_lockstep() {
        // Panic on the first callback only, so the later assertions can still run
        // operations that fire `on_evict`.
        let fired = std::sync::Arc::new(std::sync::atomic::AtomicU64::new(0));
        let cb = std::sync::Arc::clone(&fired);
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .on_evict(move |_k, _v| {
                if cb.fetch_add(1, std::sync::atomic::Ordering::Relaxed) == 0 {
                    panic!("boom");
                }
            })
            .build()
            .unwrap();
        for k in 0u32..6 {
            cache.set(k, k);
        }

        let caught = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            cache.retain(|k, _v| k % 2 == 0);
        }));
        assert!(caught.is_err(), "the callback panic must propagate");

        // Both structures completed their removals before the callback ran.
        assert_index_lockstep(&cache, "after on_evict panicked mid-retain");
        assert_eq!(cache.map.len(), 3, "the odd keys were removed");
        // The survivors are still reachable through the expiry index, so a subsequent
        // trim sees all of them rather than stopping short at an orphaned entry.
        assert_eq!(cache.retain_latest(0, false), 3);
        assert_index_lockstep(&cache, "after trimming the survivors");
    }

    /// Entries inserted back to back can share an expiry `Instant` on a coarse clock, so
    /// their `Stamped`s differ only by key. `retain` must remove exactly the index entry
    /// belonging to each removed map entry, never a same-expiry neighbour.
    #[test]
    fn retain_index_lockstep_with_colliding_expiry_instants() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        for k in 0u32..64 {
            cache.set(k, k);
        }
        assert_index_lockstep(&cache, "before retain");

        cache.retain(|k, _v| k % 2 == 0);
        assert_eq!(cache.map.len(), 32);
        assert_index_lockstep(&cache, "after retain");
        for k in 0u32..64 {
            assert_eq!(
                cache.map.contains_key(&k),
                k % 2 == 0,
                "exactly the even keys survive"
            );
        }
        assert_eq!(cache.evict(), 0, "nothing is expired, nothing is stale");

        // Every surviving stamp must still resolve to a map entry.
        assert_eq!(cache.retain_latest(0, false), 32);
        assert!(cache.map.is_empty());
        assert!(cache.keys.is_empty());
    }

    // ---------------------------------------------------------------------------
    // Front-driven `evict_at` / `retain_latest_at` equivalence with the old two-pass
    // (`range(..).count()` then pop) logic. The real monotonic clock never produces a
    // deterministic `now == expiry` tie, so these drive the private `*_at` entry points
    // with hand-built entries and an explicit cutoff.
    // ---------------------------------------------------------------------------

    /// Insert an entry with an exact `expiry`, keeping map and index in lockstep the same
    /// way `set_inner` does (same `CacheArc` in both structures).
    fn insert_raw(
        cache: &mut TtlSortedCache<u32, u32>,
        key: u32,
        value: u32,
        expiry: Option<crate::time::Instant>,
    ) {
        use super::{CacheArc, Entry, Stamped};
        let arc = CacheArc::new(key);
        cache.keys.insert(Stamped {
            expiry,
            key: Some(arc.clone()),
        });
        cache.map.insert(
            key,
            Entry {
                expiry,
                key: arc,
                value,
            },
        );
    }

    /// The number of entries the pre-refactor sweep would have removed: the count of the
    /// `[bound(min_instant), bound(cutoff))` range over the expiry index.
    fn old_range_expired_count(
        cache: &TtlSortedCache<u32, u32>,
        cutoff: crate::time::Instant,
    ) -> usize {
        use super::Stamped;
        use std::ops::Bound::{Excluded, Included};
        let min = Stamped::<u32>::bound(cache.min_instant);
        let max = Stamped::<u32>::bound(cutoff);
        cache.keys.range((Included(&min), Excluded(&max))).count()
    }

    /// Build a cache holding one entry strictly before the cutoff, one tied exactly at the
    /// cutoff, one after it, and one that never expires. All expiries are in the future
    /// relative to `min_instant`, so the old lower range bound would not have excluded any.
    fn boundary_population() -> (TtlSortedCache<u32, u32>, crate::time::Instant) {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        let cutoff = crate::time::Instant::now() + Duration::from_secs(1);
        insert_raw(&mut cache, 1, 10, Some(cutoff - Duration::from_nanos(1)));
        insert_raw(&mut cache, 2, 20, Some(cutoff));
        insert_raw(&mut cache, 3, 30, Some(cutoff + Duration::from_nanos(1)));
        insert_raw(&mut cache, 4, 40, None);
        (cache, cutoff)
    }

    /// An entry expiring EXACTLY at the cutoff is not swept (the old
    /// `Excluded(bound(cutoff))` bound excluded it too, because the sentinel's `key: None`
    /// sorts below every real key at the same expiry), and a never-expiring entry survives.
    #[test]
    fn evict_at_boundary_tie_and_never_expiring_entries_match_the_old_range_bounds() {
        let (mut cache, cutoff) = boundary_population();
        assert_eq!(
            old_range_expired_count(&cache, cutoff),
            1,
            "reference: the old range selected only the strictly-earlier entry"
        );

        assert_eq!(
            cache.evict_at(cutoff),
            1,
            "only the entry expiring strictly before the cutoff is swept"
        );
        assert!(!cache.map.contains_key(&1u32), "strictly earlier is swept");
        assert!(
            cache.map.contains_key(&2u32),
            "an exact now == expiry tie survives, matching the old Excluded upper bound"
        );
        assert!(cache.map.contains_key(&3u32), "later expiry survives");
        assert!(cache.map.contains_key(&4u32), "never-expiring survives");
        assert_index_lockstep(&cache, "after a boundary evict_at");

        // A cutoff one tick past the tie now sweeps it, proving the boundary is where the
        // old range put it and not one entry off.
        assert_eq!(cache.evict_at(cutoff + Duration::from_nanos(1)), 1);
        assert!(!cache.map.contains_key(&2u32));
        assert!(cache.map.contains_key(&3u32));
        assert!(cache.map.contains_key(&4u32));
        assert_index_lockstep(&cache, "after sweeping the tie");
    }

    /// Over a mixed population (many expired, some live, some never-expiring, colliding
    /// expiries) the front-driven sweep must drop exactly as many entries as the old
    /// `range(..).count()` pass would have.
    #[test]
    fn evict_at_drop_count_matches_the_old_two_pass_range_count() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        let cutoff = crate::time::Instant::now() + Duration::from_secs(1);
        for k in 0u32..30 {
            let expiry = match k % 3 {
                // Expired, with pairs of keys colliding on the same instant.
                0 => Some(cutoff - Duration::from_micros(u64::from(k / 3) + 1)),
                // Live: half of them tie EXACTLY on the cutoff, which must be kept.
                1 => Some(cutoff + Duration::from_micros(u64::from(k % 2))),
                // Never expires.
                _ => None,
            };
            insert_raw(&mut cache, k, k * 10, expiry);
        }
        let expected = old_range_expired_count(&cache, cutoff);
        assert_eq!(expected, 10, "reference count over the mixed population");

        assert_eq!(
            cache.evict_at(cutoff),
            expected,
            "front-driven evict must drop the same count as the old range pass"
        );
        assert_eq!(cache.map.len(), 20);
        assert_index_lockstep(&cache, "after a mixed evict_at");
        for k in 0u32..30 {
            assert_eq!(
                cache.map.contains_key(&k),
                k % 3 != 0,
                "exactly the expired third is swept"
            );
        }
        assert_eq!(
            cache.evict_at(cutoff),
            0,
            "a repeat sweep at the same cutoff drops nothing"
        );
    }

    /// A panicking `on_evict` must not desynchronize the map from the expiry index during an
    /// expiry sweep either. `evict` detaches the whole expired prefix from `keys` up front, so
    /// it must finish every `map` removal before firing any callback — unwinding mid-drain
    /// would strand the not-yet-removed entries in `map` with their stamps already gone.
    #[test]
    fn evict_on_evict_panic_leaves_the_index_in_lockstep() {
        let fired = std::sync::Arc::new(std::sync::atomic::AtomicU64::new(0));
        let cb = std::sync::Arc::clone(&fired);
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .on_evict(move |_k, _v| {
                // Panic on the FIRST callback: with an interleaved drain that would leave the
                // remaining five entries in `map` without stamps.
                if cb.fetch_add(1, std::sync::atomic::Ordering::Relaxed) == 0 {
                    panic!("boom");
                }
            })
            .build()
            .unwrap();
        let cutoff = crate::time::Instant::now() + Duration::from_secs(1);
        for k in 0u32..6 {
            insert_raw(
                &mut cache,
                k,
                k,
                Some(cutoff - Duration::from_micros(u64::from(k) + 1)),
            );
        }
        // One live entry that must survive untouched.
        insert_raw(&mut cache, 100u32, 100u32, Some(cutoff));

        let caught = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let _ = cache.evict_at(cutoff);
        }));
        assert!(caught.is_err(), "the callback panic must propagate");

        assert_index_lockstep(&cache, "after on_evict panicked mid-evict");
        assert_eq!(
            cache.map.len(),
            1,
            "every expired entry left the map before the callbacks ran"
        );
        assert!(cache.map.contains_key(&100u32), "the live entry survives");
        assert_eq!(cache.cache_evictions(), Some(6));
        // The survivor is still reachable through the index.
        assert_eq!(cache.retain_latest(0, false), 1);
        assert_index_lockstep(&cache, "after trimming the survivor");
    }

    /// A panicking `on_evict` must not desynchronize the map from the expiry index during a
    /// `retain_latest` size trim either. Unlike `evict_at`'s batch-then-notify drain,
    /// `retain_latest_at`'s pop loop fires `on_evict` inline per popped entry, so this path
    /// only stays correct because each iteration finishes its `map` removal and the eviction
    /// counter bump BEFORE the callback runs. Panic on the third callback (after two full,
    /// uninterrupted iterations) so the test also proves the ordering holds across repeated
    /// iterations, not merely on the very first one.
    #[test]
    fn retain_latest_on_evict_panic_leaves_the_index_in_lockstep() {
        let fired = std::sync::Arc::new(std::sync::atomic::AtomicU64::new(0));
        let cb = std::sync::Arc::clone(&fired);
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .on_evict(move |_k, _v| {
                if cb.fetch_add(1, std::sync::atomic::Ordering::Relaxed) == 2 {
                    panic!("boom");
                }
            })
            .build()
            .unwrap();
        let cutoff = crate::time::Instant::now() + Duration::from_secs(60);
        // Five entries with strictly ascending expiry (soonest-to-expire first, matching the
        // order `retain_latest_at`'s pop loop visits them in), plus a never-expiring survivor
        // that must remain untouched by the trim.
        for k in 0u32..5 {
            insert_raw(
                &mut cache,
                k,
                k,
                Some(cutoff - Duration::from_secs(5) + Duration::from_micros(u64::from(k))),
            );
        }
        insert_raw(&mut cache, 100u32, 100u32, None);

        let caught = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            // Keep only 1 entry: this must trim k=0..4, dropping the far end of the pop loop
            // to the third callback's panic.
            let _ = cache.retain_latest_at(1, None);
        }));
        assert!(caught.is_err(), "the callback panic must propagate");

        assert_index_lockstep(&cache, "after on_evict panicked mid retain_latest_at");
        // k=0 and k=1 completed a full (pop, map.remove, counter bump, callback) cycle before
        // the panic; k=2's own removal also completed before its callback panicked. A reordered
        // `retain_latest_at` that fires the callback before removing from `map` would strand
        // k=2 in `map` with its stamp already gone from `keys` -- caught by the lockstep assert
        // above, but pinned down explicitly here too.
        assert!(!cache.map.contains_key(&0u32), "k=0 fully removed");
        assert!(!cache.map.contains_key(&1u32), "k=1 fully removed");
        assert!(
            !cache.map.contains_key(&2u32),
            "k=2 fully removed despite its callback panic"
        );
        // The pop loop never reached these: it unwound out of the panicking callback first.
        assert!(
            cache.map.contains_key(&3u32),
            "k=3 untouched by the interrupted trim"
        );
        assert!(
            cache.map.contains_key(&4u32),
            "k=4 untouched by the interrupted trim"
        );
        assert!(
            cache.map.contains_key(&100u32),
            "never-expiring survivor untouched"
        );
        assert_eq!(
            cache.cache_evictions(),
            Some(3),
            "counter reflects exactly the three rows removed before the panic"
        );
        // The survivor is still reachable through the index after the panic.
        assert_eq!(cache.cache_get(&100u32), Some(&100u32));
        assert_eq!(cache.retain_latest(0, false), 3);
        assert_index_lockstep(&cache, "after trimming what the panic left behind");
    }

    /// The value-`Drop` panic path of the `retain_latest_at` size trim, with no `on_evict`
    /// configured. Unlike `evict_at` -- which detaches the whole expired prefix from
    /// `self.keys` in one `split_off` and so must drain `self.map` into a local `Vec` before
    /// any value drops -- `retain_latest_at`'s pop loop removes one stamp and its map row
    /// together before that value drops, so a panicking `Drop` mid-trim leaves map and keys
    /// in lockstep without a collect step. A regression that detached the trim prefix up front
    /// and dropped per iteration (as the pre-fix `evict_at` did) would orphan the
    /// not-yet-removed rows: their stamps already gone from `self.keys` while their entries
    /// linger in `self.map`.
    #[test]
    fn retain_latest_no_callback_keeps_map_and_keys_in_lockstep_on_drop_panic() {
        use std::panic::{AssertUnwindSafe, catch_unwind};

        // A value whose `Drop` panics only when armed. Exactly ONE instance is armed so the
        // unwind never double-panics into a process abort.
        struct PanicOnDrop {
            armed: bool,
        }
        impl Drop for PanicOnDrop {
            fn drop(&mut self) {
                if self.armed {
                    panic!("PanicOnDrop::drop fired");
                }
            }
        }

        // No `on_evict` and no `size_limit`: the trim runs through `retain_latest` directly.
        let mut cache: TtlSortedCache<u32, PanicOnDrop> = TtlSortedCache::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        // Five finite entries with ascending expiry (soonest first, the pop order). Arm the
        // THIRD to pop (key 2) so two full iterations complete before it drops -- proving the
        // remove-before-drop ordering holds across iterations, and leaving keys 3/4 to be
        // stranded by a batched regression.
        for k in 0u32..5 {
            cache
                .set_with(k, PanicOnDrop { armed: k == 2 })
                .ttl(Duration::from_secs(10 * (u64::from(k) + 1)))
                .set();
        }
        assert_eq!(cache.map.len(), 5);
        assert_eq!(cache.keys.len(), 5);

        // Keep 1 -> retain_drop_count = 4: the trim pops keys 0..=3, and the armed value's
        // Drop panics on the third pop before keys 3/4 are reached.
        let result = catch_unwind(AssertUnwindSafe(|| {
            let _ = cache.retain_latest(1, false);
        }));
        assert!(
            result.is_err(),
            "the armed value's Drop must have panicked mid-trim"
        );

        // Lockstep is the core property: whatever left `self.keys` also left `self.map`.
        assert_index_lockstep(&cache, "after a Drop panic mid retain_latest trim");
        assert!(!cache.map.contains_key(&0u32), "key 0 fully removed");
        assert!(!cache.map.contains_key(&1u32), "key 1 fully removed");
        assert!(
            !cache.map.contains_key(&2u32),
            "the armed row left the map before its Drop panicked"
        );
        assert!(
            cache.map.contains_key(&3u32),
            "key 3 untouched by the interrupted trim"
        );
        assert!(
            cache.map.contains_key(&4u32),
            "key 4 untouched by the interrupted trim"
        );
        assert_eq!(cache.map.len(), 2);
        // The counter is bumped before each value drops, so all three removed rows are counted.
        assert_eq!(
            cache.cache_evictions(),
            Some(3),
            "the counter reflects the three rows removed before the panic"
        );
        // The survivors remain reachable through the index for a later trim.
        assert_eq!(cache.retain_latest(0, false), 2);
        assert!(cache.map.is_empty());
        assert!(cache.keys.is_empty());
    }

    /// `retain_latest_at` with the sweep enabled: the tie at the cutoff is kept and
    /// never-expiring entries sort last, exactly as under the old range bounds.
    #[test]
    fn retain_latest_at_boundary_tie_and_never_expiring_entries_match_the_old_range_bounds() {
        // count = 4 (no size pressure): only the expiry sweep can drop anything.
        let (mut cache, cutoff) = boundary_population();
        assert_eq!(
            cache.retain_latest_at(4, Some(cutoff)),
            1,
            "only the strictly-earlier entry is swept; the tie is kept"
        );
        assert!(cache.map.contains_key(&2u32), "the exact tie survives");
        assert!(cache.map.contains_key(&4u32), "never-expiring survives");
        assert_index_lockstep(&cache, "after a boundary retain_latest_at");

        // Size pressure beyond the expired prefix: the sweep count and the trim count are
        // combined exactly as `max(retain_drop_count, expired_count)` did.
        let (mut cache, cutoff) = boundary_population();
        assert_eq!(
            cache.retain_latest_at(2, Some(cutoff)),
            2,
            "trim of 2 dominates the single expired entry"
        );
        assert!(!cache.map.contains_key(&1u32), "expired dropped first");
        assert!(!cache.map.contains_key(&2u32), "then the soonest-to-expire");
        assert!(
            cache.map.contains_key(&4u32),
            "never-expiring entries are retained last"
        );
        assert_index_lockstep(&cache, "after a size-dominated retain_latest_at");

        // Sweep disabled: a size trim alone never consults the cutoff.
        let (mut cache, _cutoff) = boundary_population();
        assert_eq!(cache.retain_latest_at(4, None), 0);
        assert_eq!(cache.map.len(), 4, "no trim needed, no sweep requested");
        assert_index_lockstep(&cache, "after a no-op retain_latest_at");
    }

    /// The combined trim/sweep drop count must equal the old
    /// `max(retain_drop_count, range(..).count())` for every `count` and both sweep modes.
    #[test]
    fn retain_latest_at_drop_count_matches_the_old_two_pass_logic() {
        fn fresh() -> (TtlSortedCache<u32, u32>, crate::time::Instant) {
            let mut cache = TtlSortedCache::<u32, u32>::builder()
                .ttl(Duration::from_secs(60))
                .build()
                .unwrap();
            let cutoff = crate::time::Instant::now() + Duration::from_secs(1);
            for k in 0u32..12 {
                let expiry = match k % 3 {
                    0 => Some(cutoff - Duration::from_micros(u64::from(k) + 1)),
                    // Half of the live entries tie EXACTLY on the cutoff.
                    1 => Some(cutoff + Duration::from_micros(u64::from(k % 2))),
                    _ => None,
                };
                insert_raw(&mut cache, k, k * 10, expiry);
            }
            (cache, cutoff)
        }

        for keep in 0..=13usize {
            for sweep in [false, true] {
                let (mut cache, cutoff) = fresh();
                let retain_drop_count = cache.map.len().saturating_sub(keep);
                let expected = if sweep {
                    retain_drop_count.max(old_range_expired_count(&cache, cutoff))
                } else {
                    retain_drop_count
                };
                let dropped = cache.retain_latest_at(keep, sweep.then_some(cutoff));
                assert_eq!(
                    dropped, expected,
                    "keep={keep} sweep={sweep}: drop count must match the old two-pass logic"
                );
                assert_eq!(cache.map.len(), 12 - expected);
                assert_index_lockstep(&cache, "after retain_latest_at");
            }
        }
    }

    // ---------------------------------------------------------------------------
    // `set_inner` / `set_and_get_mut` refactor
    // ---------------------------------------------------------------------------

    /// `set_and_get_mut` no longer re-looks-up the key after inserting; it reuses the stamp
    /// `set_inner` returns. The map/index lockstep must hold on every branch it takes:
    /// plain miss, expired displacement, and the protected size-limited eviction.
    #[test]
    fn set_and_get_mut_keeps_the_index_in_lockstep() {
        // Plain miss, no size limit.
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        for k in 0u32..4 {
            let v = cache.cache_get_or_set_with_mut(k, || k * 10);
            assert_eq!(*v, k * 10);
            assert_index_lockstep(&cache, "after a miss insert");
        }
        assert_eq!(cache.map.len(), 4);

        // Expired displacement: the factory runs again and the stale stamp is replaced.
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(20))
            .build()
            .unwrap();
        cache.cache_set(1u32, 10u32);
        std::thread::sleep(std::time::Duration::from_millis(60));
        assert_eq!(*cache.cache_get_or_set_with_mut(1u32, || 99u32), 99u32);
        assert_eq!(cache.map.len(), 1);
        assert_index_lockstep(&cache, "after replacing an expired entry");

        // Size-limited: the just-inserted entry is protected, its stamp is restored, and the
        // returned reference points at that entry.
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .max_size(3)
            .build()
            .unwrap();
        for k in 0u32..8 {
            let v = cache.cache_get_or_set_with_mut(k, || k * 100);
            assert_eq!(*v, k * 100, "the reference must be the entry just inserted");
            *v += 1;
            assert!(cache.map.len() <= 3, "the size limit is enforced");
            assert_index_lockstep(&cache, "after a protected size-limited insert");
            assert_eq!(
                crate::CachedPeek::cache_peek(&cache, &k),
                Some(&(k * 100 + 1)),
                "the protected entry survives its own insert and is mutable through the ref"
            );
        }
        // The index is still complete enough to trim everything.
        let len = cache.map.len();
        assert_eq!(cache.retain_latest(0, false), len);
        assert!(cache.keys.is_empty());
    }

    /// Overwriting an existing key reuses the stored key `Arc` (a refcount bump) instead of
    /// allocating a fresh `Arc` from a deep key clone, and the index keeps exactly one stamp.
    #[test]
    fn cache_set_overwrite_reuses_the_stored_key_arc() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        cache.cache_set(1u32, 10u32);
        let before = cache.map.get(&1u32).expect("entry present").key.0.clone();

        assert_eq!(cache.cache_set(1u32, 20u32), Some(10u32));
        let after = &cache.map.get(&1u32).expect("entry present").key.0;
        assert!(
            Arc::ptr_eq(&before, after),
            "an overwrite must reuse the stored key Arc, not allocate a new one"
        );
        assert_eq!(cache.map.len(), 1);
        assert_eq!(cache.keys.len(), 1, "no orphan stamp is left behind");
        assert_index_lockstep(&cache, "after an overwrite");
    }

    /// An overwrite that changes the expiry must remove the old stamp and index the new one;
    /// an overwrite that leaves the expiry unchanged must leave a single stamp in place.
    #[test]
    fn overwrite_replaces_the_index_stamp_only_when_the_expiry_changes() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        // Two entries so a stale stamp would be visible as a length mismatch.
        cache.set_with(1u32, 10u32).ttl(Duration::ZERO).set();
        cache.set_with(2u32, 20u32).ttl(Duration::ZERO).set();
        assert_index_lockstep(&cache, "never-expiring pair");

        // Same (None) expiry: the stamp is identical, nothing to remove.
        assert_eq!(
            cache.set_with(1u32, 11u32).ttl(Duration::ZERO).set(),
            Some(10u32)
        );
        assert_eq!(cache.keys.len(), 2);
        assert_index_lockstep(&cache, "after a same-expiry overwrite");

        // Changed expiry (None -> Some): the never-expires stamp must be dropped, leaving the
        // entry sorted by its new finite expiry (so it now evicts before the never-expiring one).
        assert_eq!(
            cache
                .set_with(1u32, 12u32)
                .ttl(Duration::from_secs(30))
                .set(),
            Some(11u32)
        );
        assert_eq!(cache.keys.len(), 2, "the stale never-expires stamp is gone");
        assert_index_lockstep(&cache, "after an expiry-changing overwrite");
        assert_eq!(cache.retain_latest(1, false), 1);
        assert!(
            !cache.map.contains_key(&1u32),
            "the re-stamped entry now expires first and is trimmed first"
        );
        assert!(cache.map.contains_key(&2u32));
        assert_index_lockstep(&cache, "after trimming the re-stamped entry");
    }

    /// The async get-or-set now checks liveness in place instead of routing through
    /// `cache_get` (which swept the expired entry before the factory even ran). A factory
    /// future dropped before completion must therefore leave the expired entry alone and
    /// must not fire `on_evict`, matching the sync path and `TtlCache`.
    #[cfg(feature = "async")]
    #[tokio::test]
    async fn async_cache_get_or_set_with_mut_cancel_keeps_expired_entry() {
        use crate::CachedGetOrSetAsync;
        use std::task::Poll;

        let fired = Arc::new(AtomicUsize::new(0));
        let fired2 = fired.clone();
        let mut c: TtlSortedCache<u32, u32> = TtlSortedCache::builder()
            .ttl(Duration::from_millis(20))
            .on_evict(move |_k: &u32, _v: &u32| {
                fired2.fetch_add(1, Ordering::Relaxed);
            })
            .build()
            .unwrap();
        c.cache_set(1u32, 100u32);
        tokio::time::sleep(std::time::Duration::from_millis(60)).await;

        {
            let mut fut = Box::pin(CachedGetOrSetAsync::async_cache_get_or_set_with_mut(
                &mut c,
                1u32,
                std::future::pending::<u32>,
            ));
            let waker = std::task::Waker::noop();
            let mut cx = std::task::Context::from_waker(waker);
            assert!(
                matches!(
                    std::future::Future::poll(fut.as_mut(), &mut cx),
                    Poll::Pending
                ),
                "future must be pending while the factory is unresolved"
            );
            // Dropped here: cancellation mid-factory.
        }

        assert_eq!(
            fired.load(Ordering::Relaxed),
            0,
            "on_evict must not fire when the factory future is dropped"
        );
        assert_eq!(c.cache_evictions(), Some(0));
        assert_eq!(c.cache_size(), 1, "the expired entry must still be present");
        assert_index_lockstep(&c, "after async factory cancellation");

        // A completed factory replaces it, firing on_evict exactly once for the displaced
        // expired value.
        let v =
            CachedGetOrSetAsync::async_cache_get_or_set_with_mut(&mut c, 1u32, || async { 200u32 })
                .await;
        assert_eq!(*v, 200u32);
        assert_eq!(fired.load(Ordering::Relaxed), 1);
        assert_eq!(c.cache_evictions(), Some(1));
        assert_index_lockstep(&c, "after async replacement");
    }

    /// An async hit counts exactly one hit (and no miss), the same as the sync path: the
    /// in-place liveness check replaced a `cache_get` call that also touched the counters.
    #[cfg(feature = "async")]
    #[tokio::test]
    async fn async_cache_get_or_set_with_mut_hit_counts_one_hit() {
        use crate::CachedGetOrSetAsync;

        let mut c: TtlSortedCache<u32, u32> = TtlSortedCache::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        c.cache_set(1u32, 100u32);
        c.cache_reset_metrics();

        let v = CachedGetOrSetAsync::async_cache_get_or_set_with_mut(&mut c, 1u32, || async {
            unreachable!("the factory must not run on a live hit")
        })
        .await;
        assert_eq!(*v, 100u32);
        assert_eq!(c.cache_hits(), Some(1));
        assert_eq!(c.cache_misses(), Some(0));

        // And a miss counts exactly one miss.
        let v =
            CachedGetOrSetAsync::async_cache_get_or_set_with_mut(&mut c, 2u32, || async { 200u32 })
                .await;
        assert_eq!(*v, 200u32);
        assert_eq!(c.cache_hits(), Some(1));
        assert_eq!(c.cache_misses(), Some(1));
        assert_index_lockstep(&c, "after an async miss insert");
    }

    /// The fallible async sibling (`async_cache_try_get_or_set_with_mut`) was never given a
    /// dedicated absent-key test: the flagged risk is that its explicit `hits`/`misses`
    /// increments (replacing the old implicit `cache_get`-driven ones) could be off by one or
    /// double-counted on the paths the big cross-variant table test does not visit (a key that
    /// was never inserted, as opposed to one that expired). Walks a live hit, an absent-key
    /// `Err`, an absent-key `Ok`, an expired-key `Err`, and an expired-key `Ok` in sequence,
    /// asserting the exact running `hits`/`misses` totals and `on_evict`/`evictions` at each
    /// step -- a double count or a missed count on any single path desyncs the running total
    /// for every step after it.
    #[cfg(feature = "async")]
    #[tokio::test]
    async fn async_try_get_or_set_with_mut_hits_and_misses_match_across_absent_expired_and_live() {
        use crate::CachedGetOrSetAsync;

        let fired = Arc::new(AtomicUsize::new(0));
        let fired2 = fired.clone();
        let mut c: TtlSortedCache<u32, u32> = TtlSortedCache::builder()
            .ttl(Duration::from_millis(20))
            .on_evict(move |_k: &u32, _v: &u32| {
                fired2.fetch_add(1, Ordering::Relaxed);
            })
            .build()
            .unwrap();

        // A live entry (key 1, long TTL via the builder) to hit against.
        c.set_with(1u32, 100u32).ttl(Duration::from_secs(60)).set();
        c.cache_reset_metrics();

        // Step 1: LIVE HIT. The factory must not run; exactly one hit, no miss, nothing
        // evicted, the value is unchanged.
        let v: Result<&mut u32, &'static str> =
            CachedGetOrSetAsync::async_cache_try_get_or_set_with_mut(&mut c, 1u32, || async {
                unreachable!("the factory must not run on a live hit")
            })
            .await;
        assert_eq!(v.ok(), Some(&mut 100u32));
        assert_eq!(c.cache_hits(), Some(1), "step 1: one hit");
        assert_eq!(c.cache_misses(), Some(0), "step 1: no miss");
        assert_eq!(fired.load(Ordering::Relaxed), 0);
        assert_eq!(c.cache_evictions(), Some(0));

        // Step 2: ABSENT KEY, factory Err. Nothing is stored, one miss is counted (before the
        // factory runs), no eviction fires (there was nothing to displace).
        let v: Result<&mut u32, &'static str> =
            CachedGetOrSetAsync::async_cache_try_get_or_set_with_mut(&mut c, 2u32, || async {
                Err("nope")
            })
            .await;
        assert_eq!(v.err(), Some("nope"));
        // `cache_peek` (not `cache_get`) to check absence without touching the very counters
        // under test.
        assert_eq!(
            crate::CachedPeek::cache_peek(&c, &2u32),
            None,
            "a failed factory inserts nothing"
        );
        assert_eq!(c.cache_size(), 1);
        assert_eq!(c.cache_hits(), Some(1), "step 2: still one hit");
        assert_eq!(c.cache_misses(), Some(1), "step 2: one miss, not two");
        assert_eq!(fired.load(Ordering::Relaxed), 0);
        assert_eq!(c.cache_evictions(), Some(0));

        // Step 3: the SAME absent key, factory Ok this time. One more miss (the key is still
        // absent going in), a fresh insert with nothing displaced, so no eviction.
        let v: Result<&mut u32, &'static str> =
            CachedGetOrSetAsync::async_cache_try_get_or_set_with_mut(&mut c, 2u32, || async {
                Ok(200u32)
            })
            .await;
        assert_eq!(v.ok(), Some(&mut 200u32));
        assert_eq!(c.cache_size(), 2);
        assert_eq!(c.cache_hits(), Some(1), "step 3: still one hit");
        assert_eq!(c.cache_misses(), Some(2), "step 3: two misses total");
        assert_eq!(fired.load(Ordering::Relaxed), 0);
        assert_eq!(c.cache_evictions(), Some(0));

        // Step 4: an EXPIRED key (key 3), factory Err. Counts a miss even though the key is
        // technically present-but-stale; the stale entry is left exactly alone (no eviction).
        c.set_with(3u32, 300u32).ttl(Duration::from_millis(1)).set();
        tokio::time::sleep(std::time::Duration::from_millis(30)).await;
        let v: Result<&mut u32, &'static str> =
            CachedGetOrSetAsync::async_cache_try_get_or_set_with_mut(&mut c, 3u32, || async {
                Err("still nope")
            })
            .await;
        assert_eq!(v.err(), Some("still nope"));
        assert_eq!(c.cache_hits(), Some(1), "step 4: still one hit");
        assert_eq!(c.cache_misses(), Some(3), "step 4: three misses total");
        assert_eq!(
            fired.load(Ordering::Relaxed),
            0,
            "the stale entry is left alone on Err, not evicted"
        );
        assert_eq!(c.cache_evictions(), Some(0));

        // Step 5: the SAME expired key, factory Ok. One more miss, and NOW the stale entry is
        // displaced: exactly one eviction fires, counted once.
        let v: Result<&mut u32, &'static str> =
            CachedGetOrSetAsync::async_cache_try_get_or_set_with_mut(&mut c, 3u32, || async {
                Ok(301u32)
            })
            .await;
        assert_eq!(v.ok(), Some(&mut 301u32));
        assert_eq!(c.cache_hits(), Some(1), "step 5: still one hit");
        assert_eq!(c.cache_misses(), Some(4), "step 5: four misses total");
        assert_eq!(
            fired.load(Ordering::Relaxed),
            1,
            "the stale entry is evicted exactly once when finally displaced"
        );
        assert_eq!(c.cache_evictions(), Some(1));

        // Step 6: a final live hit on the just-replaced key 3, to confirm hits still advance
        // correctly after a run of misses.
        let v: Result<&mut u32, &'static str> =
            CachedGetOrSetAsync::async_cache_try_get_or_set_with_mut(&mut c, 3u32, || async {
                unreachable!("the factory must not run on a live hit")
            })
            .await;
        assert_eq!(v.ok(), Some(&mut 301u32));
        assert_eq!(c.cache_hits(), Some(2), "step 6: two hits total");
        assert_eq!(c.cache_misses(), Some(4), "step 6: no additional miss");
        assert_eq!(c.cache_evictions(), Some(1));
        assert_eq!(c.cache_size(), 3);
        assert_index_lockstep(&c, "after the mixed hit/miss/absent/expired sequence");
    }

    // ===========================================================================
    // Independent certification coverage (outside-in): the `split_off` pivot,
    // the two `evict_at` drain branches, deferred-callback panic safety, the
    // orphaned-stamp divergence, coarse-`Ord` keys, and the async liveness-check
    // behavior change.
    // ===========================================================================

    /// Insert a stamp into the expiry index WITHOUT a matching map entry, i.e. deliberately
    /// break the map/index lockstep invariant. Only reachable from inside this module; used
    /// to pin what the sweeps do if the invariant were ever violated.
    fn insert_orphan_stamp(
        cache: &mut TtlSortedCache<u32, u32>,
        key: u32,
        expiry: Option<crate::time::Instant>,
    ) {
        use super::{CacheArc, Stamped};
        cache.keys.insert(Stamped {
            expiry,
            key: Some(CacheArc::new(key)),
        });
    }

    /// The `key: None` sentinel must sort strictly BELOW every real key carrying the same
    /// expiry, and strictly below any never-expiring stamp. This is the single ordering fact
    /// the whole `split_off` boundary rests on.
    #[test]
    fn bound_sentinel_sorts_below_every_real_key_at_the_same_expiry() {
        use super::{CacheArc, Stamped};
        use crate::time::Instant;

        let t = Instant::now();
        let sentinel = Stamped::<u32>::bound(t);
        for k in [u32::MIN, 1u32, u32::MAX] {
            let real = Stamped {
                expiry: Some(t),
                key: Some(CacheArc::new(k)),
            };
            assert!(
                sentinel < real,
                "bound({t:?}) must sort below the real key {k} at the same expiry"
            );
        }
        // A never-expiring stamp sorts above every finite bound, no matter how far out.
        let never = Stamped {
            expiry: None,
            key: Some(CacheArc::new(0u32)),
        };
        assert!(sentinel < never, "None expiry sorts greatest");
        assert!(
            Stamped::<u32>::bound(t + Duration::from_secs(60 * 60 * 24 * 365)) < never,
            "no finite cutoff can ever reach a never-expiring stamp"
        );
        // And the sentinel is strictly ordered by expiry.
        assert!(sentinel < Stamped::<u32>::bound(t + Duration::from_nanos(1)));
    }

    /// Non-vacuity guard for the boundary tests: the population they use genuinely
    /// discriminates the pivot. Splitting the SAME index at `bound(cutoff - 1ns)`,
    /// `bound(cutoff)` and `bound(cutoff + 1ns)` must yield three different prefix sizes
    /// (0, 1, 2), so an off-by-one-tick pivot could not pass the shipped assertions.
    #[test]
    fn evict_at_pivot_choice_is_observable_at_the_tie() {
        use super::Stamped;

        for (shift_back, expected) in [(true, 0usize), (false, 1)] {
            let (cache, cutoff) = boundary_population();
            let pivot = if shift_back {
                Stamped::<u32>::bound(cutoff - Duration::from_nanos(1))
            } else {
                Stamped::<u32>::bound(cutoff)
            };
            let mut keys = cache.keys.clone();
            let prefix_len = keys.len() - keys.split_off(&pivot).len();
            assert_eq!(
                prefix_len, expected,
                "shift_back={shift_back}: the split prefix must be sensitive to the pivot"
            );
        }
        let (cache, cutoff) = boundary_population();
        let mut keys = cache.keys.clone();
        let past_tie = keys.len()
            - keys
                .split_off(&Stamped::<u32>::bound(cutoff + Duration::from_nanos(1)))
                .len();
        assert_eq!(
            past_tie, 2,
            "a pivot one tick past the tie would sweep the tied entry too"
        );
        // The shipped pivot is the middle one, so both off-by-one-tick mutants are caught.
        let (mut cache, cutoff) = boundary_population();
        assert_eq!(cache.evict_at(cutoff), 1);
    }

    /// `split_off` on an empty index must be a clean no-op, not a panic or a leaked
    /// half-swapped tree.
    #[test]
    fn evict_at_on_an_empty_index_is_a_noop() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        let now = crate::time::Instant::now();
        assert_eq!(cache.evict_at(now), 0);
        assert_eq!(cache.evict_at(now + Duration::from_secs(1000)), 0);
        assert!(cache.keys.is_empty());
        assert!(cache.map.is_empty());
        assert_eq!(cache.cache_evictions(), Some(0));
        assert_index_lockstep(&cache, "empty evict_at");

        // Still usable afterwards.
        cache.set(1u32, 1u32);
        assert_index_lockstep(&cache, "after reuse");
        assert_eq!(cache.cache_get(&1u32), Some(&1u32));
    }

    /// An index that is ENTIRELY expired: `split_off` returns an empty live half, so the
    /// whole tree is drained and `self.keys` must end up empty (not the detached prefix).
    #[test]
    fn evict_at_with_an_entirely_expired_index_drains_everything() {
        for with_callback in [false, true] {
            let fired = Arc::new(AtomicUsize::new(0));
            let fired2 = fired.clone();
            let mut builder = TtlSortedCache::<u32, u32>::builder().ttl(Duration::from_secs(60));
            if with_callback {
                builder = builder.on_evict(move |_k: &u32, _v: &u32| {
                    fired2.fetch_add(1, Ordering::Relaxed);
                });
            }
            let mut cache = builder.build().unwrap();
            let cutoff = crate::time::Instant::now() + Duration::from_secs(1);
            for k in 0u32..8 {
                insert_raw(
                    &mut cache,
                    k,
                    k,
                    Some(cutoff - Duration::from_micros(u64::from(k) + 1)),
                );
            }
            assert_eq!(cache.evict_at(cutoff), 8, "with_callback={with_callback}");
            assert!(cache.keys.is_empty(), "the index must be fully drained");
            assert!(cache.map.is_empty());
            assert_eq!(cache.cache_evictions(), Some(8));
            assert_eq!(
                fired.load(Ordering::Relaxed),
                usize::from(with_callback) * 8
            );
            assert_index_lockstep(&cache, "after draining every entry");
            assert_eq!(cache.evict_at(cutoff), 0, "a repeat sweep drops nothing");
        }
    }

    /// An index that is ENTIRELY live: the detached prefix must be empty and the live half
    /// must be swapped back intact, in order.
    #[test]
    fn evict_at_with_an_entirely_live_index_drops_nothing() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        let cutoff = crate::time::Instant::now() + Duration::from_secs(1);
        for k in 0u32..8 {
            let expiry = if k % 2 == 0 {
                Some(cutoff + Duration::from_micros(u64::from(k) + 1))
            } else {
                None
            };
            insert_raw(&mut cache, k, k * 10, expiry);
        }
        let before: Vec<Option<u32>> = cache
            .keys
            .iter()
            .map(|s| s.key.as_ref().map(|k| *k.0))
            .collect();

        assert_eq!(cache.evict_at(cutoff), 0);
        assert_eq!(cache.map.len(), 8);
        assert_eq!(cache.cache_evictions(), Some(0));
        let after: Vec<Option<u32>> = cache
            .keys
            .iter()
            .map(|s| s.key.as_ref().map(|k| *k.0))
            .collect();
        assert_eq!(before, after, "the live half is swapped back in order");
        assert_index_lockstep(&cache, "after an all-live evict_at");
    }

    /// The degenerate single-element cases at the tie: one entry exactly at the cutoff is
    /// kept, one entry a tick earlier is swept, and one never-expiring entry is kept.
    #[test]
    fn evict_at_single_element_exactly_at_the_tie_is_kept() {
        let cutoff = crate::time::Instant::now() + Duration::from_secs(1);
        let cases: [(Option<Duration>, usize); 3] = [
            (Some(Duration::ZERO), 0),          // expiry == cutoff -> live
            (Some(Duration::from_nanos(1)), 1), // expiry == cutoff - 1ns -> swept
            (None, 0),                          // never expires -> live
        ];
        for (back_off, expected) in cases {
            let mut cache = TtlSortedCache::<u32, u32>::builder()
                .ttl(Duration::from_secs(60))
                .build()
                .unwrap();
            let expiry = back_off.map(|d| cutoff - d);
            insert_raw(&mut cache, 1u32, 10u32, expiry);
            assert_eq!(
                old_range_expired_count(&cache, cutoff),
                expected,
                "reference: the old range agrees for back_off={back_off:?}"
            );
            assert_eq!(
                cache.evict_at(cutoff),
                expected,
                "single-element boundary for back_off={back_off:?}"
            );
            assert_eq!(cache.map.len(), 1 - expected);
            assert_index_lockstep(&cache, "single-element boundary");
        }
    }

    /// DIVERGENCE FROM THE OLD CODE (intentional): the old sweep range started at
    /// `Included(bound(min_instant))`, so an entry expiring BEFORE the cache was built would
    /// never have been swept. `split_off` has no lower bound, so such an entry is now swept.
    /// It is unreachable through the public API (every stored expiry is `insert_time + ttl`
    /// and every insert happens after `min_instant`), so this is a strict improvement, but it
    /// is a real behavioral difference and is pinned here.
    #[test]
    fn evict_at_sweeps_entries_older_than_min_instant_unlike_the_old_lower_bound() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        let min_instant = cache.min_instant;
        let cutoff = min_instant + Duration::from_secs(1);
        // Below `min_instant`: outside the old `Included(bound(min_instant))..` range.
        insert_raw(
            &mut cache,
            1u32,
            10u32,
            Some(min_instant - Duration::from_nanos(1)),
        );
        insert_raw(&mut cache, 2u32, 20u32, Some(min_instant));

        assert_eq!(
            old_range_expired_count(&cache, cutoff),
            1,
            "the old lower bound excluded the pre-min_instant entry"
        );
        assert_eq!(
            cache.evict_at(cutoff),
            2,
            "the front-driven sweep has no lower bound and reaps both"
        );
        assert!(cache.map.is_empty());
        assert_index_lockstep(&cache, "after sweeping below min_instant");
    }

    /// Deferred callbacks must still fire in expiry order, exactly once per entry actually
    /// removed from the map, with the return value and the eviction counter agreeing.
    #[test]
    fn evict_at_fires_on_evict_in_expiry_order_after_every_map_removal() {
        let log = Arc::new(std::sync::Mutex::new(Vec::<(u32, u32)>::new()));
        let log2 = log.clone();
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .on_evict(move |k: &u32, v: &u32| log2.lock().unwrap().push((*k, *v)))
            .build()
            .unwrap();
        let cutoff = crate::time::Instant::now() + Duration::from_secs(1);
        // Inserted in a scrambled order; the index sorts them by expiry regardless.
        for k in [3u32, 0, 4, 1, 2] {
            insert_raw(
                &mut cache,
                k,
                k * 10,
                Some(cutoff - Duration::from_micros(10 - u64::from(k))),
            );
        }
        insert_raw(&mut cache, 100u32, 1000u32, Some(cutoff));

        let dropped = cache.evict_at(cutoff);
        assert_eq!(dropped, 5, "the returned count is the number of drops");
        assert_eq!(
            cache.cache_evictions(),
            Some(5),
            "the counter agrees with the return value"
        );
        assert_eq!(
            *log.lock().unwrap(),
            vec![(0, 0), (1, 10), (2, 20), (3, 30), (4, 40)],
            "callbacks fire in ascending expiry order, once per removed entry"
        );
        assert_eq!(cache.map.len(), 1);
        assert_index_lockstep(&cache, "after an ordered deferred sweep");
    }

    /// A callback that panics on a MIDDLE entry: every map removal has already happened, so
    /// the index and the map stay in lockstep and the counter reflects all of them.
    #[test]
    fn evict_at_on_evict_panic_on_a_middle_callback_leaves_the_index_in_lockstep() {
        for panic_on in [0usize, 2, 5] {
            let seen = Arc::new(AtomicUsize::new(0));
            let seen2 = seen.clone();
            let mut cache = TtlSortedCache::<u32, u32>::builder()
                .ttl(Duration::from_secs(60))
                .on_evict(move |_k: &u32, _v: &u32| {
                    if seen2.fetch_add(1, Ordering::Relaxed) == panic_on {
                        panic!("boom");
                    }
                })
                .build()
                .unwrap();
            let cutoff = crate::time::Instant::now() + Duration::from_secs(1);
            for k in 0u32..6 {
                insert_raw(
                    &mut cache,
                    k,
                    k,
                    Some(cutoff - Duration::from_micros(u64::from(k) + 1)),
                );
            }
            insert_raw(&mut cache, 100u32, 100u32, Some(cutoff));

            let caught = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                let _ = cache.evict_at(cutoff);
            }));
            assert!(caught.is_err(), "panic_on={panic_on}: must propagate");
            assert_index_lockstep(&cache, "after a mid-drain callback panic");
            assert_eq!(
                cache.map.len(),
                1,
                "panic_on={panic_on}: every expired entry left the map before any callback"
            );
            assert!(cache.map.contains_key(&100u32));
            assert_eq!(
                cache.cache_evictions(),
                Some(6),
                "panic_on={panic_on}: the counter is bumped before the callbacks"
            );
            assert_eq!(
                seen.load(Ordering::Relaxed),
                panic_on + 1,
                "panic_on={panic_on}: unwinding stops the remaining callbacks"
            );
            // The survivor is still reachable through the index.
            assert_eq!(cache.retain_latest(0, false), 1);
            assert_index_lockstep(&cache, "after trimming the survivor");
        }
    }

    /// The no-callback branch and the callback branch of `evict_at` must produce identical
    /// return values, identical eviction counts and identical resulting state for the same
    /// input. Only the callback branch is directly asserted elsewhere.
    #[test]
    fn evict_at_drain_branches_agree_on_count_state_and_evictions() {
        fn populate(cache: &mut TtlSortedCache<u32, u32>, cutoff: crate::time::Instant) {
            for k in 0u32..24 {
                let expiry = match k % 4 {
                    // Expired, with colliding instants in pairs.
                    0 | 1 => Some(cutoff - Duration::from_micros(u64::from(k / 2) + 1)),
                    // Live, half of them exactly on the tie.
                    2 => Some(cutoff + Duration::from_micros(u64::from(k % 3))),
                    _ => None,
                };
                insert_raw(cache, k, k * 7, expiry);
            }
        }

        let cutoff = crate::time::Instant::now() + Duration::from_secs(1);

        let mut plain = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        populate(&mut plain, cutoff);

        let fired = Arc::new(AtomicUsize::new(0));
        let fired2 = fired.clone();
        let mut with_cb = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .on_evict(move |_k: &u32, _v: &u32| {
                fired2.fetch_add(1, Ordering::Relaxed);
            })
            .build()
            .unwrap();
        populate(&mut with_cb, cutoff);

        let plain_dropped = plain.evict_at(cutoff);
        let cb_dropped = with_cb.evict_at(cutoff);

        assert_eq!(
            plain_dropped, cb_dropped,
            "both drain branches drop the same count"
        );
        assert_eq!(plain_dropped, 12, "half the population is expired");
        assert_eq!(plain.cache_evictions(), with_cb.cache_evictions());
        assert_eq!(plain.cache_evictions(), Some(12));
        assert_eq!(fired.load(Ordering::Relaxed), 12);

        let plain_keys: Vec<u32> = {
            let mut v: Vec<u32> = plain.map.keys().copied().collect();
            v.sort_unstable();
            v
        };
        let cb_keys: Vec<u32> = {
            let mut v: Vec<u32> = with_cb.map.keys().copied().collect();
            v.sort_unstable();
            v
        };
        assert_eq!(plain_keys, cb_keys, "identical surviving map contents");

        let plain_index: Vec<(Option<crate::time::Instant>, Option<u32>)> = plain
            .keys
            .iter()
            .map(|s| (s.expiry, s.key.as_ref().map(|k| *k.0)))
            .collect();
        let cb_index: Vec<(Option<crate::time::Instant>, Option<u32>)> = with_cb
            .keys
            .iter()
            .map(|s| (s.expiry, s.key.as_ref().map(|k| *k.0)))
            .collect();
        assert_eq!(plain_index, cb_index, "identical surviving index contents");
        assert_index_lockstep(&plain, "no-callback branch");
        assert_index_lockstep(&with_cb, "callback branch");
    }

    /// PINS THE ORPHAN DIVERGENCE. An index stamp with no map entry is unreachable through
    /// the public API (see the module certification notes: every insert stamps exactly one
    /// entry and every removal drops the rebuilt stamp), but if it ever occurred, `evict_at`
    /// would COUNT it as a drop in its return value while NOT counting it as an eviction.
    /// This test documents that asymmetry so a future change to either counter is deliberate.
    #[test]
    fn evict_at_orphaned_stamp_is_counted_as_a_drop_but_not_as_an_eviction() {
        for with_callback in [false, true] {
            let fired = Arc::new(AtomicUsize::new(0));
            let fired2 = fired.clone();
            let mut builder = TtlSortedCache::<u32, u32>::builder().ttl(Duration::from_secs(60));
            if with_callback {
                builder = builder.on_evict(move |_k: &u32, _v: &u32| {
                    fired2.fetch_add(1, Ordering::Relaxed);
                });
            }
            let mut cache = builder.build().unwrap();
            let cutoff = crate::time::Instant::now() + Duration::from_secs(1);
            insert_raw(
                &mut cache,
                1u32,
                10u32,
                Some(cutoff - Duration::from_micros(2)),
            );
            // No map entry for key 9.
            insert_orphan_stamp(&mut cache, 9u32, Some(cutoff - Duration::from_micros(1)));
            assert_eq!(cache.keys.len(), 2);
            assert_eq!(cache.map.len(), 1);

            assert_eq!(
                cache.evict_at(cutoff),
                2,
                "with_callback={with_callback}: the return value counts index drops"
            );
            assert_eq!(
                cache.cache_evictions(),
                Some(1),
                "with_callback={with_callback}: only real map removals are evictions"
            );
            assert_eq!(fired.load(Ordering::Relaxed), usize::from(with_callback));
            // The orphan is gone, so the cache is back in lockstep afterwards.
            assert_index_lockstep(&cache, "after sweeping an orphaned stamp");
        }
    }

    /// The same divergence through `retain_latest_at`: `retain_drop_count` is derived from
    /// `map.len()` while the pop loop walks `keys`, so an orphan makes the trim consume one
    /// index slot without freeing a map entry, leaving the cache above the requested count.
    #[test]
    fn retain_latest_at_orphaned_stamp_diverges_from_the_map_length() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        let base = crate::time::Instant::now() + Duration::from_secs(60);
        // The orphan sorts FIRST, so the trim pops it before any real entry.
        insert_orphan_stamp(&mut cache, 9u32, Some(base));
        for k in 0u32..3 {
            insert_raw(
                &mut cache,
                k,
                k,
                Some(base + Duration::from_micros(u64::from(k) + 1)),
            );
        }
        assert_eq!(cache.map.len(), 3);
        assert_eq!(cache.keys.len(), 4);

        // Ask to keep 2 of 3 map entries: one drop. The orphan absorbs it.
        assert_eq!(cache.retain_latest_at(2, None), 1);
        assert_eq!(
            cache.map.len(),
            3,
            "the orphan absorbed the trim, so no map entry was freed"
        );
        assert_eq!(cache.cache_evictions(), Some(0));
        assert_eq!(cache.keys.len(), 3, "the cache is back in lockstep");
        assert_index_lockstep(&cache, "after the orphan absorbed a trim");
        // A second trim now behaves normally.
        assert_eq!(cache.retain_latest_at(2, None), 1);
        assert_eq!(cache.map.len(), 2);
        assert_eq!(cache.cache_evictions(), Some(1));
    }

    /// `retain_latest_at` pops one stamp at a time and removes its map entry, counts the
    /// eviction, and only then fires the callback -- matching `evict_at` / `retain`. So a
    /// panicking callback still leaves the two structures in lockstep AND the entry whose
    /// callback panicked is still counted, since the counter is bumped BEFORE the callback runs.
    #[test]
    fn retain_latest_at_on_evict_panic_leaves_lockstep_and_counts_the_eviction() {
        let seen = Arc::new(AtomicUsize::new(0));
        let seen2 = seen.clone();
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .on_evict(move |_k: &u32, _v: &u32| {
                if seen2.fetch_add(1, Ordering::Relaxed) == 1 {
                    panic!("boom");
                }
            })
            .build()
            .unwrap();
        for k in 0u32..5 {
            cache
                .set_with(k, k * 10)
                .ttl(Duration::from_secs(10 * (u64::from(k) + 1)))
                .set();
        }

        let caught = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let _ = cache.retain_latest(1, false);
        }));
        assert!(caught.is_err(), "the callback panic must propagate");

        assert_index_lockstep(&cache, "after a retain_latest callback panic");
        assert_eq!(
            cache.map.len(),
            3,
            "two entries were removed before the panic"
        );
        assert_eq!(seen.load(Ordering::Relaxed), 2);
        assert_eq!(
            cache.cache_evictions(),
            Some(2),
            "both removals are counted: the counter is bumped before the callback runs"
        );
        // The cache remains usable and fully reachable through the index.
        assert_eq!(cache.retain_latest(0, false), 3);
        assert!(cache.map.is_empty());
        assert!(cache.keys.is_empty());
    }

    /// The tie boundary reached through the SECOND phase of `retain_latest_at` (after the
    /// size trim is satisfied), which uses a separate `expiry < cutoff` comparison rather
    /// than the `split_off` pivot. An entry expiring exactly at the cutoff must stop the
    /// sweep, matching `evict_at`.
    #[test]
    fn retain_latest_at_tie_stops_the_sweep_after_the_size_trim() {
        // 4 entries, keep 3 -> retain_drop_count = 1: entry 1 (strictly before the cutoff) is
        // dropped by the trim, then entry 2 sits exactly ON the cutoff and must stop the sweep.
        let (mut cache, cutoff) = boundary_population();
        assert_eq!(
            cache.retain_latest_at(3, Some(cutoff)),
            1,
            "the tied entry must not be swept by the post-trim expiry loop"
        );
        assert!(!cache.map.contains_key(&1u32));
        assert!(cache.map.contains_key(&2u32), "the exact tie survives");
        assert!(cache.map.contains_key(&3u32));
        assert!(cache.map.contains_key(&4u32));
        assert_index_lockstep(&cache, "after a post-trim tie stop");

        // One tick later the tie IS expired and the sweep continues past the trim.
        let (mut cache, cutoff) = boundary_population();
        assert_eq!(
            cache.retain_latest_at(3, Some(cutoff + Duration::from_nanos(1))),
            2
        );
        assert!(!cache.map.contains_key(&2u32));
        assert!(cache.map.contains_key(&3u32));
        assert_index_lockstep(&cache, "after sweeping past the tie");
    }

    // --- Coarse `Eq`/`Ord` keys: two distinct values that compare equal ------------------

    /// A key whose `Eq`/`Ord`/`Hash` consider only `label`, so two values carrying different
    /// `payload`s compare EQUAL. `set_inner`'s occupied branch reuses the stored `Arc`, and
    /// `BTreeSet::insert` keeps the incumbent on an equal insert, so the index and the map
    /// can hold different-but-equal `Stamped`s.
    #[derive(Clone, Debug)]
    struct CoarseKey {
        label: &'static str,
        payload: u32,
    }

    impl Hash for CoarseKey {
        fn hash<H: Hasher>(&self, state: &mut H) {
            self.label.hash(state);
        }
    }
    impl PartialEq for CoarseKey {
        fn eq(&self, other: &Self) -> bool {
            self.label == other.label
        }
    }
    impl Eq for CoarseKey {}
    impl PartialOrd for CoarseKey {
        fn partial_cmp(&self, other: &Self) -> Option<CmpOrdering> {
            Some(self.cmp(other))
        }
    }
    impl Ord for CoarseKey {
        fn cmp(&self, other: &Self) -> CmpOrdering {
            self.label.cmp(other.label)
        }
    }

    /// Overwriting with an equal-but-distinct key must keep exactly one map entry and one
    /// stamp, and the STORED key is the FIRST-inserted payload (the occupied branch reuses
    /// the existing `Arc`, matching `HashMap`'s own "insert keeps the incumbent key" rule).
    /// `on_evict` and `cache_remove_entry` therefore report the first payload, not the last.
    #[test]
    fn coarse_key_overwrite_keeps_lockstep_and_reports_the_first_stored_key() {
        let seen = Arc::new(std::sync::Mutex::new(Vec::<u32>::new()));
        let seen2 = seen.clone();
        let mut cache = TtlSortedCache::<CoarseKey, u32>::builder()
            .ttl(Duration::from_secs(60))
            .on_evict(move |k: &CoarseKey, _v: &u32| seen2.lock().unwrap().push(k.payload))
            .build()
            .unwrap();

        let first = CoarseKey {
            label: "a",
            payload: 1,
        };
        let second = CoarseKey {
            label: "a",
            payload: 2,
        };
        assert_eq!(first, second, "the two keys compare equal");
        assert_eq!(first.cmp(&second), CmpOrdering::Equal);

        cache.cache_set(first.clone(), 10u32);
        assert_eq!(cache.cache_set(second.clone(), 20u32), Some(10u32));
        assert_eq!(cache.map.len(), 1);
        assert_eq!(cache.keys.len(), 1, "no duplicate stamp for an equal key");
        assert_index_lockstep(&cache, "after a coarse-key overwrite");

        // The stored key is the first payload, and the index stamp shares its `Arc`.
        let entry = cache.map.values().next().expect("one entry");
        assert_eq!(
            entry.key.0.payload, 1,
            "the occupied branch reuses the originally stored key"
        );
        let stamp = cache.keys.iter().next().expect("one stamp");
        assert!(
            Arc::ptr_eq(
                &stamp.key.as_ref().expect("real key").0,
                &cache.map.values().next().expect("one entry").key.0
            ),
            "the index stamp and the map entry share the same key Arc"
        );

        // Removal by the SECOND (equal) key still finds the entry, and hands the callback and
        // the `cache_remove_entry` return the FIRST payload.
        let removed = cache.cache_remove_entry(&second).expect("present");
        assert_eq!(removed.0.payload, 1, "the stored key is returned");
        assert_eq!(removed.1, 20u32);
        assert_eq!(*seen.lock().unwrap(), vec![1u32]);
        assert!(cache.map.is_empty());
        assert!(cache.keys.is_empty());
        assert_index_lockstep(&cache, "after a coarse-key removal");
    }

    /// The same coarse key across an expiry change: the stale stamp must be removed by the
    /// rebuilt `old.as_stamped()`, which carries the shared `Arc`, so no duplicate survives
    /// and the sweep order follows the NEW expiry.
    #[test]
    fn coarse_key_overwrite_with_a_new_expiry_replaces_exactly_one_stamp() {
        let mut cache = TtlSortedCache::<CoarseKey, u32>::builder()
            .ttl(Duration::from_secs(60))
            .build()
            .unwrap();
        let a1 = CoarseKey {
            label: "a",
            payload: 1,
        };
        let a2 = CoarseKey {
            label: "a",
            payload: 2,
        };
        let b = CoarseKey {
            label: "b",
            payload: 9,
        };
        cache.set_with(a1, 10u32).ttl(Duration::ZERO).set();
        cache.set_with(b, 90u32).ttl(Duration::from_secs(30)).set();
        assert_index_lockstep(&cache, "coarse pair");

        // UNCHANGED expiry (None -> None) with a different-but-equal key: the new stamp is
        // Ord-equal to the incumbent, so `BTreeSet::insert` is a no-op and the stale-stamp
        // removal MUST be skipped -- removing it here would delete the only stamp for a live
        // map entry and orphan it.
        assert_eq!(
            cache.set_with(a2.clone(), 10u32).ttl(Duration::ZERO).set(),
            Some(10u32)
        );
        assert_eq!(cache.map.len(), 2);
        assert_eq!(
            cache.keys.len(),
            2,
            "the single stamp survives the overwrite"
        );
        assert_index_lockstep(&cache, "after a same-expiry coarse-key overwrite");

        // None -> Some: the never-expires stamp must go.
        assert_eq!(
            cache
                .set_with(a2.clone(), 11u32)
                .ttl(Duration::from_secs(5))
                .set(),
            Some(10u32)
        );
        assert_eq!(cache.cache_evictions(), Some(0), "no expired displacement");
        assert_eq!(cache.map.len(), 2);
        assert_eq!(cache.keys.len(), 2, "the stale stamp is gone");
        assert_index_lockstep(&cache, "after a coarse-key re-stamp");

        // "a" now expires first, so it is trimmed first.
        assert_eq!(cache.retain_latest(1, false), 1);
        assert!(!cache.map.contains_key(&a2));
        assert_index_lockstep(&cache, "after trimming the re-stamped coarse key");
    }

    /// A long mixed sequence over the public surface: the lockstep invariant must hold after
    /// every single operation. This is the empirical half of the "an orphaned stamp is
    /// unreachable by construction" claim.
    #[test]
    fn lockstep_holds_across_a_mixed_public_operation_sequence() {
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_millis(30))
            .max_size(5)
            .on_evict(|_k: &u32, _v: &u32| {})
            .build()
            .unwrap();
        for round in 0u32..40 {
            let k = round % 7;
            match round % 8 {
                0 => {
                    cache.set(k, round);
                }
                1 => {
                    cache.set_with(k, round).ttl(Duration::ZERO).set();
                }
                2 => {
                    cache
                        .set_with(k, round)
                        .ttl(Duration::from_millis(10))
                        .evict()
                        .set();
                }
                3 => {
                    let _ = cache.cache_get(&k);
                }
                4 => {
                    let _ = cache.cache_remove(&k);
                }
                5 => {
                    let _ = cache.cache_get_or_set_with_mut(k, || round);
                }
                6 => {
                    cache.retain(|kk, _v| *kk != round % 5);
                }
                _ => {
                    let _ = cache.retain_latest(3, round % 16 == 7);
                }
            }
            assert_index_lockstep(&cache, "mixed sequence");
            assert!(cache.map.len() <= 5 || round % 8 == 5);
        }
        let _ = cache.evict();
        assert_index_lockstep(&cache, "after the final sweep");
        cache.cache_clear();
        assert_index_lockstep(&cache, "after clear");
    }

    /// `set_inner` samples the clock ONCE and judges the displaced entry against that sample.
    /// The classification itself is pinned here with exact expiries; the sub-microsecond
    /// window between the sample and the map write is NOT observable from a test (see the
    /// certification notes) because the store exposes no clock injection point.
    #[test]
    fn set_inner_classifies_the_displaced_entry_against_its_clock_sample() {
        let fired = Arc::new(AtomicUsize::new(0));
        let fired2 = fired.clone();
        let mut cache = TtlSortedCache::<u32, u32>::builder()
            .ttl(Duration::from_secs(60))
            .on_evict(move |_k: &u32, _v: &u32| {
                fired2.fetch_add(1, Ordering::Relaxed);
            })
            .build()
            .unwrap();

        // Displaced entry already past its deadline -> filtered from the return, evicted.
        insert_raw(
            &mut cache,
            1u32,
            10u32,
            Some(crate::time::Instant::now() - Duration::from_nanos(1)),
        );
        assert_eq!(cache.cache_set(1u32, 11u32), None);
        assert_eq!(fired.load(Ordering::Relaxed), 1);
        assert_eq!(cache.cache_evictions(), Some(1));
        assert_index_lockstep(&cache, "after displacing an expired entry");

        // Displaced entry comfortably in the future -> returned as live, no eviction.
        insert_raw(
            &mut cache,
            2u32,
            20u32,
            Some(crate::time::Instant::now() + Duration::from_secs(3600)),
        );
        assert_eq!(cache.cache_set(2u32, 21u32), Some(20u32));
        assert_eq!(fired.load(Ordering::Relaxed), 1);
        assert_eq!(cache.cache_evictions(), Some(1));
        assert_index_lockstep(&cache, "after displacing a live entry");
    }

    // --- Item 7: the async liveness-check behavior change -------------------------------

    /// (c) A PANICKING factory future: the expired entry is left in place, `on_evict` never
    /// fires and nothing is counted. Under the previous `cache_get`-based shape the entry was
    /// already swept (and `on_evict` already fired) before the future was ever polled.
    #[cfg(feature = "async")]
    #[tokio::test]
    async fn async_cache_get_or_set_with_mut_factory_panic_keeps_the_expired_entry() {
        use crate::CachedGetOrSetAsync;

        let fired = Arc::new(AtomicUsize::new(0));
        let fired2 = fired.clone();
        let mut c: TtlSortedCache<u32, u32> = TtlSortedCache::builder()
            .ttl(Duration::from_millis(20))
            .on_evict(move |_k: &u32, _v: &u32| {
                fired2.fetch_add(1, Ordering::Relaxed);
            })
            .build()
            .unwrap();
        c.cache_set(1u32, 100u32);
        tokio::time::sleep(std::time::Duration::from_millis(60)).await;
        c.cache_reset_metrics();

        {
            let mut fut = Box::pin(CachedGetOrSetAsync::async_cache_get_or_set_with_mut(
                &mut c,
                1u32,
                || async { panic!("factory boom") },
            ));
            let waker = std::task::Waker::noop();
            let mut cx = std::task::Context::from_waker(waker);
            let caught = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                let _ = std::future::Future::poll(fut.as_mut(), &mut cx);
            }));
            assert!(caught.is_err(), "the factory panic must propagate");
        }

        assert_eq!(
            fired.load(Ordering::Relaxed),
            0,
            "on_evict must not fire when the factory panics"
        );
        assert_eq!(c.cache_evictions(), Some(0));
        assert_eq!(c.cache_size(), 1, "the expired entry is still stored");
        assert_eq!(
            c.cache_misses(),
            Some(1),
            "the miss is counted before the factory runs"
        );
        // The stale value is still observable through the expiry-status peek.
        assert_eq!(
            crate::CloneCached::cache_peek_with_expiry_status(&c, &1u32),
            (Some(100u32), true),
            "the stale value survives the panic and is reported as expired"
        );
        assert_index_lockstep(&c, "after an async factory panic");
    }

    /// (a) NORMAL COMPLETION: the eviction of the displaced expired entry now happens AFTER
    /// the factory resolves, not before it is polled. Observed through the callback ordering.
    #[cfg(feature = "async")]
    #[tokio::test]
    async fn async_cache_get_or_set_with_mut_fires_on_evict_after_the_factory_completes() {
        use crate::CachedGetOrSetAsync;

        let log = Arc::new(std::sync::Mutex::new(Vec::<&'static str>::new()));
        let log2 = log.clone();
        let mut c: TtlSortedCache<u32, u32> = TtlSortedCache::builder()
            .ttl(Duration::from_millis(20))
            .on_evict(move |_k: &u32, _v: &u32| log2.lock().unwrap().push("evict"))
            .build()
            .unwrap();
        c.cache_set(1u32, 100u32);
        tokio::time::sleep(std::time::Duration::from_millis(60)).await;

        let log3 = log.clone();
        let v = CachedGetOrSetAsync::async_cache_get_or_set_with_mut(&mut c, 1u32, || async move {
            log3.lock().unwrap().push("factory");
            200u32
        })
        .await;
        assert_eq!(*v, 200u32);
        assert_eq!(
            *log.lock().unwrap(),
            vec!["factory", "evict"],
            "the displaced expired entry is evicted after the factory resolves"
        );
        assert_eq!(c.cache_evictions(), Some(1));
        assert_eq!(c.cache_size(), 1);
        assert_index_lockstep(&c, "after an async replacement");
    }

    /// The fallible async sibling now uses the same in-place liveness check as the other
    /// three `cache_*get_or_set_with_mut` variants instead of routing through `cache_get`
    /// (which would sweep the expired entry, firing `on_evict`, before the factory is ever
    /// polled). On `Err` the expired entry is left exactly in place, matching the sync
    /// fallible sibling.
    #[cfg(feature = "async")]
    #[tokio::test]
    async fn async_try_get_or_set_with_mut_leaves_the_expired_entry_alone_on_err() {
        use crate::CachedGetOrSetAsync;

        let fired = Arc::new(AtomicUsize::new(0));
        let fired2 = fired.clone();
        let mut c: TtlSortedCache<u32, u32> = TtlSortedCache::builder()
            .ttl(Duration::from_millis(20))
            .on_evict(move |_k: &u32, _v: &u32| {
                fired2.fetch_add(1, Ordering::Relaxed);
            })
            .build()
            .unwrap();
        c.cache_set(1u32, 100u32);
        tokio::time::sleep(std::time::Duration::from_millis(60)).await;
        c.cache_reset_metrics();

        let out: Result<&mut u32, &'static str> =
            CachedGetOrSetAsync::async_cache_try_get_or_set_with_mut(&mut c, 1u32, || async {
                Err("nope")
            })
            .await;
        assert_eq!(out.err(), Some("nope"));
        assert_eq!(
            c.cache_size(),
            1,
            "the fallible async path leaves the expired entry alone on Err"
        );
        assert_eq!(fired.load(Ordering::Relaxed), 0, "on_evict must not fire");
        assert_eq!(c.cache_evictions(), Some(0));
        assert_eq!(
            c.cache_misses(),
            Some(1),
            "the miss is counted before the factory runs"
        );
        assert_index_lockstep(&c, "after a failed async try factory");

        // The SYNC fallible sibling agrees: the expired entry is left in place.
        let mut c2: TtlSortedCache<u32, u32> = TtlSortedCache::builder()
            .ttl(Duration::from_millis(20))
            .build()
            .unwrap();
        c2.cache_set(1u32, 100u32);
        std::thread::sleep(std::time::Duration::from_millis(60));
        let out: Result<&mut u32, &'static str> =
            c2.cache_try_get_or_set_with_mut(1u32, || Err("nope"));
        assert_eq!(out.err(), Some("nope"));
        assert_eq!(
            c2.cache_size(),
            1,
            "the sync fallible path leaves the expired entry alone"
        );
        assert_eq!(c2.cache_evictions(), Some(0));
    }

    /// (b) CANCELLATION, contrasted directly between the two async methods on an identical
    /// starting state. Both now agree: the expired entry survives an in-flight future being
    /// dropped, and `on_evict` never fires for it.
    #[cfg(feature = "async")]
    #[tokio::test]
    async fn async_get_or_set_variants_agree_on_cancellation_over_an_expired_entry() {
        use crate::CachedGetOrSetAsync;

        async fn expired_cache() -> (TtlSortedCache<u32, u32>, Arc<AtomicUsize>) {
            let fired = Arc::new(AtomicUsize::new(0));
            let fired2 = fired.clone();
            let mut c: TtlSortedCache<u32, u32> = TtlSortedCache::builder()
                .ttl(Duration::from_millis(20))
                .on_evict(move |_k: &u32, _v: &u32| {
                    fired2.fetch_add(1, Ordering::Relaxed);
                })
                .build()
                .unwrap();
            c.cache_set(1u32, 100u32);
            tokio::time::sleep(std::time::Duration::from_millis(60)).await;
            (c, fired)
        }

        // Infallible: nothing observable happens to the entry.
        let (mut c, fired) = expired_cache().await;
        {
            let mut fut = Box::pin(CachedGetOrSetAsync::async_cache_get_or_set_with_mut(
                &mut c,
                1u32,
                std::future::pending::<u32>,
            ));
            let waker = std::task::Waker::noop();
            let mut cx = std::task::Context::from_waker(waker);
            assert!(matches!(
                std::future::Future::poll(fut.as_mut(), &mut cx),
                std::task::Poll::Pending
            ));
        }
        assert_eq!(c.cache_size(), 1);
        assert_eq!(fired.load(Ordering::Relaxed), 0);
        assert_eq!(c.cache_evictions(), Some(0));
        assert_index_lockstep(&c, "after a cancelled infallible async factory");

        // Fallible: now agrees -- the expired entry is likewise left alone by cancellation.
        let (mut c, fired) = expired_cache().await;
        {
            let mut fut = Box::pin(CachedGetOrSetAsync::async_cache_try_get_or_set_with_mut::<
                _,
                _,
                &'static str,
            >(&mut c, 1u32, || {
                std::future::pending::<Result<u32, &'static str>>()
            }));
            let waker = std::task::Waker::noop();
            let mut cx = std::task::Context::from_waker(waker);
            assert!(matches!(
                std::future::Future::poll(fut.as_mut(), &mut cx),
                std::task::Poll::Pending
            ));
        }
        assert_eq!(
            c.cache_size(),
            1,
            "the fallible async path also leaves the expired entry alone when cancelled"
        );
        assert_eq!(fired.load(Ordering::Relaxed), 0);
        assert_eq!(c.cache_evictions(), Some(0));
        assert_index_lockstep(&c, "after a cancelled fallible async factory");
    }

    /// (d) `Ok` completion for the fallible async variant: net end state/counters match the
    /// pre-fix behavior, but `on_evict` now fires AFTER the factory resolves (observed via
    /// callback ordering) and the insert takes the OCCUPIED `set_inner` branch (reusing the
    /// stored key `Arc`) rather than a vacant one, because the expired entry was never swept.
    #[cfg(feature = "async")]
    #[tokio::test]
    async fn async_try_get_or_set_with_mut_fires_on_evict_after_the_factory_completes() {
        use crate::CachedGetOrSetAsync;

        let log = Arc::new(std::sync::Mutex::new(Vec::<&'static str>::new()));
        let log2 = log.clone();
        let mut c: TtlSortedCache<u32, u32> = TtlSortedCache::builder()
            .ttl(Duration::from_millis(20))
            .on_evict(move |_k: &u32, _v: &u32| log2.lock().unwrap().push("evict"))
            .build()
            .unwrap();
        c.cache_set(1u32, 100u32);
        tokio::time::sleep(std::time::Duration::from_millis(60)).await;

        let log3 = log.clone();
        let out: Result<&mut u32, &'static str> =
            CachedGetOrSetAsync::async_cache_try_get_or_set_with_mut(&mut c, 1u32, || async move {
                log3.lock().unwrap().push("factory");
                Ok(200u32)
            })
            .await;
        assert_eq!(out.ok(), Some(&mut 200u32));
        assert_eq!(
            *log.lock().unwrap(),
            vec!["factory", "evict"],
            "the displaced expired entry is evicted after the factory resolves"
        );
        assert_eq!(c.cache_evictions(), Some(1));
        assert_eq!(c.cache_size(), 1);
        assert_index_lockstep(&c, "after an async fallible replacement");
    }

    /// Cross-variant agreement table: sync infallible, sync fallible, async infallible, and
    /// async fallible must all behave identically on an expired-entry key for each terminal
    /// outcome the family shares, so a future change to one cannot silently desync the group.
    #[cfg(feature = "async")]
    #[tokio::test]
    async fn get_or_set_family_agrees_on_expired_entry_across_variants() {
        use crate::CachedGetOrSetAsync;

        fn expired_cache() -> TtlSortedCache<u32, u32> {
            let mut c: TtlSortedCache<u32, u32> = TtlSortedCache::builder()
                .ttl(Duration::from_millis(1))
                .build()
                .unwrap();
            c.cache_set(1u32, 100u32);
            std::thread::sleep(std::time::Duration::from_millis(20));
            c
        }

        /// Same as `expired_cache`, but wired with an `on_evict` that appends to a shared
        /// log, so the Ok-outcome sub-block below can pin *when* the displaced expired entry
        /// is evicted relative to the factory, not just the net counts. This is the
        /// discriminator the plain count assertions below cannot see: reverting the fix-2
        /// liveness check to an eager `cache_get`-based sweep still nets `cache_size() == 1`
        /// and `cache_evictions() == Some(1)` for the Ok outcome (the eager sweep evicts, then
        /// the factory's value is inserted fresh), so only the callback order and `cache_get`
        /// on-hit accounting expose that regression on this outcome.
        type Log = std::sync::Arc<std::sync::Mutex<Vec<&'static str>>>;
        fn expired_cache_with_log() -> (TtlSortedCache<u32, u32>, Log) {
            let log: Log = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
            let log2 = log.clone();
            let mut c: TtlSortedCache<u32, u32> = TtlSortedCache::builder()
                .ttl(Duration::from_millis(1))
                .on_evict(move |_k: &u32, _v: &u32| log2.lock().unwrap().push("evict"))
                .build()
                .unwrap();
            c.cache_set(1u32, 100u32);
            std::thread::sleep(std::time::Duration::from_millis(20));
            (c, log)
        }

        // --- Ok / infallible-success outcome: all four end up with the factory's value,
        // fire on_evict exactly once AFTER the factory runs, and count exactly one eviction. ---
        {
            let (mut sync_infallible, log) = expired_cache_with_log();
            let log2 = log.clone();
            let v = sync_infallible.cache_get_or_set_with_mut(1u32, || {
                log2.lock().unwrap().push("factory");
                200u32
            });
            assert_eq!(*v, 200u32);
            assert_eq!(sync_infallible.cache_size(), 1);
            assert_eq!(sync_infallible.cache_evictions(), Some(1));
            assert_eq!(
                *log.lock().unwrap(),
                vec!["factory", "evict"],
                "sync infallible: on_evict fires after the factory"
            );
            assert_index_lockstep(&sync_infallible, "sync infallible Ok outcome");

            let (mut sync_try, log) = expired_cache_with_log();
            let log2 = log.clone();
            let v: Result<&mut u32, &'static str> =
                sync_try.cache_try_get_or_set_with_mut(1u32, || {
                    log2.lock().unwrap().push("factory");
                    Ok(200u32)
                });
            assert_eq!(v.ok(), Some(&mut 200u32));
            assert_eq!(sync_try.cache_size(), 1);
            assert_eq!(sync_try.cache_evictions(), Some(1));
            assert_eq!(
                *log.lock().unwrap(),
                vec!["factory", "evict"],
                "sync try: on_evict fires after the factory"
            );
            assert_index_lockstep(&sync_try, "sync try Ok outcome");

            let (mut async_infallible, log) = expired_cache_with_log();
            let log2 = log.clone();
            let v = CachedGetOrSetAsync::async_cache_get_or_set_with_mut(
                &mut async_infallible,
                1u32,
                || async move {
                    log2.lock().unwrap().push("factory");
                    200u32
                },
            )
            .await;
            assert_eq!(*v, 200u32);
            assert_eq!(async_infallible.cache_size(), 1);
            assert_eq!(async_infallible.cache_evictions(), Some(1));
            assert_eq!(
                *log.lock().unwrap(),
                vec!["factory", "evict"],
                "async infallible: on_evict fires after the factory, not eagerly on the check"
            );
            assert_index_lockstep(&async_infallible, "async infallible Ok outcome");

            let (mut async_try, log) = expired_cache_with_log();
            let log2 = log.clone();
            let v: Result<&mut u32, &'static str> =
                CachedGetOrSetAsync::async_cache_try_get_or_set_with_mut(
                    &mut async_try,
                    1u32,
                    || async move {
                        log2.lock().unwrap().push("factory");
                        Ok(200u32)
                    },
                )
                .await;
            assert_eq!(v.ok(), Some(&mut 200u32));
            assert_eq!(
                *log.lock().unwrap(),
                vec!["factory", "evict"],
                "async try: on_evict fires after the factory, not eagerly on the check"
            );
            assert_eq!(async_try.cache_size(), 1);
            assert_eq!(async_try.cache_evictions(), Some(1));
            assert_index_lockstep(&async_try, "async try Ok outcome");
        }

        // --- Err outcome (fallible variants only): the expired entry is left exactly alone,
        // no eviction fires or is counted. ---
        {
            let mut sync_try = expired_cache();
            let v: Result<&mut u32, &'static str> =
                sync_try.cache_try_get_or_set_with_mut(1u32, || Err("nope"));
            assert_eq!(v.err(), Some("nope"));
            assert_eq!(sync_try.cache_size(), 1);
            assert_eq!(sync_try.cache_evictions(), Some(0));
            assert_index_lockstep(&sync_try, "sync try Err outcome");

            let mut async_try = expired_cache();
            let v: Result<&mut u32, &'static str> =
                CachedGetOrSetAsync::async_cache_try_get_or_set_with_mut(
                    &mut async_try,
                    1u32,
                    || async { Err("nope") },
                )
                .await;
            assert_eq!(v.err(), Some("nope"));
            assert_eq!(async_try.cache_size(), 1);
            assert_eq!(async_try.cache_evictions(), Some(0));
            assert_index_lockstep(&async_try, "async try Err outcome");
        }

        // --- Cancellation (async variants only): dropping the future mid-await leaves the
        // expired entry exactly alone, no eviction fires or is counted. ---
        {
            let mut async_infallible = expired_cache();
            {
                let mut fut = Box::pin(CachedGetOrSetAsync::async_cache_get_or_set_with_mut(
                    &mut async_infallible,
                    1u32,
                    std::future::pending::<u32>,
                ));
                let waker = std::task::Waker::noop();
                let mut cx = std::task::Context::from_waker(waker);
                assert!(matches!(
                    std::future::Future::poll(fut.as_mut(), &mut cx),
                    std::task::Poll::Pending
                ));
            }
            assert_eq!(async_infallible.cache_size(), 1);
            assert_eq!(async_infallible.cache_evictions(), Some(0));
            assert_index_lockstep(&async_infallible, "async infallible cancellation");

            let mut async_try = expired_cache();
            {
                let mut fut = Box::pin(CachedGetOrSetAsync::async_cache_try_get_or_set_with_mut::<
                    _,
                    _,
                    &'static str,
                >(&mut async_try, 1u32, || {
                    std::future::pending::<Result<u32, &'static str>>()
                }));
                let waker = std::task::Waker::noop();
                let mut cx = std::task::Context::from_waker(waker);
                assert!(matches!(
                    std::future::Future::poll(fut.as_mut(), &mut cx),
                    std::task::Poll::Pending
                ));
            }
            assert_eq!(async_try.cache_size(), 1);
            assert_eq!(async_try.cache_evictions(), Some(0));
            assert_index_lockstep(&async_try, "async try cancellation");
        }
    }
}