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
//! Cache statistics and entry information
/// Cache entry information for inspection
#[derive(Debug, Clone)]
pub struct CacheEntryInfo {
/// The key of the entry
pub key: String,
/// Size of the value in bytes
pub size: usize,
/// Whether the entry has an expiration time
pub has_expiry: bool,
/// Seconds until expiration (if applicable)
pub ttl_seconds: Option<u64>,
}
/// Cache statistics
#[derive(Debug, Clone, Default)]
pub struct CacheStatistics {
/// Number of cache hits
pub hits: u64,
/// Number of cache misses
pub misses: u64,
/// Total number of requests
pub total_requests: u64,
/// Current number of entries in cache
pub entry_count: u64,
/// Approximate memory usage in bytes
pub memory_usage: u64,
}
impl CacheStatistics {
/// Calculate hit rate (0.0 to 1.0)
///
/// # Examples
///
/// ```
/// use reinhardt_utils::cache::CacheStatistics;
///
/// let mut stats = CacheStatistics::default();
/// stats.hits = 75;
/// stats.misses = 25;
/// stats.total_requests = 100;
///
/// assert_eq!(stats.hit_rate(), 0.75);
/// ```
pub fn hit_rate(&self) -> f64 {
if self.total_requests == 0 {
0.0
} else {
self.hits as f64 / self.total_requests as f64
}
}
/// Calculate miss rate (0.0 to 1.0)
///
/// # Examples
///
/// ```
/// use reinhardt_utils::cache::CacheStatistics;
///
/// let mut stats = CacheStatistics::default();
/// stats.hits = 75;
/// stats.misses = 25;
/// stats.total_requests = 100;
///
/// assert_eq!(stats.miss_rate(), 0.25);
/// ```
pub fn miss_rate(&self) -> f64 {
if self.total_requests == 0 {
0.0
} else {
self.misses as f64 / self.total_requests as f64
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_statistics_hit_miss_rate_zero_requests() {
let stats = CacheStatistics::default();
assert_eq!(stats.hit_rate(), 0.0);
assert_eq!(stats.miss_rate(), 0.0);
}
}