use std::time::Duration;
use crate::cache::{CacheKey, EvictionPolicy, UnifiedCache};
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
struct ValidationKey {
tool: String,
args_hash: u64,
}
impl CacheKey for ValidationKey {
fn to_cache_key(&self) -> String {
format!("{}:{}", self.tool, self.args_hash)
}
}
pub struct ValidationCache {
cache: UnifiedCache<ValidationKey, bool>,
}
impl ValidationCache {
pub fn new(ttl: Duration) -> Self {
Self {
cache: UnifiedCache::new(1000, ttl, EvictionPolicy::TtlOnly),
}
}
pub fn check(&self, tool: &str, args_hash: u64) -> Option<bool> {
let key = ValidationKey {
tool: tool.to_string(),
args_hash,
};
self.cache.get_owned(&key)
}
pub fn insert(&self, tool: &str, args_hash: u64, result: bool) {
let key = ValidationKey {
tool: tool.to_string(),
args_hash,
};
self.cache.insert(key, result, 1); }
}
impl Default for ValidationCache {
fn default() -> Self {
Self::new(Duration::from_secs(300)) }
}