hydracache-core 0.19.0

Core runtime types and traits for HydraCache.
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
use std::time::SystemTime;

use crate::CacheKey;

/// Kind of cache event emitted by a HydraCache runtime.
///
/// Access and loader events are intentionally separate from mutation events so
/// applications can keep high-volume hit/miss reporting disabled until needed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CacheEventKind {
    /// A lookup returned a cached value.
    Hit,
    /// A lookup did not find a usable cached value.
    Miss,
    /// A caller joined an already running single-flight load.
    SingleFlightJoined,
    /// The cache owner started a loader for a missing key.
    LoadStarted,
    /// A loader completed and its result was accepted into the cache.
    LoadCompleted,
    /// A loader returned an error or failed to encode the loaded value.
    LoadFailed,
    /// A value was stored.
    Stored,
    /// A key was explicitly removed.
    Removed,
    /// A key was explicitly invalidated.
    KeyInvalidated,
    /// A tag was invalidated.
    TagInvalidated,
    /// The cache was flushed.
    Flushed,
    /// A loader result was discarded because an invalidation made it stale.
    StaleLoadDiscarded,
    /// An entry expired and was removed during cache access.
    Expired,
    /// An entry was evicted by the backend.
    Evicted,
}

impl CacheEventKind {
    /// Return whether this event belongs to the high-volume access/load group.
    pub fn is_access(self) -> bool {
        matches!(
            self,
            Self::Hit
                | Self::Miss
                | Self::SingleFlightJoined
                | Self::LoadStarted
                | Self::LoadCompleted
                | Self::LoadFailed
        )
    }

    /// Return whether this event describes a cache mutation or invalidation.
    pub fn is_mutation(self) -> bool {
        !self.is_access()
    }
}

/// Logical scope affected by a cache event.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CacheEventScope {
    /// Event for one cache key.
    Key {
        /// Physical cache key.
        key: CacheKey<'static>,
    },
    /// Event for one invalidation tag.
    Tag {
        /// Invalidated tag.
        tag: String,
        /// Number of keys affected by the operation.
        affected_keys: u64,
    },
    /// Event for the whole cache.
    Cache {
        /// Approximate number of keys affected, when known.
        affected_keys: Option<u64>,
    },
}

impl CacheEventScope {
    /// Return the event key when this is a key-scoped event.
    pub fn key(&self) -> Option<&str> {
        match self {
            Self::Key { key } => Some(key.as_str()),
            Self::Tag { .. } | Self::Cache { .. } => None,
        }
    }

    /// Return the event tag when this is a tag-scoped event.
    pub fn tag(&self) -> Option<&str> {
        match self {
            Self::Tag { tag, .. } => Some(tag),
            Self::Key { .. } | Self::Cache { .. } => None,
        }
    }

    /// Return the affected-key count when the scope carries one.
    pub fn affected_keys(&self) -> Option<u64> {
        match self {
            Self::Key { .. } => Some(1),
            Self::Tag { affected_keys, .. } => Some(*affected_keys),
            Self::Cache { affected_keys } => *affected_keys,
        }
    }
}

/// Origin of a cache event.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CacheEventOrigin {
    /// Event was caused by a direct local API call.
    LocalApi,
    /// Event was caused by a loader owner.
    Loader,
    /// Event was caused by single-flight coordination.
    SingleFlight,
    /// Event was caused by backend expiration or eviction.
    Backend,
    /// Event was received from a future distributed bus.
    DistributedBus,
}

/// Value payload mode requested by event subscribers.
///
/// The first implementation emits metadata-only events. The enum exists so the
/// public filter shape can grow toward encoded-value delivery without changing
/// subscription options later.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum CacheEventValueMode {
    /// Do not include cached values in events.
    #[default]
    MetadataOnly,
    /// Reserve space for a future encoded-value event mode.
    EncodedBytes,
}

/// Metadata-only cache event.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CacheEvent {
    kind: CacheEventKind,
    scope: CacheEventScope,
    origin: CacheEventOrigin,
    tags: Vec<String>,
    timestamp: SystemTime,
}

