pub mod cleaner;
pub mod metadata;
use crate::config::JxlifyConfig;
use dashmap::DashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::sync::Mutex;
#[derive(Clone, Default)]
pub struct CacheManager {
in_flight_locks: Arc<DashMap<String, Arc<Mutex<()>>>>,
}
impl CacheManager {
pub fn new() -> Self {
Self {
in_flight_locks: Arc::new(DashMap::new()),
}
}
pub fn get_lock(&self, key: &str) -> Arc<Mutex<()>> {
self.in_flight_locks
.entry(key.to_string())
.or_insert_with(|| Arc::new(Mutex::new(())))
.clone()
}
pub fn remove_lock(&self, key: &str) {
self.in_flight_locks.remove(key);
}
pub fn get_cache_path(&self, id: &str, ext: &str, subdir: &str, config: &JxlifyConfig) -> PathBuf {
crate::helper::get_sharded_path(&config.images_cache_path(), subdir, id, ext)
}
pub fn is_cached(&self, cached_path: &Path) -> bool {
crate::helper::image_exists(cached_path)
}
}