use std::collections::HashMap;
use std::sync::Arc;
#[allow(clippy::type_complexity)]
pub struct CacheMetrics {
counters: Arc<std::sync::RwLock<HashMap<String, u64>>>,
}
impl CacheMetrics {
pub fn new() -> Self {
Self {
counters: Arc::new(std::sync::RwLock::new(HashMap::new())),
}
}
pub fn increment_counter(&self, name: &str) {
if let Ok(mut counters) = self.counters.write() {
*counters.entry(name.to_string()).or_insert(0) += 1;
}
}
pub fn record_hit(&self) {
self.increment_counter("cache_hits_total");
}
pub fn record_miss(&self) {
self.increment_counter("cache_misses_total");
}
pub fn record_eviction(&self) {
self.increment_counter("cache_evictions_total");
}
pub fn get_metrics(&self) -> HashMap<String, u64> {
self.counters.read().unwrap().clone()
}
}
impl Default for CacheMetrics {
fn default() -> Self {
Self::new()
}
}