hydracache 0.43.0

User-facing HydraCache runtime crate.
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
use std::error::Error;
use std::future::Future;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Instant;

use bytes::Bytes;
use futures_util::FutureExt;
use hydracache_core::{
    CacheCodec, CacheDiagnostics, CacheError, CacheEvent, CacheEventKind, CacheEventOptions,
    CacheEventOrigin, CacheOptions, CacheStats, PostcardCodec, Result,
};
use moka::future::Cache;
use serde::{de::DeserializeOwned, Serialize};
use tokio::sync::watch;

use crate::builder::HydraCacheBuilder;
use crate::cluster::{
    ClusterCacheCounters, ClusterDiagnostics, ClusterDiscoveryDiagnostics, ClusterFillCounters,
    ClusterMembershipEvent, ClusterMembershipSubscriber, ClusterNodeId,
    ClusterOwnershipDiagnostics, ClusterPilotReadiness, ClusterPilotReport, ClusterRuntime,
    ClusterStagingCounters, ClusterStagingHealth, HydraCacheClientBuilder, HydraCacheMemberBuilder,
    RoutingMode, TopologyFence, TransportPosture,
};
use crate::entry::CacheEntry;
use crate::events::{CacheEventListenerHandle, CacheEventSubscriber, EventBus};
use crate::grid::{ClusterGridCounters, ReplicatedValueSecurityPosture, ReplicationConfig};
use crate::inflight::{InFlightMap, SharedLoadFuture};
use crate::invalidation_bus::{
    CacheInvalidation, CacheInvalidationBus, CacheInvalidationMessage, CacheInvalidationReceive,
};
use crate::refresh::RefreshOptions;
use crate::stats::StatsCounters;
use crate::tag_index::{LoadGenerationSnapshot, TagIndex};
use crate::typed::TypedCache;

/// Local async cache runtime.
///
/// `HydraCache` stores encoded values in a local Moka-backed cache and exposes
/// async helpers for loader-based caching, TTLs, tags, explicit invalidation,
/// local single-flight, and lightweight stats.
///
/// # Example
///
/// ```rust
/// use hydracache::{CacheOptions, HydraCache};
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
/// struct User {
///     id: u64,
/// }
///
/// # #[tokio::main]
/// # async fn main() -> hydracache::CacheResult<()> {
/// let cache = HydraCache::local().build();
///
/// cache.put("user:1", User { id: 1 }, CacheOptions::new()).await?;
/// let cached: Option<User> = cache.get("user:1").await?;
///
/// assert_eq!(cached, Some(User { id: 1 }));
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct HydraCache<C = PostcardCodec>
where
    C: CacheCodec,
{
    pub(crate) inner: Arc<HydraCacheInner<C>>,
}

#[derive(Debug)]
pub(crate) struct HydraCacheInner<C>
where
    C: CacheCodec,
{
    pub(crate) store: Cache<String, CacheEntry>,
    pub(crate) tag_index: TagIndex,
    pub(crate) in_flight: InFlightMap,
    pub(crate) codec: C,
    pub(crate) default_ttl: std::time::Duration,
    pub(crate) max_entry_bytes: usize,
    pub(crate) stats: Arc<StatsCounters>,
    pub(crate) events: EventBus,
    pub(crate) invalidation_bus: Option<Arc<dyn CacheInvalidationBus>>,
    pub(crate) invalidation_node_id: String,
    pub(crate) consistency_generation: AtomicU64,
    pub(crate) bus_shutdown: Option<watch::Sender<bool>>,
    pub(crate) cluster_runtime: Option<ClusterRuntime>,
    pub(crate) transport_posture: TransportPosture,
    pub(crate) routing_mode: RoutingMode,
    pub(crate) read_through_enabled: bool,
    pub(crate) replication_config: ReplicationConfig,
    pub(crate) replicated_value_security: ReplicatedValueSecurityPosture,
}

impl<C> Drop for HydraCacheInner<C>
where
    C: CacheCodec,
{
    fn drop(&mut self) {
        if let Some(shutdown) = &self.bus_shutdown {
            let _ = shutdown.send(true);
        }
    }
}

impl HydraCache<PostcardCodec> {
    /// Start building a local cache.
    ///
    /// # Example
    ///
    /// ```rust
    /// use hydracache::HydraCache;
    ///
    /// let cache = HydraCache::local().build();
    /// ```
    pub fn local() -> HydraCacheBuilder<PostcardCodec> {
        HydraCacheBuilder::default()
    }

    /// Start building a client near-cache connected to a HydraCache cluster.
    ///
    /// v0.20 provides an in-process cluster model for tests and demos. Network
    /// discovery and Raft-backed membership are intentionally future adapters.
    pub fn client() -> HydraCacheClientBuilder<PostcardCodec> {
        HydraCacheClientBuilder::default()
    }

    /// Start building an in-process cluster member.
    ///
    /// Members participate in the in-memory invalidation bus today and provide
    /// the API shape for future chitchat/Raft-backed cluster runtimes.
    pub fn member() -> HydraCacheMemberBuilder<PostcardCodec> {
        HydraCacheMemberBuilder::default()
    }
}

