cache_rs/metrics/lru.rs
1//! LRU Cache Metrics
2//!
3//! Metrics specific to the LRU (Least Recently Used) cache algorithm.
4
5extern crate alloc;
6
7use super::{CacheMetrics, CoreCacheMetrics};
8use alloc::collections::BTreeMap;
9use alloc::string::String;
10
11/// LRU-specific metrics (extends CoreCacheMetrics)
12///
13/// This struct contains metrics specific to the LRU (Least Recently Used) cache algorithm.
14/// Currently, LRU uses only the core metrics, but this structure allows for future
15/// LRU-specific metrics to be added.
16#[derive(Debug, Clone)]
17pub struct LruCacheMetrics {
18 /// Core metrics common to all cache algorithms
19 pub core: CoreCacheMetrics,
20 // LRU doesn't have algorithm-specific metrics beyond core metrics
21 // But we keep this structure for consistency with other cache algorithms
22 //
23 // Future LRU-specific metrics could include:
24 // - pub recency_distribution: BTreeMap<String, u64>, // Age distribution of cached items
25 // - pub access_pattern_stats: AccessPatternStats, // Sequential vs random access patterns
26}
27
28impl LruCacheMetrics {
29 /// Creates a new LruCacheMetrics instance with the specified maximum cache size
30 ///
31 /// # Arguments
32 /// * `max_cache_size_bytes` - The maximum allowed cache size in bytes
33 pub fn new(max_cache_size_bytes: u64) -> Self {
34 Self {
35 core: CoreCacheMetrics::new(max_cache_size_bytes),
36 }
37 }
38
39 /// Converts LRU metrics to a BTreeMap for reporting
40 ///
41 /// This method returns all metrics relevant to the LRU cache algorithm.
42 /// Currently, this includes only core metrics, but LRU-specific metrics
43 /// could be added here in the future.
44 ///
45 /// Uses BTreeMap to ensure consistent, deterministic ordering of metrics.
46 ///
47 /// # Returns
48 /// A BTreeMap containing all LRU cache metrics as key-value pairs
49 pub fn to_btreemap(&self) -> BTreeMap<String, f64> {
50 // LRU-specific metrics would go here
51 // For now, LRU only uses core metrics
52 //
53 // Example future additions:
54 // metrics.insert("avg_item_age".to_string(), self.calculate_avg_age());
55 // metrics.insert("recency_variance".to_string(), self.calculate_recency_variance());
56
57 self.core.to_btreemap()
58 }
59}
60
61impl CacheMetrics for LruCacheMetrics {
62 /// Returns all LRU cache metrics as key-value pairs in deterministic order
63 ///
64 /// # Returns
65 /// A BTreeMap containing all metrics tracked by this LRU cache instance
66 fn metrics(&self) -> BTreeMap<String, f64> {
67 self.to_btreemap()
68 }
69
70 /// Returns the algorithm name for this cache implementation
71 ///
72 /// # Returns
73 /// "LRU" - identifying this as a Least Recently Used cache
74 fn algorithm_name(&self) -> &'static str {
75 "LRU"
76 }
77}