threatflux_cache/
config.rs1use serde::{Deserialize, Serialize};
4use std::time::Duration;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct CacheConfig {
9 pub max_entries_per_key: usize,
11 pub max_total_entries: usize,
13 pub eviction_policy: EvictionPolicy,
15 pub persistence: PersistenceConfig,
17 pub default_ttl: Option<Duration>,
19}
20
21impl Default for CacheConfig {
22 fn default() -> Self {
23 Self {
24 max_entries_per_key: 100,
25 max_total_entries: 10_000,
26 eviction_policy: EvictionPolicy::Lru,
27 persistence: PersistenceConfig::default(),
28 default_ttl: None,
29 }
30 }
31}
32
33impl CacheConfig {
34 pub fn new() -> Self {
36 Self::default()
37 }
38
39 pub fn with_max_entries_per_key(mut self, max: usize) -> Self {
41 self.max_entries_per_key = max;
42 self
43 }
44
45 pub fn with_max_total_entries(mut self, max: usize) -> Self {
47 self.max_total_entries = max;
48 self
49 }
50
51 pub fn with_eviction_policy(mut self, policy: EvictionPolicy) -> Self {
53 self.eviction_policy = policy;
54 self
55 }
56
57 pub fn with_persistence(mut self, persistence: PersistenceConfig) -> Self {
59 self.persistence = persistence;
60 self
61 }
62
63 pub fn with_default_ttl(mut self, ttl: Duration) -> Self {
65 self.default_ttl = Some(ttl);
66 self
67 }
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
72pub enum EvictionPolicy {
73 Lru,
75 Lfu,
77 Fifo,
79 Ttl,
81 None,
83}
84
85#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct PersistenceConfig {
88 pub enabled: bool,
90 pub sync_interval: usize,
92 pub load_on_startup: bool,
94}
95
96impl Default for PersistenceConfig {
97 fn default() -> Self {
98 Self {
99 enabled: false,
100 sync_interval: 100,
101 load_on_startup: true,
102 }
103 }
104}
105
106impl PersistenceConfig {
107 pub fn enabled() -> Self {
109 Self {
110 enabled: true,
111 ..Default::default()
112 }
113 }
114
115 pub fn disabled() -> Self {
117 Self {
118 enabled: false,
119 ..Default::default()
120 }
121 }
122}
123
124#[cfg(test)]
125mod tests {
126 use super::*;
127
128 #[test]
129 fn test_default_config() {
130 let config = CacheConfig::default();
131 assert_eq!(config.max_entries_per_key, 100);
132 assert_eq!(config.max_total_entries, 10_000);
133 assert_eq!(config.eviction_policy, EvictionPolicy::Lru);
134 assert!(!config.persistence.enabled);
135 }
136
137 #[test]
138 fn test_config_builder() {
139 let config = CacheConfig::new()
140 .with_max_entries_per_key(50)
141 .with_max_total_entries(5000)
142 .with_eviction_policy(EvictionPolicy::Lfu)
143 .with_default_ttl(Duration::from_secs(300));
144
145 assert_eq!(config.max_entries_per_key, 50);
146 assert_eq!(config.max_total_entries, 5000);
147 assert_eq!(config.eviction_policy, EvictionPolicy::Lfu);
148 assert_eq!(config.default_ttl, Some(Duration::from_secs(300)));
149 }
150
151 #[test]
152 fn test_persistence_config() {
153 let persistence = PersistenceConfig::enabled();
154 assert!(persistence.enabled);
155 assert_eq!(persistence.sync_interval, 100);
156 assert!(persistence.load_on_startup);
157 }
158
159 #[test]
160 fn test_persistence_config_disabled() {
161 let persistence = PersistenceConfig::disabled();
162 assert!(!persistence.enabled);
163 }
164
165 #[test]
166 fn test_with_persistence_builder() {
167 let p = PersistenceConfig::enabled();
168 let config = CacheConfig::new().with_persistence(p.clone());
169 assert!(config.persistence.enabled);
170 }
171}