use crate::models::ThingsId;
use serde::{Deserialize, Serialize};
use std::time::Duration;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InvalidationStrategy {
TimeBased,
EventBased,
DependencyBased,
Hybrid,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheDependency {
pub entity_type: String,
pub entity_id: Option<ThingsId>,
pub invalidating_operations: Vec<String>,
}
impl CacheDependency {
#[must_use]
pub fn matches(&self, entity_type: &str, entity_id: Option<&ThingsId>) -> bool {
if self.entity_type != entity_type {
return false;
}
match (&self.entity_id, entity_id) {
(Some(dep_id), Some(req_id)) => dep_id == req_id,
_ => true,
}
}
#[must_use]
pub fn matches_operation(&self, operation: &str) -> bool {
self.invalidating_operations
.iter()
.any(|op| op == operation)
}
}
#[derive(Debug, Clone)]
pub struct CacheConfig {
pub max_capacity: u64,
pub ttl: Duration,
pub tti: Duration,
pub invalidation_strategy: InvalidationStrategy,
pub enable_cache_warming: bool,
pub warming_interval: Duration,
pub max_warming_entries: usize,
}
impl Default for CacheConfig {
fn default() -> Self {
Self {
max_capacity: 1000,
ttl: Duration::from_secs(300), tti: Duration::from_secs(60), invalidation_strategy: InvalidationStrategy::Hybrid,
enable_cache_warming: true,
warming_interval: Duration::from_secs(60), max_warming_entries: 50,
}
}
}