use crate::cache::RedisCache;
pub struct CacheInvalidator {
cache: RedisCache,
patterns: Vec<String>,
}
impl CacheInvalidator {
pub fn new(cache: RedisCache) -> Self {
Self {
cache,
patterns: Vec::new(),
}
}
pub fn add_pattern(mut self, pattern: &str) -> Self {
self.patterns.push(pattern.to_string());
self
}
pub async fn invalidate(&self) -> Result<u64, crate::cache::client::CacheError> {
let mut total = 0u64;
for pattern in &self.patterns {
let deleted = self.cache.delete_pattern(pattern).await?;
total += deleted;
}
Ok(total)
}
}