Skip to main content

threatflux_cache/
config.rs

1//! Configuration types for the cache
2
3use serde::{Deserialize, Serialize};
4use std::time::Duration;
5
6/// Cache configuration
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct CacheConfig {
9    /// Maximum number of entries per key
10    pub max_entries_per_key: usize,
11    /// Maximum total number of entries
12    pub max_total_entries: usize,
13    /// Eviction policy to use
14    pub eviction_policy: EvictionPolicy,
15    /// Persistence configuration
16    pub persistence: PersistenceConfig,
17    /// Default TTL for entries (if not specified per-entry)
18    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    /// Create a new cache configuration with default values
35    pub fn new() -> Self {
36        Self::default()
37    }
38
39    /// Set maximum entries per key
40    pub fn with_max_entries_per_key(mut self, max: usize) -> Self {
41        self.max_entries_per_key = max;
42        self
43    }
44
45    /// Set maximum total entries
46    pub fn with_max_total_entries(mut self, max: usize) -> Self {
47        self.max_total_entries = max;
48        self
49    }
50
51    /// Set eviction policy
52    pub fn with_eviction_policy(mut self, policy: EvictionPolicy) -> Self {
53        self.eviction_policy = policy;
54        self
55    }
56
57    /// Set persistence configuration
58    pub fn with_persistence(mut self, persistence: PersistenceConfig) -> Self {
59        self.persistence = persistence;
60        self
61    }
62
63    /// Set default TTL for entries
64    pub fn with_default_ttl(mut self, ttl: Duration) -> Self {
65        self.default_ttl = Some(ttl);
66        self
67    }
68}
69
70/// Eviction policy for cache entries
71#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
72pub enum EvictionPolicy {
73    /// Least Recently Used
74    Lru,
75    /// Least Frequently Used
76    Lfu,
77    /// First In First Out
78    Fifo,
79    /// Time To Live based
80    Ttl,
81    /// No eviction (manual only)
82    None,
83}
84
85/// Persistence configuration
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct PersistenceConfig {
88    /// Enable persistence
89    pub enabled: bool,
90    /// Sync to disk after every N operations
91    pub sync_interval: usize,
92    /// Load existing cache on startup
93    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    /// Create an enabled persistence configuration.
108    pub fn enabled() -> Self {
109        Self {
110            enabled: true,
111            ..Default::default()
112        }
113    }
114
115    /// Disable persistence
116    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}