use crate::deps::Duration;
#[derive(Debug, Clone)]
pub struct CacheConfig {
pub max_capacity: usize,
pub default_ttl: Option<Duration>,
pub cleanup_interval: Duration,
pub eviction_batch_size: usize,
pub enable_stats: bool,
}
impl Default for CacheConfig {
fn default() -> Self {
Self {
max_capacity: 1000,
default_ttl: Some(Duration::from_secs(300)),
cleanup_interval: Duration::from_secs(60),
eviction_batch_size: 100,
enable_stats: true,
}
}
}
impl CacheConfig {
pub fn new() -> Self {
Self::default()
}
pub fn max_capacity(mut self, capacity: usize) -> Self {
self.max_capacity = capacity;
self.eviction_batch_size = (capacity / 10).max(1);
self
}
pub fn default_ttl(mut self, ttl: Duration) -> Self {
self.default_ttl = Some(ttl);
self
}
pub fn no_ttl(mut self) -> Self {
self.default_ttl = None;
self
}
pub fn cleanup_interval(mut self, interval: Duration) -> Self {
self.cleanup_interval = interval;
self
}
pub fn eviction_batch_size(mut self, size: usize) -> Self {
self.eviction_batch_size = size;
self
}
pub fn no_stats(mut self) -> Self {
self.enable_stats = false;
self
}
pub fn high_performance() -> Self {
Self {
max_capacity: 100_000,
default_ttl: Some(Duration::from_secs(3600)),
cleanup_interval: Duration::from_secs(300),
eviction_batch_size: 10_000,
enable_stats: false,
}
}
pub fn session() -> Self {
Self {
max_capacity: 10_000,
default_ttl: Some(Duration::from_secs(1800)), cleanup_interval: Duration::from_secs(60),
eviction_batch_size: 1_000,
enable_stats: true,
}
}
pub fn short_lived() -> Self {
Self {
max_capacity: 100,
default_ttl: Some(Duration::from_secs(60)),
cleanup_interval: Duration::from_secs(10),
eviction_batch_size: 10,
enable_stats: true,
}
}
}