tower_resilience_cache/
events.rs1use std::time::Instant;
4use tower_resilience_core::ResilienceEvent;
5
6#[derive(Debug, Clone)]
8pub enum CacheEvent {
9 Hit {
11 pattern_name: String,
13 timestamp: Instant,
15 },
16 Miss {
18 pattern_name: String,
20 timestamp: Instant,
22 },
23 Eviction {
25 pattern_name: String,
27 timestamp: Instant,
29 },
30}
31
32impl ResilienceEvent for CacheEvent {
33 fn event_type(&self) -> &'static str {
34 match self {
35 CacheEvent::Hit { .. } => "cache_hit",
36 CacheEvent::Miss { .. } => "cache_miss",
37 CacheEvent::Eviction { .. } => "cache_eviction",
38 }
39 }
40
41 fn timestamp(&self) -> Instant {
42 match self {
43 CacheEvent::Hit { timestamp, .. }
44 | CacheEvent::Miss { timestamp, .. }
45 | CacheEvent::Eviction { timestamp, .. } => *timestamp,
46 }
47 }
48
49 fn pattern_name(&self) -> &str {
50 match self {
51 CacheEvent::Hit { pattern_name, .. }
52 | CacheEvent::Miss { pattern_name, .. }
53 | CacheEvent::Eviction { pattern_name, .. } => pattern_name,
54 }
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
63 fn test_event_types() {
64 let now = Instant::now();
65
66 let hit = CacheEvent::Hit {
67 pattern_name: "test".to_string(),
68 timestamp: now,
69 };
70 assert_eq!(hit.event_type(), "cache_hit");
71 assert_eq!(hit.pattern_name(), "test");
72
73 let miss = CacheEvent::Miss {
74 pattern_name: "test".to_string(),
75 timestamp: now,
76 };
77 assert_eq!(miss.event_type(), "cache_miss");
78
79 let eviction = CacheEvent::Eviction {
80 pattern_name: "test".to_string(),
81 timestamp: now,
82 };
83 assert_eq!(eviction.event_type(), "cache_eviction");
84 }
85}