impl CacheEvent {
    /// Create a key-scoped event.
    pub fn for_key<I, S>(
        kind: CacheEventKind,
        key: impl Into<String>,
        origin: CacheEventOrigin,
        tags: I,
    ) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            kind,
            scope: CacheEventScope::Key {
                key: CacheKey::from(key.into()),
            },
            origin,
            tags: tags.into_iter().map(Into::into).collect(),
            timestamp: SystemTime::now(),
        }
    }

    /// Create a tag-scoped event.
    pub fn for_tag(
        kind: CacheEventKind,
        tag: impl Into<String>,
        affected_keys: u64,
        origin: CacheEventOrigin,
    ) -> Self {
        let tag = tag.into();
        Self {
            kind,
            scope: CacheEventScope::Tag {
                tag: tag.clone(),
                affected_keys,
            },
            origin,
            tags: vec![tag],
            timestamp: SystemTime::now(),
        }
    }

    /// Create a cache-wide event.
    pub fn for_cache(
        kind: CacheEventKind,
        affected_keys: Option<u64>,
        origin: CacheEventOrigin,
    ) -> Self {
        Self {
            kind,
            scope: CacheEventScope::Cache { affected_keys },
            origin,
            tags: Vec::new(),
            timestamp: SystemTime::now(),
        }
    }

    /// Return the event kind.
    pub fn kind(&self) -> CacheEventKind {
        self.kind
    }

    /// Return the event scope.
    pub fn scope(&self) -> &CacheEventScope {
        &self.scope
    }

    /// Return the event origin.
    pub fn origin(&self) -> CacheEventOrigin {
        self.origin
    }

    /// Return the event key when this is a key-scoped event.
    pub fn key(&self) -> Option<&str> {
        self.scope.key()
    }

    /// Return the event tag when this is a tag-scoped event.
    pub fn tag(&self) -> Option<&str> {
        self.scope.tag()
    }

    /// Return event tags associated with the key or invalidation.
    pub fn tags(&self) -> &[String] {
        &self.tags
    }

    /// Return the affected-key count when known.
    pub fn affected_keys(&self) -> Option<u64> {
        self.scope.affected_keys()
    }

    /// Return the event timestamp.
    pub fn timestamp(&self) -> SystemTime {
        self.timestamp
    }
}

/// Subscription filters for cache events.
///
/// Filters are intentionally metadata-only. They are cheap enough to apply in
/// the subscriber wrapper and avoid coupling event publication to decoded
/// values.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CacheEventOptions {
    include_kinds: Option<Vec<CacheEventKind>>,
    exclude_kinds: Vec<CacheEventKind>,
    key: Option<String>,
    key_prefix: Option<String>,
    tag: Option<String>,
    origin: Option<CacheEventOrigin>,
    value_mode: CacheEventValueMode,
}

impl CacheEventOptions {
    /// Create event options that accept all published events.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create event options for mutation/invalidation events.
    pub fn mutations() -> Self {
        Self::new().include_kinds([
            CacheEventKind::Stored,
            CacheEventKind::Removed,
            CacheEventKind::KeyInvalidated,
            CacheEventKind::TagInvalidated,
            CacheEventKind::Flushed,
            CacheEventKind::StaleLoadDiscarded,
            CacheEventKind::Expired,
            CacheEventKind::Evicted,
        ])
    }

    /// Create event options for high-volume access/load events.
    pub fn access() -> Self {
        Self::new().include_kinds([
            CacheEventKind::Hit,
            CacheEventKind::Miss,
            CacheEventKind::SingleFlightJoined,
            CacheEventKind::LoadStarted,
            CacheEventKind::LoadCompleted,
            CacheEventKind::LoadFailed,
        ])
    }

    /// Include one event kind.
    pub fn include_kind(self, kind: CacheEventKind) -> Self {
        self.include_kinds([kind])
    }

    /// Include several event kinds.
    pub fn include_kinds<I>(mut self, kinds: I) -> Self
    where
        I: IntoIterator<Item = CacheEventKind>,
    {
        self.include_kinds
            .get_or_insert_with(Vec::new)
            .extend(kinds);
        self
    }

    /// Exclude one event kind.
    pub fn exclude_kind(self, kind: CacheEventKind) -> Self {
        self.exclude_kinds([kind])
    }

    /// Exclude several event kinds.
    pub fn exclude_kinds<I>(mut self, kinds: I) -> Self
    where
        I: IntoIterator<Item = CacheEventKind>,
    {
        self.exclude_kinds.extend(kinds);
        self
    }

    /// Restrict events to one exact key.
    pub fn key(mut self, key: impl Into<String>) -> Self {
        self.key = Some(key.into());
        self
    }

    /// Restrict events to key-scoped events whose key starts with the prefix.
    pub fn key_prefix(mut self, key_prefix: impl Into<String>) -> Self {
        self.key_prefix = Some(key_prefix.into());
        self
    }

    /// Restrict events to a tag.
    pub fn tag(mut self, tag: impl Into<String>) -> Self {
        self.tag = Some(tag.into());
        self
    }

    /// Restrict events to one origin.
    pub fn origin(mut self, origin: CacheEventOrigin) -> Self {
        self.origin = Some(origin);
        self
    }

