arch_toolkit/cache/config.rs
1//! Cache configuration for arch-toolkit operations.
2
3use std::time::Duration;
4
5/// What: Configuration for cache behavior.
6///
7/// Inputs: None (created via `CacheConfig::builder()` or `default()`)
8///
9/// Output:
10/// - `CacheConfig` instance with configurable cache settings
11///
12/// Details:
13/// - Controls per-operation cache enable/disable flags
14/// - Configures TTL for each operation type
15/// - Sets memory cache size limits
16/// - Controls disk cache enable/disable
17#[derive(Debug, Clone)]
18#[allow(clippy::struct_excessive_bools)] // Per-operation flags are necessary for fine-grained control
19pub struct CacheConfig {
20 /// Whether search operation caching is enabled (default: false).
21 pub enable_search: bool,
22 /// TTL for search cache entries (default: 5 minutes).
23 pub search_ttl: Duration,
24 /// Whether info operation caching is enabled (default: false).
25 pub enable_info: bool,
26 /// TTL for info cache entries (default: 15 minutes).
27 pub info_ttl: Duration,
28 /// Whether comments operation caching is enabled (default: false).
29 pub enable_comments: bool,
30 /// TTL for comments cache entries (default: 10 minutes).
31 pub comments_ttl: Duration,
32 /// Whether pkgbuild operation caching is enabled (default: false).
33 pub enable_pkgbuild: bool,
34 /// TTL for pkgbuild cache entries (default: 1 hour).
35 pub pkgbuild_ttl: Duration,
36 /// Maximum number of entries in memory cache per operation (default: 100).
37 pub memory_cache_size: usize,
38 /// Whether disk cache is enabled (default: false).
39 pub enable_disk_cache: bool,
40}
41
42impl Default for CacheConfig {
43 fn default() -> Self {
44 Self {
45 enable_search: false,
46 search_ttl: Duration::from_secs(300), // 5 minutes
47 enable_info: false,
48 info_ttl: Duration::from_secs(900), // 15 minutes
49 enable_comments: false,
50 comments_ttl: Duration::from_secs(600), // 10 minutes
51 enable_pkgbuild: false,
52 pkgbuild_ttl: Duration::from_secs(3600), // 1 hour
53 memory_cache_size: 100,
54 enable_disk_cache: false,
55 }
56 }
57}
58
59/// What: Builder for creating `CacheConfig` with custom settings.
60///
61/// Inputs: None (created via `CacheConfig::builder()`)
62///
63/// Output:
64/// - `CacheConfigBuilder` that can be configured and built
65///
66/// Details:
67/// - Allows customization of all cache settings
68/// - All methods return `&mut Self` for method chaining
69/// - Call `build()` to create the `CacheConfig`
70#[derive(Debug, Clone)]
71pub struct CacheConfigBuilder {
72 /// Internal cache configuration being built.
73 config: CacheConfig,
74}
75
76impl CacheConfigBuilder {
77 /// What: Create a new builder with default values.
78 ///
79 /// Inputs: None
80 ///
81 /// Output:
82 /// - `CacheConfigBuilder` with default configuration
83 ///
84 /// Details:
85 /// - Starts with all caches disabled
86 /// - Uses default TTL values
87 #[must_use]
88 #[allow(clippy::missing_const_for_fn)] // Cannot be const: uses Default::default()
89 pub fn new() -> Self {
90 Self {
91 config: CacheConfig::default(),
92 }
93 }
94
95 /// What: Enable or disable search operation caching.
96 ///
97 /// Inputs:
98 /// - `enable`: Whether to enable search caching
99 ///
100 /// Output:
101 /// - `&mut Self` for method chaining
102 ///
103 /// Details:
104 /// - Default: false
105 #[must_use]
106 #[allow(clippy::missing_const_for_fn)] // Cannot be const: mutates self
107 pub fn enable_search(mut self, enable: bool) -> Self {
108 self.config.enable_search = enable;
109 self
110 }
111
112 /// What: Set TTL for search cache entries.
113 ///
114 /// Inputs:
115 /// - `ttl`: Time-to-live duration
116 ///
117 /// Output:
118 /// - `&mut Self` for method chaining
119 ///
120 /// Details:
121 /// - Default: 5 minutes
122 #[must_use]
123 #[allow(clippy::missing_const_for_fn)] // Cannot be const: mutates self and uses Duration
124 pub fn search_ttl(mut self, ttl: Duration) -> Self {
125 self.config.search_ttl = ttl;
126 self
127 }
128
129 /// What: Enable or disable info operation caching.
130 ///
131 /// Inputs:
132 /// - `enable`: Whether to enable info caching
133 ///
134 /// Output:
135 /// - `&mut Self` for method chaining
136 ///
137 /// Details:
138 /// - Default: false
139 #[must_use]
140 #[allow(clippy::missing_const_for_fn)] // Cannot be const: mutates self
141 pub fn enable_info(mut self, enable: bool) -> Self {
142 self.config.enable_info = enable;
143 self
144 }
145
146 /// What: Set TTL for info cache entries.
147 ///
148 /// Inputs:
149 /// - `ttl`: Time-to-live duration
150 ///
151 /// Output:
152 /// - `&mut Self` for method chaining
153 ///
154 /// Details:
155 /// - Default: 15 minutes
156 #[must_use]
157 #[allow(clippy::missing_const_for_fn)] // Cannot be const: mutates self and uses Duration
158 pub fn info_ttl(mut self, ttl: Duration) -> Self {
159 self.config.info_ttl = ttl;
160 self
161 }
162
163 /// What: Enable or disable comments operation caching.
164 ///
165 /// Inputs:
166 /// - `enable`: Whether to enable comments caching
167 ///
168 /// Output:
169 /// - `&mut Self` for method chaining
170 ///
171 /// Details:
172 /// - Default: false
173 #[must_use]
174 #[allow(clippy::missing_const_for_fn)] // Cannot be const: mutates self
175 pub fn enable_comments(mut self, enable: bool) -> Self {
176 self.config.enable_comments = enable;
177 self
178 }
179
180 /// What: Set TTL for comments cache entries.
181 ///
182 /// Inputs:
183 /// - `ttl`: Time-to-live duration
184 ///
185 /// Output:
186 /// - `&mut Self` for method chaining
187 ///
188 /// Details:
189 /// - Default: 10 minutes
190 #[must_use]
191 #[allow(clippy::missing_const_for_fn)] // Cannot be const: mutates self and uses Duration
192 pub fn comments_ttl(mut self, ttl: Duration) -> Self {
193 self.config.comments_ttl = ttl;
194 self
195 }
196
197 /// What: Enable or disable pkgbuild operation caching.
198 ///
199 /// Inputs:
200 /// - `enable`: Whether to enable pkgbuild caching
201 ///
202 /// Output:
203 /// - `&mut Self` for method chaining
204 ///
205 /// Details:
206 /// - Default: false
207 #[must_use]
208 #[allow(clippy::missing_const_for_fn)] // Cannot be const: mutates self
209 pub fn enable_pkgbuild(mut self, enable: bool) -> Self {
210 self.config.enable_pkgbuild = enable;
211 self
212 }
213
214 /// What: Set TTL for pkgbuild cache entries.
215 ///
216 /// Inputs:
217 /// - `ttl`: Time-to-live duration
218 ///
219 /// Output:
220 /// - `&mut Self` for method chaining
221 ///
222 /// Details:
223 /// - Default: 1 hour
224 #[must_use]
225 #[allow(clippy::missing_const_for_fn)] // Cannot be const: mutates self and uses Duration
226 pub fn pkgbuild_ttl(mut self, ttl: Duration) -> Self {
227 self.config.pkgbuild_ttl = ttl;
228 self
229 }
230
231 /// What: Set maximum number of entries in memory cache per operation.
232 ///
233 /// Inputs:
234 /// - `size`: Maximum cache size
235 ///
236 /// Output:
237 /// - `&mut Self` for method chaining
238 ///
239 /// Details:
240 /// - Default: 100
241 #[must_use]
242 #[allow(clippy::missing_const_for_fn)] // Cannot be const: mutates self
243 pub fn memory_cache_size(mut self, size: usize) -> Self {
244 self.config.memory_cache_size = size;
245 self
246 }
247
248 /// What: Enable or disable disk cache.
249 ///
250 /// Inputs:
251 /// - `enable`: Whether to enable disk cache
252 ///
253 /// Output:
254 /// - `&mut Self` for method chaining
255 ///
256 /// Details:
257 /// - Default: false
258 #[must_use]
259 #[allow(clippy::missing_const_for_fn)] // Cannot be const: mutates self
260 pub fn enable_disk_cache(mut self, enable: bool) -> Self {
261 self.config.enable_disk_cache = enable;
262 self
263 }
264
265 /// What: Build the `CacheConfig` with configured settings.
266 ///
267 /// Inputs: None
268 ///
269 /// Output:
270 /// - `CacheConfig` with configured values
271 ///
272 /// Details:
273 /// - Consumes the builder
274 /// - Returns the configured `CacheConfig`
275 #[must_use]
276 #[allow(clippy::missing_const_for_fn)] // Cannot be const: consumes self
277 pub fn build(self) -> CacheConfig {
278 self.config
279 }
280}
281
282impl Default for CacheConfigBuilder {
283 fn default() -> Self {
284 Self::new()
285 }
286}
287
288#[cfg(test)]
289mod tests {
290 use super::*;
291
292 #[test]
293 fn test_cache_config_default() {
294 let config = CacheConfig::default();
295 assert!(!config.enable_search);
296 assert!(!config.enable_info);
297 assert!(!config.enable_comments);
298 assert!(!config.enable_pkgbuild);
299 assert!(!config.enable_disk_cache);
300 assert_eq!(config.memory_cache_size, 100);
301 assert_eq!(config.search_ttl, Duration::from_secs(300));
302 assert_eq!(config.info_ttl, Duration::from_secs(900));
303 assert_eq!(config.comments_ttl, Duration::from_secs(600));
304 assert_eq!(config.pkgbuild_ttl, Duration::from_secs(3600));
305 }
306
307 #[test]
308 fn test_cache_config_builder() {
309 let config = CacheConfigBuilder::new()
310 .enable_search(true)
311 .search_ttl(Duration::from_secs(600))
312 .enable_info(true)
313 .info_ttl(Duration::from_secs(1800))
314 .memory_cache_size(200)
315 .enable_disk_cache(true)
316 .build();
317
318 assert!(config.enable_search);
319 assert!(config.enable_info);
320 assert_eq!(config.search_ttl, Duration::from_secs(600));
321 assert_eq!(config.info_ttl, Duration::from_secs(1800));
322 assert_eq!(config.memory_cache_size, 200);
323 assert!(config.enable_disk_cache);
324 }
325}