impl<C> HydraCache<C>
where
    C: CacheCodec,
{
    /// Create a typed, namespaced view over this cache.
    ///
    /// The typed view prefixes physical keys as `namespace:key` while sharing
    /// the same storage, stats, single-flight map, tags, and invalidation
    /// generations.
    ///
    /// # Example
    ///
    /// ```rust
    /// use hydracache::{CacheOptions, HydraCache};
    /// use serde::{Deserialize, Serialize};
    ///
    /// #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    /// struct User {
    ///     id: u64,
    /// }
    ///
    /// # #[tokio::main]
    /// # async fn main() -> hydracache::CacheResult<()> {
    /// let cache = HydraCache::local().build();
    /// let users = cache.typed::<User>("users");
    ///
    /// users.put("1", User { id: 1 }, CacheOptions::new()).await?;
    /// assert_eq!(users.get("1").await?, Some(User { id: 1 }));
    /// # Ok(())
    /// # }
    /// ```
    pub fn typed<T>(&self, namespace: impl Into<String>) -> TypedCache<T, C> {
        TypedCache::new(self.clone(), namespace.into())
    }

    /// Return this cache instance's invalidation node id.
    ///
    /// The id is included in bus messages and lets each cache ignore messages it
    /// originally published.
    pub fn invalidation_node_id(&self) -> &str {
        &self.inner.invalidation_node_id
    }

    /// Return cluster diagnostics when this cache was built as a client or member.
    ///
    /// Local caches return `None`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::sync::Arc;
    ///
    /// use hydracache::{HydraCache, InMemoryCluster};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> hydracache::CacheResult<()> {
    /// let cluster = Arc::new(InMemoryCluster::new("orders"));
    /// let client = HydraCache::client()
    ///     .shared_cluster(cluster)
    ///     .node_id("client-a")
    ///     .connect()
    ///     .await?;
    ///
    /// let diagnostics = client.cluster_diagnostics().expect("cluster runtime");
    /// assert_eq!(diagnostics.client_count, 1);
    /// assert!(diagnostics.lifecycle.is_running());
    ///
    /// client.leave_cluster().await?;
    /// assert!(client.cluster_diagnostics().unwrap().lifecycle.is_stopped());
    /// # Ok(())
    /// # }
    /// ```
    pub fn cluster_diagnostics(&self) -> Option<ClusterDiagnostics> {
        self.inner
            .cluster_runtime
            .as_ref()
            .map(ClusterRuntime::diagnostics)
    }

    /// Return ownership diagnostics when this cache was built as a client or member.
    ///
    /// Local caches return `None`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::sync::Arc;
    ///
    /// use hydracache::{HydraCache, InMemoryCluster};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> hydracache::CacheResult<()> {
    /// let cluster = Arc::new(InMemoryCluster::new("orders"));
    /// let member = HydraCache::member()
    ///     .shared_cluster(cluster.clone())
    ///     .node_id("member-a")
    ///     .start()
    ///     .await?;
    ///
    /// let _owner = cluster.owner_for_key("user:42");
    /// let ownership = member
    ///     .cluster_ownership_diagnostics()
    ///     .expect("cluster runtime");
    ///
    /// assert_eq!(ownership.resolver, "rendezvous");
    /// assert_eq!(ownership.resolutions, 1);
    /// # Ok(())
    /// # }
    /// ```
    pub fn cluster_ownership_diagnostics(&self) -> Option<ClusterOwnershipDiagnostics> {
        self.inner
            .cluster_runtime
            .as_ref()
            .map(ClusterRuntime::ownership_diagnostics)
    }

    /// Return discovery diagnostics when this cache was built with discovery.
    ///
    /// Local caches and client/member caches without a discovery adapter return
    /// `None`.
    pub fn cluster_discovery_diagnostics(&self) -> Option<ClusterDiscoveryDiagnostics> {
        self.inner
            .cluster_runtime
            .as_ref()
            .and_then(ClusterRuntime::discovery_diagnostics)
    }

    /// Return owner-load, remote-fetch, and hot-cache hit counters.
    pub fn cluster_fill_counters(&self) -> ClusterFillCounters {
        ClusterFillCounters {
            owner_load_success: self
                .inner
                .stats
                .cluster_owner_load_success
                .load(Ordering::Relaxed),
            owner_load_errors: self
                .inner
                .stats
                .cluster_owner_load_errors
                .load(Ordering::Relaxed),
            remote_fetch_success: self
                .inner
                .stats
                .cluster_remote_fetch_success
                .load(Ordering::Relaxed),
            remote_fetch_errors: self
                .inner
                .stats
                .cluster_remote_fetch_errors
                .load(Ordering::Relaxed),
            hot_cache_hits: self
                .inner
                .stats
                .cluster_hot_cache_hits
                .load(Ordering::Relaxed),
        }
    }

    /// Return cluster staging counters that are not part of local cache stats.
    pub fn cluster_staging_counters(&self) -> ClusterStagingCounters {
        ClusterStagingCounters {
            peer_fetch_auth_failures: self
                .inner
                .stats
                .cluster_peer_fetch_auth_failures
                .load(Ordering::Relaxed),
            wire_version_rejections: self
                .inner
                .stats
                .cluster_wire_version_rejections
                .load(Ordering::Relaxed),
            stale_generation_rejected: self
                .inner
                .stats
                .cluster_stale_generation_rejected
                .load(Ordering::Relaxed),
            tombstone_age_ms: self
                .inner
                .stats
                .cluster_gossip_tombstone_age_ms
                .load(Ordering::Relaxed),
            gossip_reset_count: self
                .inner
                .stats
                .cluster_gossip_reset_count
                .load(Ordering::Relaxed),
            barrier_timeouts: self
                .inner
                .stats
                .cluster_barrier_timeouts
                .load(Ordering::Relaxed),
            near_cache_conservative_invalidations: self
                .inner
                .stats
                .cluster_near_cache_conservative_invalidations
                .load(Ordering::Relaxed),
            lifecycle_stop_count: self
                .inner
                .stats
                .cluster_lifecycle_stop_count
                .load(Ordering::Relaxed),
            lifecycle_restart_count: self
                .inner
                .stats
                .cluster_lifecycle_restart_count
                .load(Ordering::Relaxed),
        }
    }

    /// Return aggregate 0.41 distributed-grid counters.
    pub fn cluster_grid_counters(&self) -> ClusterGridCounters {
        ClusterGridCounters {
            replication_success_total: self
                .inner
                .stats
                .cluster_replication_success_total
                .load(Ordering::Relaxed),
            replication_failure_total: self
                .inner
                .stats
                .cluster_replication_failure_total
                .load(Ordering::Relaxed),
            bytes_replicated_total: self
                .inner
                .stats
                .cluster_bytes_replicated_total
                .load(Ordering::Relaxed),
            replication_backpressure_total: self
                .inner
                .stats
                .cluster_replication_backpressure_total
                .load(Ordering::Relaxed),
            replication_oversized_rejected_total: self
                .inner
                .stats
                .cluster_replication_oversized_rejected_total
                .load(Ordering::Relaxed),
            replication_decrypt_failure_total: self
                .inner
                .stats
                .cluster_replication_decrypt_failure_total
                .load(Ordering::Relaxed),
            under_replicated_keys: self
                .inner
                .stats
                .cluster_under_replicated_keys
                .load(Ordering::Relaxed),
            failover_total: self
                .inner
                .stats
                .cluster_failover_total
                .load(Ordering::Relaxed),
            repair_task_total: self
                .inner
                .stats
                .cluster_repair_task_total
                .load(Ordering::Relaxed),
            repair_failure_total: self
                .inner
                .stats
                .cluster_repair_failure_total
                .load(Ordering::Relaxed),
            rebalance_plan_total: self
                .inner
                .stats
                .cluster_rebalance_plan_total
                .load(Ordering::Relaxed),
            rebalance_task_ack_total: self
                .inner
                .stats
                .cluster_rebalance_task_ack_total
                .load(Ordering::Relaxed),
            topology_fence_rejected_total: self
                .inner
                .stats
                .cluster_topology_fence_rejected_total
                .load(Ordering::Relaxed),
            tombstone_repair_debt: self
                .inner
                .stats
                .cluster_tombstone_repair_debt
                .load(Ordering::Relaxed),
            replicated_value_rejected_total: 0,
            split_brain_detected_total: 0,
            merge_discarded_entries_total: 0,
            merge_unresolved_conflicts_total: 0,
            cluster_auth_rejected_total: 0,
            repair_debt_degraded_mode: 0,
            placement_zone_underspread: 0,
            reshard_moves_inflight: 0,
            reshard_backfill_lag: 0,
            read_local_zone_total: 0,
            read_hedged_total: 0,
            read_hedge_win_total: 0,
            value_tier_promotions_total: 0,
            value_tier_demotions_total: 0,
            invalidate_batch_total: 0,
            invalidation_saga_pending: 0,
            auto_repair_active_total: 0,
            auto_repair_advisory_total: 0,
        }
    }

    /// Return the configured value-replication shape.
    pub fn replication_config(&self) -> ReplicationConfig {
        self.inner.replication_config
    }

    /// Return the replicated-value confidentiality posture.
    pub fn replicated_value_security_posture(&self) -> ReplicatedValueSecurityPosture {
        self.inner.replicated_value_security
    }

    /// Return the declared transport-security posture.
    pub fn transport_posture(&self) -> TransportPosture {
        self.inner.transport_posture
    }

    /// Return the configured client routing mode.
    pub fn routing_mode(&self) -> RoutingMode {
        self.inner.routing_mode
    }

    /// Return whether cluster read-through / remote peer-fetch is enabled.
    pub fn read_through_enabled(&self) -> bool {
        self.inner.read_through_enabled
    }

    /// Return a topology fence from the latest cluster diagnostics.
    pub fn cluster_topology_fence(&self) -> TopologyFence {
        let committed_epoch = self
            .cluster_diagnostics()
            .map(|diagnostics| diagnostics.epoch)
            .unwrap_or_default();
        TopologyFence::new(committed_epoch)
    }

    /// Return the boolean pilot readiness contract.
    pub fn cluster_pilot_readiness(&self) -> ClusterPilotReadiness {
        let diagnostics = self.cluster_diagnostics();
        let stats = self.stats();
        let member_count = diagnostics
            .as_ref()
            .map(|diagnostics| diagnostics.member_count)
            .unwrap_or_default();
        let lifecycle_operational = diagnostics
            .as_ref()
            .map(|diagnostics| diagnostics.lifecycle.is_running())
            .unwrap_or(false);
        let topology_committed = diagnostics
            .as_ref()
            .map(|diagnostics| diagnostics.epoch > Default::default())
            .unwrap_or(false);

        ClusterPilotReadiness {
            transport_posture: self.transport_posture(),
            has_members: member_count > 0,
            member_count,
            within_supported_size: (2..=5).contains(&member_count),
            strict_wire_compatibility: self.transport_posture().wire_strict,
            diagnostics_clean: stats.distributed_invalidation_decode_errors == 0
                && stats.distributed_invalidation_publish_failures == 0
                && stats.distributed_invalidation_receiver_closed == 0,
            lifecycle_operational,
            topology_committed,
        }
    }

    /// Return a dashboard-ready pilot report.
    pub fn cluster_pilot_report(&self) -> ClusterPilotReport {
        let readiness = self.cluster_pilot_readiness();
        let diagnostics = self.cluster_diagnostics();
        let ownership = self.cluster_ownership_diagnostics();
        let stats = self.stats();
        let fill = self.cluster_fill_counters();
        let staging = self.cluster_staging_counters();
        let transport_posture = self.transport_posture();

        ClusterPilotReport {
            readiness,
            counters: ClusterCacheCounters::from(fill),
            epoch: diagnostics
                .as_ref()
                .map(|diagnostics| diagnostics.epoch.value())
                .unwrap_or_default(),
            generation: diagnostics
                .as_ref()
                .map(|diagnostics| diagnostics.generation.value())
                .unwrap_or_default(),
            stamp: ownership
                .as_ref()
                .map(|diagnostics| diagnostics.stamp)
                .unwrap_or_default(),
            invalidations_published: stats.distributed_invalidations_published,
            invalidations_received: stats.distributed_invalidations_received,
            invalidations_applied: stats.distributed_invalidations_applied,
            invalidation_lagged: stats.distributed_invalidation_lagged,
            decode_errors: stats.distributed_invalidation_decode_errors,
            publish_failures: stats.distributed_invalidation_publish_failures,
            receiver_closed: stats.distributed_invalidation_receiver_closed,
            owner_load_success: fill.owner_load_success,
            owner_load_errors: fill.owner_load_errors,
            remote_fetch_success: fill.remote_fetch_success,
            remote_fetch_errors: fill.remote_fetch_errors,
            auth_failures: staging.peer_fetch_auth_failures,
            wire_version_failures: staging.wire_version_rejections,
            stale_generation_rejections: staging.stale_generation_rejected,
            barrier_timeouts: staging.barrier_timeouts,
            near_cache_conservative_invalidations: staging.near_cache_conservative_invalidations,
            lifecycle_stop_count: staging.lifecycle_stop_count,
            lifecycle_restart_count: staging.lifecycle_restart_count,
            transport_posture,
            highlights: transport_posture
                .highlight()
                .into_iter()
                .chain(self.replicated_value_security_posture().highlight())
                .map(str::to_owned)
                .collect(),
        }
    }

    /// Return a staging-focused cluster health summary.
    ///
    /// Local caches return `None`; client/member caches return a derived
    /// machine-readable health state and all counters used to compute it.
    pub fn cluster_staging_health(&self) -> Option<ClusterStagingHealth> {
        let diagnostics = self.cluster_diagnostics()?;
        Some(ClusterStagingHealth::from_parts(
            diagnostics,
            self.stats(),
            self.cluster_fill_counters(),
            self.cluster_staging_counters(),
        ))
    }

    /// Record a successful owner-side origin load for staging diagnostics.
    pub fn record_cluster_owner_load_success(&self) {
        self.inner
            .stats
            .cluster_owner_load_success
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Record a failed owner-side origin load for staging diagnostics.
    pub fn record_cluster_owner_load_error(&self) {
        self.inner
            .stats
            .cluster_owner_load_errors
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Record a successful remote peer-fetch for staging diagnostics.
    pub fn record_cluster_remote_fetch_success(&self) {
        self.inner
            .stats
            .cluster_remote_fetch_success
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Record a failed remote peer-fetch for staging diagnostics.
    pub fn record_cluster_remote_fetch_error(&self) {
        self.inner
            .stats
            .cluster_remote_fetch_errors
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Record a hot near-cache hit for a non-owned value.
    pub fn record_cluster_hot_cache_hit(&self) {
        self.inner
            .stats
            .cluster_hot_cache_hits
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Record a peer-fetch or owner-load auth failure.
    pub fn record_cluster_peer_fetch_auth_failure(&self) {
        self.inner
            .stats
            .cluster_peer_fetch_auth_failures
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Record a peer-fetch or owner-load wire-version rejection.
    pub fn record_cluster_wire_version_rejection(&self) {
        self.inner
            .stats
            .cluster_wire_version_rejections
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Record a stale-generation rejection observed by staging fencing checks.
    pub fn record_cluster_stale_generation_rejected(&self) {
        self.inner
            .stats
            .cluster_stale_generation_rejected
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Record a gossip reset/tombstone diagnostic.
    pub fn record_cluster_gossip_reset(&self, tombstone_age_ms: u64) {
        self.inner
            .stats
            .cluster_gossip_tombstone_age_ms
            .store(tombstone_age_ms, Ordering::Relaxed);
        self.inner
            .stats
            .cluster_gossip_reset_count
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Record a read-after-write/quorum barrier timeout.
    pub fn record_cluster_barrier_timeout(&self) {
        self.inner
            .stats
            .cluster_barrier_timeouts
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Record a conservative invalidation caused by near-cache repair.
    pub fn record_cluster_near_cache_conservative_invalidation(&self) {
        self.inner
            .stats
            .cluster_near_cache_conservative_invalidations
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Record a successful replicated value/tombstone send.
    pub fn record_cluster_replication_success(&self, bytes: u64) {
        self.inner
            .stats
            .cluster_replication_success_total
            .fetch_add(1, Ordering::Relaxed);
        self.inner
            .stats
            .cluster_bytes_replicated_total
            .fetch_add(bytes, Ordering::Relaxed);
    }

    /// Record a failed replicated value/tombstone send.
    pub fn record_cluster_replication_failure(&self) {
        self.inner
            .stats
            .cluster_replication_failure_total
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Record replication queue backpressure.
    pub fn record_cluster_replication_backpressure(&self) {
        self.inner
            .stats
            .cluster_replication_backpressure_total
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Record a value rejected by the replication byte cap.
    pub fn record_cluster_replication_oversized_rejected(&self) {
        self.inner
            .stats
            .cluster_replication_oversized_rejected_total
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Record a replicated payload decrypt/open failure.
    pub fn record_cluster_replication_decrypt_failure(&self) {
        self.inner
            .stats
            .cluster_replication_decrypt_failure_total
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Set the aggregate under-replicated key gauge.
    pub fn set_cluster_under_replicated_keys(&self, value: u64) {
        self.inner
            .stats
            .cluster_under_replicated_keys
            .store(value, Ordering::Relaxed);
    }

    /// Record a topology-fence rejection.
    pub fn record_cluster_topology_fence_rejected(&self) {
        self.inner
            .stats
            .cluster_topology_fence_rejected_total
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Set the aggregate tombstone repair-debt gauge.
    pub fn set_cluster_tombstone_repair_debt(&self, value: u64) {
        self.inner
            .stats
            .cluster_tombstone_repair_debt
            .store(value, Ordering::Relaxed);
    }

    /// Record a lifecycle stop observed by a pilot probe.
    pub fn record_cluster_lifecycle_stop(&self) {
        self.inner
            .stats
            .cluster_lifecycle_stop_count
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Record a lifecycle restart/rejoin observed by a pilot probe.
    pub fn record_cluster_lifecycle_restart(&self) {
        self.inner
            .stats
            .cluster_lifecycle_restart_count
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Record a synthetic direct-routing remote fetch for pilot routing tests.
    pub fn record_cluster_direct_remote_fetch(&self) -> Result<()> {
        if !self.read_through_enabled() {
            return Ok(());
        }
        match self.routing_mode() {
            RoutingMode::Direct => self.record_cluster_remote_fetch_success(),
            RoutingMode::SingleEndpoint => self.record_cluster_hot_cache_hit(),
        }
        Ok(())
    }

    /// Subscribe to membership events for this cache's attached cluster runtime.
    ///
    /// Local caches return `None`. Client/member caches return a bounded stream
    /// of events emitted after subscription; slow subscribers may receive lag
    /// errors.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::sync::Arc;
    ///
    /// use hydracache::{ClusterMembershipEvent, HydraCache, InMemoryCluster};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> hydracache::CacheResult<()> {
    /// let cluster = Arc::new(InMemoryCluster::new("orders"));
    /// let member = HydraCache::member()
    ///     .shared_cluster(cluster.clone())
    ///     .node_id("member-a")
    ///     .start()
    ///     .await?;
    /// let mut events = member
    ///     .subscribe_cluster_membership()
    ///     .expect("member has a cluster runtime");
    ///
    /// let _client = HydraCache::client()
    ///     .shared_cluster(cluster)
    ///     .node_id("client-a")
    ///     .connect()
    ///     .await?;
    ///
    /// assert!(matches!(
    ///     events.recv().await.expect("membership event"),
    ///     ClusterMembershipEvent::ClientConnected(_)
    /// ));
    /// # Ok(())
    /// # }
    /// ```
    pub fn subscribe_cluster_membership(&self) -> Option<ClusterMembershipSubscriber> {
        self.inner
            .cluster_runtime
            .as_ref()
            .map(ClusterRuntime::subscribe_membership)
    }

    /// Leave the attached cluster runtime when this cache is a client or member.
    ///
    /// Local caches return `Ok(None)`. A client/member cache returns the
    /// control-plane leave event when the node was still admitted.
    ///
    /// This removes cluster membership metadata, but it does not clear the
    /// local cache contents.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::sync::Arc;
    ///
    /// use hydracache::{ClusterMembershipEvent, ClusterRole, HydraCache, InMemoryCluster};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> hydracache::CacheResult<()> {
    /// let cluster = Arc::new(InMemoryCluster::new("orders"));
    /// let client = HydraCache::client()
    ///     .shared_cluster(cluster)
    ///     .node_id("client-a")
    ///     .connect()
    ///     .await?;
    ///
    /// let left = client.leave_cluster().await?.expect("client was admitted");
    /// assert!(matches!(
    ///     left,
    ///     ClusterMembershipEvent::NodeLeft {
    ///         role: ClusterRole::Client,
    ///         ..
    ///     }
    /// ));
    /// assert!(client.cluster_diagnostics().unwrap().lifecycle.is_stopped());
    /// # Ok(())
    /// # }
    /// ```
    pub async fn leave_cluster(&self) -> Result<Option<ClusterMembershipEvent>> {
        match &self.inner.cluster_runtime {
            Some(runtime) => runtime.leave().await,
            None => Ok(None),
        }
    }

    /// Subscribe to cache events matching the provided filters.
    ///
    /// Dropping the returned subscriber unregisters it. Access/load events are
    /// only published when the cache was built with
    /// [`HydraCacheBuilder::enable_access_events`].
    ///
    /// # Example
    ///
    /// ```rust
    /// use hydracache::{CacheEventKind, CacheEventOptions, CacheOptions, HydraCache};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> hydracache::CacheResult<()> {
    /// let cache = HydraCache::local().build();
    /// let mut events = cache.subscribe(
    ///     CacheEventOptions::mutations().include_kind(CacheEventKind::Stored),
    /// );
    ///
    /// cache.put("answer", 42_u64, CacheOptions::new()).await?;
    ///
    /// let event = events.recv().await.expect("stored event");
    /// assert_eq!(event.kind(), CacheEventKind::Stored);
    /// assert_eq!(event.key(), Some("answer"));
    /// # Ok(())
    /// # }
    /// ```
    pub fn subscribe(&self, options: CacheEventOptions) -> CacheEventSubscriber {
        self.inner
            .events
            .subscribe(options, self.inner.stats.clone())
    }

    /// Subscribe to mutation and invalidation events.
    pub fn subscribe_mutations(&self) -> CacheEventSubscriber {
        self.subscribe(CacheEventOptions::mutations())
    }

    /// Subscribe to access and loader events.
    ///
    /// These events are published only when the cache is built with
    /// [`HydraCacheBuilder::enable_access_events`].
    pub fn subscribe_access(&self) -> CacheEventSubscriber {
        self.subscribe(CacheEventOptions::access())
    }

    /// Subscribe to events for one exact physical key.
    pub fn subscribe_key(&self, key: impl Into<String>) -> CacheEventSubscriber {
        self.subscribe(CacheEventOptions::new().key(key))
    }

    /// Subscribe to events associated with one tag.
    pub fn subscribe_tag(&self, tag: impl Into<String>) -> CacheEventSubscriber {
        self.subscribe(CacheEventOptions::new().tag(tag))
    }

    /// Run a callback for events matching the provided filters.
    ///
    /// The callback runs in a background task over a normal event subscription;
    /// it is never executed directly by cache operations.
    pub fn add_listener<F>(
        &self,
        options: CacheEventOptions,
        listener: F,
    ) -> CacheEventListenerHandle
    where
        F: Fn(CacheEvent) + Send + 'static,
    {
        CacheEventListenerHandle::spawn(self.subscribe(options), listener)
    }

    /// Run a callback for mutation and invalidation events.
    pub fn on_mutation<F>(&self, listener: F) -> CacheEventListenerHandle
    where
        F: Fn(CacheEvent) + Send + 'static,
    {
        self.add_listener(CacheEventOptions::mutations(), listener)
    }

    /// Run a callback for access and loader events.
    ///
    /// These events are published only when the cache is built with
    /// [`HydraCacheBuilder::enable_access_events`].
    pub fn on_access<F>(&self, listener: F) -> CacheEventListenerHandle
    where
        F: Fn(CacheEvent) + Send + 'static,
    {
        self.add_listener(CacheEventOptions::access(), listener)
    }

    /// Get and decode a cached value.
    ///
    /// Returns `Ok(None)` when the key is missing or expired.
    pub async fn get<T>(&self, key: &str) -> Result<Option<T>>
    where
        T: DeserializeOwned,
    {
        match self.inner.store.get(key).await {
            Some(entry) if entry.is_expired() => {
                self.remove_expired(key, &entry).await;
                self.inner.stats.misses.fetch_add(1, Ordering::Relaxed);
                self.publish_key_event_with_tags(
                    CacheEventKind::Miss,
                    key,
                    CacheEventOrigin::LocalApi,
                    || entry.tags.clone(),
                );
                Ok(None)
            }
            Some(entry) => match self.inner.codec.decode::<T>(&entry.value) {
                Ok(value) => {
                    self.inner.stats.hits.fetch_add(1, Ordering::Relaxed);
                    self.publish_key_event_with_tags(
                        CacheEventKind::Hit,
                        key,
                        CacheEventOrigin::LocalApi,
                        || entry.tags.clone(),
                    );
                    Ok(Some(value))
                }
                Err(error) => {
                    self.remove_entry(key, &entry).await;
                    self.inner.stats.misses.fetch_add(1, Ordering::Relaxed);
                    self.publish_key_event_with_tags(
                        CacheEventKind::Miss,
                        key,
                        CacheEventOrigin::LocalApi,
                        || entry.tags.clone(),
                    );
                    Err(error)
                }
            },
            None => {
                self.inner.stats.misses.fetch_add(1, Ordering::Relaxed);
                self.publish_key_event(
                    CacheEventKind::Miss,
                    key,
                    CacheEventOrigin::LocalApi,
                    Vec::<String>::new(),
                );
                Ok(None)
            }
        }
    }

    /// Get the encoded bytes stored for a key.
    ///
    /// This is mainly intended for transport adapters that need to move an
    /// already-encoded value between cache members without knowing the
    /// application type. Most application code should prefer [`get`](Self::get)
    /// or [`get_or_insert_with`](Self::get_or_insert_with).
    ///
    /// Expiration, hit/miss counters, and access events follow the same rules
    /// as [`get`](Self::get).
    ///
    /// # Example
    ///
    /// ```rust
    /// use hydracache::{CacheOptions, HydraCache};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> hydracache::CacheResult<()> {
    /// let cache = HydraCache::local().build();
    ///
    /// cache.put("answer", 42_u64, CacheOptions::new()).await?;
    ///
    /// let encoded = cache.get_encoded("answer").await?;
    /// assert!(encoded.is_some());
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_encoded(&self, key: &str) -> Result<Option<Bytes>> {
        match self.inner.store.get(key).await {
            Some(entry) if entry.is_expired() => {
                self.remove_expired(key, &entry).await;
                self.inner.stats.misses.fetch_add(1, Ordering::Relaxed);
                self.publish_key_event_with_tags(
                    CacheEventKind::Miss,
                    key,
                    CacheEventOrigin::LocalApi,
                    || entry.tags.clone(),
                );
                Ok(None)
            }
            Some(entry) => {
                self.inner.stats.hits.fetch_add(1, Ordering::Relaxed);
                self.publish_key_event_with_tags(
                    CacheEventKind::Hit,
                    key,
                    CacheEventOrigin::LocalApi,
                    || entry.tags.clone(),
                );
                Ok(Some(entry.value))
            }
            None => {
                self.inner.stats.misses.fetch_add(1, Ordering::Relaxed);
                self.publish_key_event(
                    CacheEventKind::Miss,
                    key,
                    CacheEventOrigin::LocalApi,
                    Vec::<String>::new(),
                );
                Ok(None)
            }
        }
    }

    /// Store already-encoded bytes for a key.
    ///
    /// This is mainly intended for transport adapters and cluster near-cache
    /// hydration. The bytes must have been produced by a compatible
    /// [`CacheCodec`]; `HydraCache` stores them as-is and will decode them on a
    /// later typed [`get`](Self::get).
    ///
    /// TTLs, tags, tag-index updates, store events, and diagnostics follow the
    /// same rules as [`put`](Self::put).
    ///
    /// # Example
    ///
    /// ```rust
    /// use hydracache::{CacheOptions, HydraCache};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> hydracache::CacheResult<()> {
    /// let source = HydraCache::local().build();
    /// let target = HydraCache::local().build();
    ///
    /// source.put("answer", 42_u64, CacheOptions::new()).await?;
    /// let encoded = source
    ///     .get_encoded("answer")
    ///     .await?
    ///     .expect("source value is cached");
    ///
    /// target
    ///     .put_encoded("answer", encoded, CacheOptions::new().tag("answers"))
    ///     .await?;
    ///
    /// assert_eq!(target.get::<u64>("answer").await?, Some(42));
    /// # Ok(())
    /// # }
    /// ```
    pub async fn put_encoded(
        &self,
        key: &str,
        value: impl Into<Bytes>,
        options: CacheOptions,
    ) -> Result<()> {
        self.put_bytes(key, value.into(), options).await
    }

    /// Encode and store a value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use hydracache::{CacheOptions, HydraCache};
    /// use serde::{Deserialize, Serialize};
    ///
    /// #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    /// struct User {
    ///     id: u64,
    /// }
    ///
    /// # #[tokio::main]
    /// # async fn main() -> hydracache::CacheResult<()> {
    /// let cache = HydraCache::local().build();
    ///
    /// cache.put("user:1", User { id: 1 }, CacheOptions::new()).await?;
    /// assert_eq!(cache.get::<User>("user:1").await?, Some(User { id: 1 }));
    /// # Ok(())
    /// # }
    /// ```
    pub async fn put<T>(&self, key: &str, value: T, options: CacheOptions) -> Result<()>
    where
        T: Serialize,
    {
        let bytes = self.inner.codec.encode(&value)?;
        self.put_bytes(key, bytes, options).await
    }

    /// Get a value, or run the loader and cache its result on miss.
    ///
    /// Concurrent misses for the same key share one loader execution.
    ///
    /// # Example
    ///
    /// ```rust
    /// use hydracache::{CacheOptions, HydraCache};
    /// use serde::{Deserialize, Serialize};
    ///
    /// #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    /// struct User {
    ///     id: u64,
    /// }
    ///
    /// #[derive(Debug)]
    /// struct LoaderError;
    ///
    /// impl std::fmt::Display for LoaderError {
    ///     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    ///         f.write_str("loader failed")
    ///     }
    /// }
    ///
    /// impl std::error::Error for LoaderError {}
    ///
    /// # #[tokio::main]
    /// # async fn main() -> hydracache::CacheResult<()> {
    /// let cache = HydraCache::local().build();
    ///
    /// let user = cache
    ///     .get_or_load("user:1", CacheOptions::new(), || async {
    ///         Ok::<_, LoaderError>(User { id: 1 })
    ///     })
    ///     .await?;
    ///
    /// assert_eq!(user, User { id: 1 });
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_or_load<T, E, F, Fut>(
        &self,
        key: &str,
        options: CacheOptions,
        loader: F,
    ) -> Result<T>
    where
        T: Serialize + DeserializeOwned,
        E: Error + Send + Sync + 'static,
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = std::result::Result<T, E>> + Send + 'static,
    {
        if let Some(value) = self.get(key).await? {
            return Ok(value);
        }

        let shared = self
            .shared_load(key, options, move |cache| async move {
                cache.inner.stats.loads.fetch_add(1, Ordering::Relaxed);
                let value = loader().await.map_err(CacheError::loader)?;
                let bytes = cache.inner.codec.encode(&value)?;
                Ok(bytes)
            })
            .await;

        let bytes = shared.await.map_err(|error| (*error).clone())?;
        self.inner.codec.decode(&bytes)
    }

    /// Get a value with explicit refresh/stale behavior.
    ///
    /// This is the production-oriented sibling of [`HydraCache::get_or_load`].
    /// It keeps the same single-flight and invalidation-safety semantics, but
    /// can return a stale value while refreshing it in the background.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::time::Duration;
    ///
    /// use hydracache::{CacheOptions, HydraCache, RefreshOptions};
    /// use serde::{Deserialize, Serialize};
    ///
    /// #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    /// struct User {
    ///     id: u64,
    /// }
    ///
    /// #[derive(Debug)]
    /// struct LoaderError;
    ///
    /// impl std::fmt::Display for LoaderError {
    ///     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    ///         f.write_str("loader failed")
    ///     }
    /// }
    ///
    /// impl std::error::Error for LoaderError {}
    ///
    /// # #[tokio::main]
    /// # async fn main() -> hydracache::CacheResult<()> {
    /// let cache = HydraCache::local().build();
    ///
    /// let user = cache
    ///     .get_or_load_with_refresh(
    ///         "user:1",
    ///         CacheOptions::new().ttl(Duration::from_secs(60)).tag("user:1"),
    ///         RefreshOptions::new()
    ///             .refresh_ahead(Duration::from_secs(10))
    ///             .stale_while_revalidate(Duration::from_secs(300))
    ///             .serve_stale_on_loader_error(true),
    ///         || async { Ok::<_, LoaderError>(User { id: 1 }) },
    ///     )
    ///     .await?;
    ///
    /// assert_eq!(user, User { id: 1 });
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_or_load_with_refresh<T, E, F, Fut>(
        &self,
        key: &str,
        options: CacheOptions,
        refresh_options: RefreshOptions,
        loader: F,
    ) -> Result<T>
    where
        T: Serialize + DeserializeOwned + Send + 'static,
        E: Error + Send + Sync + 'static,
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = std::result::Result<T, E>> + Send + 'static,
    {
        let mut loader = Some(loader);

        if let Some(entry) = self.inner.store.get(key).await {
            if !entry.is_expired() {
                let value = self.decode_cached_hit(key, &entry).await?;
                if refresh_options
                    .refresh_ahead_value()
                    .is_some_and(|threshold| entry.refresh_ahead_due(threshold))
                {
                    self.spawn_background_refresh(
                        key.to_owned(),
                        options,
                        take_loader(&mut loader),
                    );
                }
                return Ok(value);
            }

            let in_stale_while_revalidate_window =
                entry_in_stale_window(&entry, refresh_options.stale_while_revalidate_value());

            if in_stale_while_revalidate_window {
                match self.decode_cached_hit(key, &entry).await {
                    Ok(value) => {
                        self.spawn_background_refresh(
                            key.to_owned(),
                            options,
                            take_loader(&mut loader),
                        );
                        return Ok(value);
                    }
                    Err(_) => {
                        self.remove_entry(key, &entry).await;
                    }
                }
            } else {
                self.remove_expired(key, &entry).await;
            }

            self.record_miss(key, || entry.tags.clone());

            let fallback = refresh_options
                .serve_stale_on_loader_error_value()
                .then(|| {
                    entry_in_stale_window(&entry, refresh_options.stale_on_loader_error_window())
                        .then(|| entry.clone())
                })
                .flatten();

            return self
                .load_and_decode_with_stale_fallback(
                    key,
                    options,
                    take_loader(&mut loader),
                    fallback,
                )
                .await;
        }

        self.record_miss(key, Vec::<String>::new);
        self.load_and_decode_with_stale_fallback(key, options, take_loader(&mut loader), None)
            .await
    }

    /// Get a value, or compute and cache it with an infallible async loader.
    ///
    /// This is the most ergonomic local-cache spelling for loaders that cannot
    /// fail in application terms. Fallible loaders should use `try_get_or_insert_with`
    /// or `get_or_load`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use hydracache::{CacheOptions, HydraCache};
    /// use serde::{Deserialize, Serialize};
    ///
    /// #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    /// struct User {
    ///     id: u64,
    /// }
    ///
    /// # #[tokio::main]
    /// # async fn main() -> hydracache::CacheResult<()> {
    /// let cache = HydraCache::local().build();
    ///
    /// let user = cache
    ///     .get_or_insert_with("user:1", CacheOptions::new(), || async { User { id: 1 } })
    ///     .await?;
    ///
    /// assert_eq!(user, User { id: 1 });
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_or_insert_with<T, F, Fut>(
        &self,
        key: &str,
        options: CacheOptions,
        loader: F,
    ) -> Result<T>
    where
        T: Serialize + DeserializeOwned,
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = T> + Send + 'static,
    {
        self.get_or_load(key, options, move || async move {
            Ok::<_, std::convert::Infallible>(loader().await)
        })
        .await
    }

    /// Get a value, or run a fallible async loader and cache its result on miss.
    ///
    /// This is an alias for `get_or_load` with a name that mirrors common
    /// cache-map APIs.
    pub async fn try_get_or_insert_with<T, E, F, Fut>(
        &self,
        key: &str,
        options: CacheOptions,
        loader: F,
    ) -> Result<T>
    where
        T: Serialize + DeserializeOwned,
        E: Error + Send + Sync + 'static,
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = std::result::Result<T, E>> + Send + 'static,
    {
        self.get_or_load(key, options, loader).await
    }

    /// Remove one key from the cache.
    pub async fn invalidate_key(&self, key: &str) -> Result<bool> {
        let removed = self
            .remove_with_event(
                key,
                CacheEventKind::KeyInvalidated,
                CacheEventOrigin::LocalApi,
            )
            .await?;
        self.publish_invalidation(CacheInvalidation::key(key))
            .await?;
        Ok(removed)
    }

    /// Remove one key from the cache.
    ///
    /// This is an alias for `invalidate_key` with a shorter name for local-cache use.
    pub async fn remove(&self, key: &str) -> Result<bool> {
        let removed = self
            .remove_with_event(key, CacheEventKind::Removed, CacheEventOrigin::LocalApi)
            .await?;
        self.publish_invalidation(CacheInvalidation::key(key))
            .await?;
        Ok(removed)
    }

    /// Return whether the key currently maps to a usable value.
    ///
    /// Expired entries are removed and reported as absent.
    pub async fn contains_key(&self, key: &str) -> bool {
        match self.inner.store.get(key).await {
            Some(entry) if entry.is_expired() => {
                self.remove_expired(key, &entry).await;
                false
            }
            Some(_) => true,
            None => false,
        }
    }

    /// Remove all entries currently associated with a tag.
    ///
    /// Tag invalidation also advances the tag generation. Tagged loaders that
    /// started before the invalidation will return to their caller but skip
    /// storing stale values back into the cache.
    pub async fn invalidate_tag(&self, tag: &str) -> Result<u64> {
        let removed = self
            .invalidate_tag_with_origin(tag, CacheEventOrigin::LocalApi)
            .await?;
        self.publish_invalidation(CacheInvalidation::tag(tag))
            .await?;
        Ok(removed)
    }

    async fn invalidate_tag_with_origin(&self, tag: &str, origin: CacheEventOrigin) -> Result<u64> {
        let keys = self.inner.tag_index.take_tag(tag).await;
        let mut removed = 0;

        for key in keys {
            if let Some(entry) = self.inner.store.get(&key).await {
                self.remove_entry(&key, &entry).await;
                removed += 1;
            }
        }

        if removed > 0 {
            self.inner
                .stats
                .invalidations
                .fetch_add(removed, Ordering::Relaxed);
        }

        self.publish_tag_event(CacheEventKind::TagInvalidated, tag, removed, origin);

        Ok(removed)
    }

    /// Remove all cached entries and tag mappings.
    pub async fn flush(&self) -> Result<()> {
        self.flush_with_origin(CacheEventOrigin::LocalApi).await?;
        self.publish_invalidation(CacheInvalidation::flush()).await
    }

    async fn flush_with_origin(&self, origin: CacheEventOrigin) -> Result<()> {
        let estimated_entries = self.inner.store.entry_count();
        self.inner.store.invalidate_all();
        self.inner.tag_index.clear().await;
        self.publish_cache_event(CacheEventKind::Flushed, Some(estimated_entries), origin);
        Ok(())
    }

    /// Return a snapshot of lightweight cache counters.
    ///
    /// # Example
    ///
    /// ```rust
    /// use hydracache::{CacheOptions, HydraCache};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> hydracache::CacheResult<()> {
    /// let cache = HydraCache::local().build();
    ///
    /// let first = cache
    ///     .get_or_insert_with("answer", CacheOptions::new(), || async { 42_u64 })
    ///     .await?;
    /// let second = cache
    ///     .get_or_insert_with("answer", CacheOptions::new(), || async { 7_u64 })
    ///     .await?;
    ///
    /// let stats = cache.stats();
    /// assert_eq!((first, second), (42, 42));
    /// assert_eq!(stats.loads, 1);
    /// assert_eq!(stats.hits, 1);
    /// assert_eq!(stats.hit_ratio(), Some(0.5));
    /// # Ok(())
    /// # }
    /// ```
    pub fn stats(&self) -> CacheStats {
        self.inner.stats.snapshot()
    }

    /// Return a diagnostic snapshot for quick application-level smoke checks.
    ///
    /// `diagnostics` includes [`CacheStats`] plus the local backend's
    /// approximate entry count. Use it to answer questions like "did this call
    /// hit the cache on the second run?" without wiring a metrics system yet.
    ///
    /// # Example
    ///
    /// ```rust
    /// use hydracache::{CacheOptions, HydraCache};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> hydracache::CacheResult<()> {
    /// let cache = HydraCache::local().build();
    ///
    /// cache
    ///     .get_or_insert_with("report:daily", CacheOptions::new(), || async { 1_u64 })
    ///     .await?;
    /// cache
    ///     .get_or_insert_with("report:daily", CacheOptions::new(), || async { 2_u64 })
    ///     .await?;
    ///
    /// let diagnostics = cache.diagnostics().await;
    /// assert_eq!(diagnostics.stats.loads, 1);
    /// assert_eq!(diagnostics.stats.hits, 1);
    /// assert_eq!(diagnostics.total_requests(), 2);
    /// assert!(!diagnostics.is_empty());
    /// # Ok(())
    /// # }
    /// ```
    pub async fn diagnostics(&self) -> CacheDiagnostics {
        self.inner.store.run_pending_tasks().await;
        CacheDiagnostics {
            stats: self.stats(),
            estimated_entries: self.inner.store.entry_count(),
        }
    }

    async fn decode_cached_hit<T>(&self, key: &str, entry: &CacheEntry) -> Result<T>
    where
        T: DeserializeOwned,
    {
        match self.inner.codec.decode::<T>(&entry.value) {
            Ok(value) => {
                self.inner.stats.hits.fetch_add(1, Ordering::Relaxed);
                self.publish_key_event_with_tags(
                    CacheEventKind::Hit,
                    key,
                    CacheEventOrigin::LocalApi,
                    || entry.tags.clone(),
                );
                Ok(value)
            }
            Err(error) => {
                self.remove_entry(key, entry).await;
                self.record_miss(key, || entry.tags.clone());
                Err(error)
            }
        }
    }

    fn record_miss<I, S>(&self, key: &str, tags: I)
    where
        I: FnOnce() -> S,
        S: IntoIterator,
        S::Item: Into<String>,
    {
        self.inner.stats.misses.fetch_add(1, Ordering::Relaxed);
        self.publish_key_event_with_tags(
            CacheEventKind::Miss,
            key,
            CacheEventOrigin::LocalApi,
            tags,
        );
    }

    fn spawn_background_refresh<T, E, F, Fut>(&self, key: String, options: CacheOptions, loader: F)
    where
        T: Serialize + Send + 'static,
        E: Error + Send + Sync + 'static,
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = std::result::Result<T, E>> + Send + 'static,
    {
        let cache = self.clone();
        tokio::spawn(async move {
            let shared = cache
                .shared_load(&key, options, move |cache| async move {
                    cache.inner.stats.loads.fetch_add(1, Ordering::Relaxed);
                    let value = loader().await.map_err(CacheError::loader)?;
                    let bytes = cache.inner.codec.encode(&value)?;
                    Ok(bytes)
                })
                .await;
            let _ = shared.await;
        });
    }

    async fn load_and_decode_with_stale_fallback<T, E, F, Fut>(
        &self,
        key: &str,
        options: CacheOptions,
        loader: F,
        stale_entry: Option<CacheEntry>,
    ) -> Result<T>
    where
        T: Serialize + DeserializeOwned + Send + 'static,
        E: Error + Send + Sync + 'static,
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = std::result::Result<T, E>> + Send + 'static,
    {
        let shared = self
            .shared_load(key, options, move |cache| async move {
                cache.inner.stats.loads.fetch_add(1, Ordering::Relaxed);
                let value = loader().await.map_err(CacheError::loader)?;
                let bytes = cache.inner.codec.encode(&value)?;
                Ok(bytes)
            })
            .await;

        match shared.await {
            Ok(bytes) => self.inner.codec.decode(&bytes),
            Err(error) => {
                if let Some(entry) = stale_entry {
                    match self.decode_cached_hit(key, &entry).await {
                        Ok(value) => return Ok(value),
                        Err(_) => {
                            self.remove_entry(key, &entry).await;
                        }
                    }
                }
                Err((*error).clone())
            }
        }
    }

    pub(crate) async fn put_bytes(
        &self,
        key: &str,
        value: Bytes,
        options: CacheOptions,
    ) -> Result<()> {
        self.put_bytes_unchecked(key, value, options, CacheEventOrigin::LocalApi)
            .await
    }

    async fn put_bytes_unchecked(
        &self,
        key: &str,
        value: Bytes,
        options: CacheOptions,
        origin: CacheEventOrigin,
    ) -> Result<()> {
        if self.exceeds_max_entry_bytes(value.len()) {
            return Err(self.oversize_rejection_error(value.len()));
        }

        let ttl = options.ttl_value().unwrap_or(self.inner.default_ttl);
        let tags = options.tags_value().to_vec();
        let entry = CacheEntry {
            value,
            tags: tags.clone(),
            expires_at: Instant::now().checked_add(ttl),
        };

        if let Some(old_entry) = self.inner.store.get(key).await {
            self.inner.tag_index.unregister(key, &old_entry.tags).await;
        }

        self.inner.store.insert(key.to_owned(), entry).await;
        self.inner.tag_index.register(key, &tags).await;
        self.publish_key_event(CacheEventKind::Stored, key, origin, tags);
        Ok(())
    }

    async fn put_bytes_if_fresh(
        &self,
        key: &str,
        value: Bytes,
        options: CacheOptions,
        generation: &LoadGenerationSnapshot,
    ) -> Result<bool> {
        if self.exceeds_max_entry_bytes(value.len()) {
            self.record_oversize_rejection();
            return Ok(false);
        }

        if !self.inner.tag_index.is_current(generation).await {
            self.inner
                .stats
                .stale_load_discards
                .fetch_add(1, Ordering::Relaxed);
            self.publish_key_event_with_tags(
                CacheEventKind::StaleLoadDiscarded,
                key,
                CacheEventOrigin::Loader,
                || options.tags_value().to_vec(),
            );
            return Ok(false);
        }

        self.put_bytes_unchecked(key, value, options, CacheEventOrigin::Loader)
            .await?;
        Ok(true)
    }

    async fn shared_load<F, Fut>(
        &self,
        key: &str,
        options: CacheOptions,
        loader: F,
    ) -> SharedLoadFuture
    where
        F: FnOnce(Self) -> Fut + Send + 'static,
        Fut: Future<Output = Result<Bytes>> + Send + 'static,
    {
        let generation = self.inner.tag_index.snapshot(options.tags_value()).await;

        if let Some(shared) = self.inner.in_flight.get_current(key, &generation).await {
            self.inner
                .stats
                .single_flight_joins
                .fetch_add(1, Ordering::Relaxed);
            self.publish_key_event_with_tags(
                CacheEventKind::SingleFlightJoined,
                key,
                CacheEventOrigin::SingleFlight,
                || options.tags_value().to_vec(),
            );
            return shared;
        }

        // Coverage builds get one cooperative scheduling point here so tests can
        // deterministically exercise the defensive "insert_or_get_current lost
        // the race" branch below. Normal builds do not yield on this path.
        #[cfg(coverage)]
        tokio::task::yield_now().await;

        let key_owned = key.to_owned();
        let cache = self.clone();
        let load_key = key_owned.clone();
        let load_generation = generation.clone();
        let late_join_event_tags = self
            .event_tags_if_observed(CacheEventKind::SingleFlightJoined, || {
                options.tags_value().to_vec()
            });
        let shared = async move {
            let load_event_tags = cache.event_tags_if_observed(CacheEventKind::LoadStarted, || {
                options.tags_value().to_vec()
            });
            let load_completed_event_tags = cache
                .event_tags_if_observed(CacheEventKind::LoadCompleted, || {
                    options.tags_value().to_vec()
                });
            let load_failed_event_tags = cache
                .event_tags_if_observed(CacheEventKind::LoadFailed, || {
                    options.tags_value().to_vec()
                });

            let result = async {
                cache.publish_key_event_with_prepared_tags(
                    CacheEventKind::LoadStarted,
                    &load_key,
                    CacheEventOrigin::Loader,
                    load_event_tags,
                );
                let bytes = loader(cache.clone()).await?;
                let accepted = cache
                    .put_bytes_if_fresh(&load_key, bytes.clone(), options, &load_generation)
                    .await?;
                if accepted {
                    cache.publish_key_event_with_prepared_tags(
                        CacheEventKind::LoadCompleted,
                        &load_key,
                        CacheEventOrigin::Loader,
                        load_completed_event_tags,
                    );
                }
                Ok(bytes)
            }
            .await;

            if result.is_err() {
                cache.publish_key_event_with_prepared_tags(
                    CacheEventKind::LoadFailed,
                    &load_key,
                    CacheEventOrigin::Loader,
                    load_failed_event_tags,
                );
            }

            let result = result.map_err(Arc::new);

            cache
                .inner
                .in_flight
                .remove_if_generation_matches(&load_key, &load_generation)
                .await;
            result
        }
        .boxed()
        .shared();

        let (shared, inserted) = self
            .inner
            .in_flight
            .insert_or_get_current(key_owned, shared, generation)
            .await;
        if !inserted {
            self.inner
                .stats
                .single_flight_joins
                .fetch_add(1, Ordering::Relaxed);
            self.publish_key_event_with_prepared_tags(
                CacheEventKind::SingleFlightJoined,
                key,
                CacheEventOrigin::SingleFlight,
                late_join_event_tags,
            );
        }

        shared
    }

    fn exceeds_max_entry_bytes(&self, encoded_len: usize) -> bool {
        encoded_len > self.inner.max_entry_bytes
    }

    fn record_oversize_rejection(&self) {
        self.inner
            .stats
            .oversize_rejections
            .fetch_add(1, Ordering::Relaxed);
    }

    fn oversize_rejection_error(&self, encoded_len: usize) -> CacheError {
        self.record_oversize_rejection();
        CacheError::Backend(format!(
            "encoded cache entry is {encoded_len} bytes, exceeding max_entry_bytes={}",
            self.inner.max_entry_bytes
        ))
    }

    async fn remove_expired(&self, key: &str, entry: &CacheEntry) {
        self.remove_entry(key, entry).await;
        self.publish_key_event_with_tags(
            CacheEventKind::Expired,
            key,
            CacheEventOrigin::Backend,
            || entry.tags.clone(),
        );
    }

    async fn remove_entry(&self, key: &str, entry: &CacheEntry) {
        self.inner.store.invalidate(key).await;
        self.inner.tag_index.unregister(key, &entry.tags).await;
    }

    async fn remove_with_event(
        &self,
        key: &str,
        kind: CacheEventKind,
        origin: CacheEventOrigin,
    ) -> Result<bool> {
        let Some(entry) = self.inner.store.get(key).await else {
            return Ok(false);
        };

        self.remove_entry(key, &entry).await;
        self.inner
            .stats
            .invalidations
            .fetch_add(1, Ordering::Relaxed);
        self.publish_key_event_with_tags(kind, key, origin, || entry.tags.clone());
        Ok(true)
    }

    fn may_publish_event(&self, kind: CacheEventKind) -> bool {
        self.inner.events.may_publish(kind)
    }

    fn event_tags_if_observed<F>(&self, kind: CacheEventKind, tags: F) -> Option<Vec<String>>
    where
        F: FnOnce() -> Vec<String>,
    {
        self.may_publish_event(kind).then(tags)
    }

    fn publish_key_event<I, S>(
        &self,
        kind: CacheEventKind,
        key: &str,
        origin: CacheEventOrigin,
        tags: I,
    ) where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.publish_key_event_with_tags(kind, key, origin, || tags);
    }

    fn publish_key_event_with_tags<F, I, S>(
        &self,
        kind: CacheEventKind,
        key: &str,
        origin: CacheEventOrigin,
        tags: F,
    ) where
        F: FnOnce() -> I,
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        if !self.may_publish_event(kind) {
            return;
        }

        self.publish_event(CacheEvent::for_key(kind, key, origin, tags()));
    }

    fn publish_key_event_with_prepared_tags(
        &self,
        kind: CacheEventKind,
        key: &str,
        origin: CacheEventOrigin,
        tags: Option<Vec<String>>,
    ) {
        if let Some(tags) = tags {
            self.publish_key_event(kind, key, origin, tags);
        }
    }

    fn publish_tag_event(
        &self,
        kind: CacheEventKind,
        tag: &str,
        affected_keys: u64,
        origin: CacheEventOrigin,
    ) {
        if !self.may_publish_event(kind) {
            return;
        }

        self.publish_event(CacheEvent::for_tag(kind, tag, affected_keys, origin));
    }

    fn publish_cache_event(
        &self,
        kind: CacheEventKind,
        affected_keys: Option<u64>,
        origin: CacheEventOrigin,
    ) {
        if !self.may_publish_event(kind) {
            return;
        }

        self.publish_event(CacheEvent::for_cache(kind, affected_keys, origin));
    }

    fn publish_event(&self, event: CacheEvent) {
        self.inner.events.publish(event, &self.inner.stats);
    }

    async fn publish_invalidation(&self, invalidation: CacheInvalidation) -> Result<()> {
        let Some(bus) = &self.inner.invalidation_bus else {
            return Ok(());
        };

        let mut message =
            CacheInvalidationMessage::new(self.inner.invalidation_node_id.clone(), invalidation);
        if let Some(runtime) = &self.inner.cluster_runtime {
            if let Err(error) = runtime.validate_generation().await {
                self.inner
                    .stats
                    .distributed_invalidation_publish_failures
                    .fetch_add(1, Ordering::Relaxed);
                return Err(error);
            }
            message = message.with_source_generation(runtime.generation());
        }

        if let Err(error) = bus.publish(message).await {
            self.inner
                .stats
                .distributed_invalidation_publish_failures
                .fetch_add(1, Ordering::Relaxed);
            return Err(error);
        }
        self.inner
            .stats
            .distributed_invalidations_published
            .fetch_add(1, Ordering::Relaxed);
        Ok(())
    }

    pub(crate) fn spawn_invalidation_listener(&self, mut shutdown: watch::Receiver<bool>) {
        let Some(bus) = self.inner.invalidation_bus.clone() else {
            return;
        };
        let mut receiver = bus.subscribe();
        let node_id = self.inner.invalidation_node_id.clone();
        let weak_inner = Arc::downgrade(&self.inner);

        tokio::spawn(async move {
            loop {
                tokio::select! {
                    _ = shutdown.changed() => break,
                    received = receiver.recv() => {
                        let Some(inner) = weak_inner.upgrade() else {
                            break;
                        };
                        match received {
                            CacheInvalidationReceive::Message(message) => {
                                if message.source_id() == node_id {
                                    continue;
                                }

                                let cache = HydraCache { inner };
                                let _ = cache.apply_remote_invalidation(message).await;
                            }
                            CacheInvalidationReceive::Lagged(count) => {
                                inner
                                    .stats
                                    .distributed_invalidation_lagged
                                    .fetch_add(count, Ordering::Relaxed);
                            }
                            CacheInvalidationReceive::DecodeError(_) => {
                                inner
                                    .stats
                                    .distributed_invalidation_decode_errors
                                    .fetch_add(1, Ordering::Relaxed);
                            }
                            CacheInvalidationReceive::Closed => {
                                inner
                                    .stats
                                    .distributed_invalidation_receiver_closed
                                    .fetch_add(1, Ordering::Relaxed);
                                break;
                            }
                        }
                    }
                }
            }
        });
    }

    async fn apply_remote_invalidation(&self, message: CacheInvalidationMessage) -> Result<()> {
        if let (Some(runtime), Some(generation)) =
            (&self.inner.cluster_runtime, message.source_generation())
        {
            let source_id = ClusterNodeId::from(message.source_id().to_owned());
            runtime
                .validate_remote_generation(&source_id, generation)
                .await?;
        }

        let (_, invalidation) = message.into_parts();
        self.inner
            .stats
            .distributed_invalidations_received
            .fetch_add(1, Ordering::Relaxed);

        match invalidation {
            CacheInvalidation::Key { key } => {
                self.remove_with_event(
                    &key,
                    CacheEventKind::KeyInvalidated,
                    CacheEventOrigin::DistributedBus,
                )
                .await?;
            }
            CacheInvalidation::Tag { tag } => {
                self.invalidate_tag_with_origin(&tag, CacheEventOrigin::DistributedBus)
                    .await?;
            }
            CacheInvalidation::Flush => {
                self.flush_with_origin(CacheEventOrigin::DistributedBus)
                    .await?;
            }
        }

        self.inner
            .stats
            .distributed_invalidations_applied
            .fetch_add(1, Ordering::Relaxed);
        Ok(())
    }
}

fn entry_in_stale_window(entry: &CacheEntry, window: Option<std::time::Duration>) -> bool {
    window
        .map(|window| entry.stale_window_contains_now(window))
        .unwrap_or(false)
}

fn take_loader<F>(loader: &mut Option<F>) -> F {
    loader
        .take()
        .expect("refresh loader is consumed exactly once per cache call")
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::{AtomicUsize, Ordering};

    use hydracache_core::{CacheEventKind, CacheEventOrigin};

    use super::HydraCache;

    #[test]
    fn lazy_key_event_tags_are_not_built_without_subscribers() {
        let cache = HydraCache::local().build();
        let tag_builds = AtomicUsize::new(0);

        cache.publish_key_event_with_tags(
            CacheEventKind::Stored,
            "user:42",
            CacheEventOrigin::LocalApi,
            || {
                tag_builds.fetch_add(1, Ordering::Relaxed);
                vec!["users".to_owned()]
            },
        );

        assert_eq!(tag_builds.load(Ordering::Relaxed), 0);
    }

    #[test]
    fn lazy_key_event_tags_are_built_for_observed_mutations() {
        let cache = HydraCache::local().build();
        let _events = cache.subscribe_mutations();
        let tag_builds = AtomicUsize::new(0);

        cache.publish_key_event_with_tags(
            CacheEventKind::Stored,
            "user:42",
            CacheEventOrigin::LocalApi,
            || {
                tag_builds.fetch_add(1, Ordering::Relaxed);
                vec!["users".to_owned()]
            },
        );

        assert_eq!(tag_builds.load(Ordering::Relaxed), 1);
    }

    #[test]
    fn lazy_key_event_tags_respect_disabled_access_events() {
        let cache = HydraCache::local().build();
        let _events = cache.subscribe_access();
        let tag_builds = AtomicUsize::new(0);

        cache.publish_key_event_with_tags(
            CacheEventKind::Hit,
            "user:42",
            CacheEventOrigin::LocalApi,
            || {
                tag_builds.fetch_add(1, Ordering::Relaxed);
                vec!["users".to_owned()]
            },
        );

        assert_eq!(tag_builds.load(Ordering::Relaxed), 0);
    }
}