use crate::fetch::{FetchError, Response};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};
#[derive(Debug, Clone)]
pub struct CacheEntry {
pub response: Response,
pub stored_at: Instant,
pub max_age: Option<Duration>,
pub etag: Option<String>,
pub no_cache: bool,
}
impl CacheEntry {
pub fn is_fresh(&self) -> bool {
if self.no_cache {
return false;
}
match self.max_age {
Some(max_age) => self.stored_at.elapsed() < max_age,
None => true, }
}
}
#[derive(Debug, Clone, Default)]
pub struct Cache {
entries: Arc<RwLock<HashMap<String, CacheEntry>>>,
}
impl Cache {
pub fn new() -> Self {
Self {
entries: Arc::new(RwLock::new(HashMap::new())),
}
}
pub fn match_url(&self, url: &str) -> Option<Response> {
let entries = self.entries.read().ok()?;
let entry = entries.get(url)?;
if entry.is_fresh() {
Some(entry.response.clone())
} else {
None
}
}
pub fn put(&self, url: &str, response: Response) -> Result<bool, FetchError> {
let cache_control = response.headers.get("cache-control").unwrap_or("");
let lower_cc = cache_control.to_ascii_lowercase();
if lower_cc.contains("no-store") {
return Ok(false);
}
let no_cache = lower_cc.contains("no-cache");
let mut max_age = None;
for part in lower_cc.split(',') {
let trimmed = part.trim();
if let Some(sec_str) = trimmed.strip_prefix("max-age=") {
if let Ok(secs) = sec_str.trim().parse::<u64>() {
max_age = Some(Duration::from_secs(secs));
}
}
}
let etag = response.headers.get("etag").map(|s| s.to_string());
let entry = CacheEntry {
response,
stored_at: Instant::now(),
max_age,
etag,
no_cache,
};
if let Ok(mut lock) = self.entries.write() {
lock.insert(url.to_string(), entry);
Ok(true)
} else {
Err(FetchError::IoError("Cache lock poisoned".into()))
}
}
pub fn delete(&self, url: &str) -> bool {
if let Ok(mut lock) = self.entries.write() {
lock.remove(url).is_some()
} else {
false
}
}
pub fn keys(&self) -> Vec<String> {
self.entries
.read()
.map(|l| l.keys().cloned().collect())
.unwrap_or_default()
}
pub fn clear(&self) {
if let Ok(mut lock) = self.entries.write() {
lock.clear();
}
}
}
#[derive(Debug, Clone, Default)]
pub struct CacheStorage {
caches: Arc<RwLock<HashMap<String, Cache>>>,
}
impl CacheStorage {
pub fn new() -> Self {
Self {
caches: Arc::new(RwLock::new(HashMap::new())),
}
}
pub fn open(&self, name: &str) -> Cache {
let mut lock = self.caches.write().expect("CacheStorage write lock");
lock.entry(name.to_string()).or_default().clone()
}
pub fn has(&self, name: &str) -> bool {
self.caches
.read()
.map(|l| l.contains_key(name))
.unwrap_or(false)
}
pub fn delete(&self, name: &str) -> bool {
self.caches
.write()
.map(|mut l| l.remove(name).is_some())
.unwrap_or(false)
}
pub fn keys(&self) -> Vec<String> {
self.caches
.read()
.map(|l| l.keys().cloned().collect())
.unwrap_or_default()
}
}