1use crate::deps::Duration;
8
9#[derive(Debug, Clone)]
11pub struct CacheConfig {
12 pub max_capacity: usize,
14
15 pub default_ttl: Option<Duration>,
17
18 pub cleanup_interval: Duration,
20
21 pub eviction_batch_size: usize,
23
24 pub enable_stats: bool,
26}
27
28impl Default for CacheConfig {
29 fn default() -> Self {
30 Self {
31 max_capacity: 1000,
32 default_ttl: Some(Duration::from_secs(300)),
33 cleanup_interval: Duration::from_secs(60),
34 eviction_batch_size: 100,
35 enable_stats: true,
36 }
37 }
38}
39
40impl CacheConfig {
41 pub fn new() -> Self {
43 Self::default()
44 }
45
46 pub fn max_capacity(mut self, capacity: usize) -> Self {
48 self.max_capacity = capacity;
49 self.eviction_batch_size = (capacity / 10).max(1);
50 self
51 }
52
53 pub fn default_ttl(mut self, ttl: Duration) -> Self {
55 self.default_ttl = Some(ttl);
56 self
57 }
58
59 pub fn no_ttl(mut self) -> Self {
61 self.default_ttl = None;
62 self
63 }
64
65 pub fn cleanup_interval(mut self, interval: Duration) -> Self {
67 self.cleanup_interval = interval;
68 self
69 }
70
71 pub fn eviction_batch_size(mut self, size: usize) -> Self {
73 self.eviction_batch_size = size;
74 self
75 }
76
77 pub fn no_stats(mut self) -> Self {
79 self.enable_stats = false;
80 self
81 }
82
83 pub fn high_performance() -> Self {
85 Self {
86 max_capacity: 100_000,
87 default_ttl: Some(Duration::from_secs(3600)),
88 cleanup_interval: Duration::from_secs(300),
89 eviction_batch_size: 10_000,
90 enable_stats: false,
91 }
92 }
93
94 pub fn session() -> Self {
96 Self {
97 max_capacity: 10_000,
98 default_ttl: Some(Duration::from_secs(1800)), cleanup_interval: Duration::from_secs(60),
100 eviction_batch_size: 1_000,
101 enable_stats: true,
102 }
103 }
104
105 pub fn short_lived() -> Self {
107 Self {
108 max_capacity: 100,
109 default_ttl: Some(Duration::from_secs(60)),
110 cleanup_interval: Duration::from_secs(10),
111 eviction_batch_size: 10,
112 enable_stats: true,
113 }
114 }
115}