    /// Set the value mode requested by this subscription.
    pub fn value_mode(mut self, value_mode: CacheEventValueMode) -> Self {
        self.value_mode = value_mode;
        self
    }

    /// Return the requested value mode.
    pub fn value_mode_value(&self) -> CacheEventValueMode {
        self.value_mode
    }

    /// Return whether this event passes all filters.
    pub fn matches(&self, event: &CacheEvent) -> bool {
        if let Some(include_kinds) = &self.include_kinds {
            if !include_kinds.contains(&event.kind()) {
                return false;
            }
        }

        if self.exclude_kinds.contains(&event.kind()) {
            return false;
        }

        if let Some(key) = &self.key {
            if event.key() != Some(key.as_str()) {
                return false;
            }
        }

        if let Some(key_prefix) = &self.key_prefix {
            let Some(key) = event.key() else {
                return false;
            };
            if !key.starts_with(key_prefix) {
                return false;
            }
        }

        if let Some(tag) = &self.tag {
            let scope_matches = event.tag() == Some(tag.as_str());
            let tags_match = event.tags().iter().any(|event_tag| event_tag == tag);
            if !scope_matches && !tags_match {
                return false;
            }
        }

        if let Some(origin) = self.origin {
            if event.origin() != origin {
                return false;
            }
        }

        true
    }
}

#[cfg(test)]
mod tests {
    use super::{
        CacheEvent, CacheEventKind, CacheEventOptions, CacheEventOrigin, CacheEventScope,
        CacheEventValueMode,
    };

    #[test]
    fn event_kind_groups_access_and_mutation_events() {
        assert!(CacheEventKind::Hit.is_access());
        assert!(CacheEventKind::LoadFailed.is_access());
        assert!(!CacheEventKind::Stored.is_access());
        assert!(CacheEventKind::Stored.is_mutation());
    }

    #[test]
    fn event_constructors_capture_scope_tags_and_origin() {
        let key_event = CacheEvent::for_key(
            CacheEventKind::Stored,
            "user:42",
            CacheEventOrigin::LocalApi,
            ["users", "user:42"],
        );
        let tag_event = CacheEvent::for_tag(
            CacheEventKind::TagInvalidated,
            "users",
            3,
            CacheEventOrigin::LocalApi,
        );
        let cache_event = CacheEvent::for_cache(
            CacheEventKind::Flushed,
            Some(10),
            CacheEventOrigin::LocalApi,
        );

        assert_eq!(key_event.key(), Some("user:42"));
        assert_eq!(
            key_event.tags(),
            &["users".to_owned(), "user:42".to_owned()]
        );
        assert_eq!(key_event.origin(), CacheEventOrigin::LocalApi);
        assert_eq!(tag_event.tag(), Some("users"));
        assert_eq!(tag_event.affected_keys(), Some(3));
        assert_eq!(
            cache_event.scope(),
            &CacheEventScope::Cache {
                affected_keys: Some(10)
            }
        );
    }

    #[test]
    fn event_options_filter_by_kind_key_prefix_tag_and_origin() {
        let stored = CacheEvent::for_key(
            CacheEventKind::Stored,
            "users:42",
            CacheEventOrigin::LocalApi,
            ["users"],
        );
        let removed = CacheEvent::for_key(
            CacheEventKind::Removed,
            "orders:7",
            CacheEventOrigin::LocalApi,
            ["orders"],
        );

        let options = CacheEventOptions::new()
            .include_kind(CacheEventKind::Stored)
            .key_prefix("users:")
            .tag("users")
            .origin(CacheEventOrigin::LocalApi);

        assert!(options.matches(&stored));
        assert!(!options.matches(&removed));
        assert!(!options
            .clone()
            .exclude_kind(CacheEventKind::Stored)
            .matches(&stored));
    }

    #[test]
    fn event_options_mutations_and_access_presets_are_distinct() {
        let stored = CacheEvent::for_key(
            CacheEventKind::Stored,
            "k",
            CacheEventOrigin::LocalApi,
            ["t"],
        );
        let hit = CacheEvent::for_key(CacheEventKind::Hit, "k", CacheEventOrigin::LocalApi, ["t"]);

        assert!(CacheEventOptions::mutations().matches(&stored));
        assert!(!CacheEventOptions::mutations().matches(&hit));
        assert!(CacheEventOptions::access().matches(&hit));
        assert!(!CacheEventOptions::access().matches(&stored));
    }

    #[test]
    fn event_options_keep_requested_value_mode() {
        let options = CacheEventOptions::new().value_mode(CacheEventValueMode::EncodedBytes);

        assert_eq!(
            options.value_mode_value(),
            CacheEventValueMode::EncodedBytes
        );
    }
}