pub mod embedding_cache;
pub mod memory_monitor;
pub mod metrics;
pub mod prefix_cache;
#[cfg(feature = "redis-cache")]
pub mod redis_backend;
pub mod token_cache;
use serde::{Deserialize, Serialize};
use std::sync::atomic::Ordering;
use tracing::trace;
pub use metrics::CacheMetrics;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheStats {
pub hits: u64,
pub misses: u64,
pub evictions: u64,
pub memory_bytes: u64,
pub entry_count: u64,
pub hit_rate: f64,
}
impl CacheStats {
pub fn from_metrics(metrics: &CacheMetrics, entry_count: u64) -> Self {
trace!("Creating stats from metrics: {metrics:?}, entry_count: {entry_count}");
let hits = metrics.hits.load(Ordering::Relaxed);
let misses = metrics.misses.load(Ordering::Relaxed);
let total = hits + misses;
#[allow(clippy::cast_precision_loss)]
let hit_rate = if total > 0 {
hits as f64 / total as f64
} else {
0.0
};
Self {
hits,
misses,
evictions: metrics.evictions.load(Ordering::Relaxed),
memory_bytes: metrics.memory_bytes.load(Ordering::Relaxed),
entry_count,
hit_rate,
}
}
}
pub trait CacheStore<K, V> {
fn get(&self, key: &K) -> Option<V>;
fn insert(&self, key: K, value: V);
fn clear(&self);
fn stats(&self) -> CacheStats;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
}