use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use tokio::sync::RwLock;
pub use ck_index::IndexStats;
#[derive(Clone)]
pub struct StatsCache {
stats: Arc<RwLock<HashMap<PathBuf, (IndexStats, SystemTime)>>>,
ttl: Duration,
}
impl StatsCache {
pub fn new(ttl: Duration) -> Self {
Self {
stats: Arc::new(RwLock::new(HashMap::new())),
ttl,
}
}
pub async fn get(&self, path: &PathBuf) -> Option<IndexStats> {
let stats = self.stats.read().await;
if let Some((stats, cached_at)) = stats.get(path)
&& cached_at.elapsed().unwrap_or_default() < self.ttl
{
return Some(stats.clone());
}
None
}
pub async fn update(&self, path: PathBuf, stats: IndexStats) {
let mut cache = self.stats.write().await;
cache.insert(path, (stats, SystemTime::now()));
}
pub async fn invalidate(&self, path: &PathBuf) {
let mut cache = self.stats.write().await;
cache.remove(path);
}
}
impl Default for StatsCache {
fn default() -> Self {
Self::new(Duration::from_secs(30))
}
}