arch-toolkit 0.2.0

Complete Rust toolkit for Arch Linux package management
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! In-memory LRU cache implementation.

use super::{Cache, CacheError};
use lru::LruCache;
use serde::{Deserialize, Serialize};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

/// What: Cache entry with value and expiration timestamp.
///
/// Inputs: None (created internally)
///
/// Output:
/// - `CacheEntry<V>` containing cached value and expiration time
///
/// Details:
/// - Stores the cached value along with when it expires
/// - Used internally by `MemoryCache`
struct CacheEntry<V> {
    /// Cached value.
    value: V,
    /// Expiration timestamp.
    expires_at: Instant,
}

/// What: In-memory LRU cache with TTL expiration.
///
/// Inputs: None (created via `MemoryCache::new()`)
///
/// Output:
/// - `MemoryCache` instance ready for use
///
/// Details:
/// - Uses LRU eviction when cache is full
/// - Supports TTL-based expiration
/// - Thread-safe via `Arc<Mutex<>>`
/// - Generic over value type
#[derive(Debug)]
pub struct MemoryCache {
    /// Internal LRU cache wrapped in mutex for thread safety.
    cache: Arc<Mutex<LruCache<String, CacheEntry<Vec<u8>>>>>,
}

impl MemoryCache {
    /// What: Create a new memory cache with specified capacity.
    ///
    /// Inputs:
    /// - `capacity`: Maximum number of entries in cache
    ///
    /// Output:
    /// - `MemoryCache` instance
    ///
    /// Details:
    /// - Creates LRU cache with specified capacity
    /// - Wraps in `Arc<Mutex<>>` for thread safety
    pub fn new(capacity: usize) -> Self {
        Self {
            cache: Arc::new(Mutex::new(LruCache::new(
                std::num::NonZeroUsize::new(capacity.max(1))
                    .expect("capacity.max(1) should always be >= 1"),
            ))),
        }
    }

    /// What: Clean up expired entries from cache.
    ///
    /// Inputs: None
    ///
    /// Output: None
    ///
    /// Details:
    /// - Removes all expired entries
    /// - Called automatically during get operations
    /// - May be called manually for cleanup
    fn cleanup_expired(&self) {
        let mut cache = match self.cache.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };

        let now = Instant::now();
        let keys_to_remove: Vec<String> = cache
            .iter()
            .filter(|(_, entry)| entry.expires_at < now)
            .map(|(key, _)| key.clone())
            .collect();

        for key in keys_to_remove {
            cache.pop(&key);
        }
    }
}

impl<K, V> Cache<K, V> for MemoryCache
where
    K: AsRef<str>,
    V: Clone + Serialize + for<'de> Deserialize<'de>,
{
    /// What: Get a value from the cache.
    ///
    /// Inputs:
    /// - `key`: Cache key to look up
    ///
    /// Output:
    /// - `Option<V>` containing cached value if found and not expired, `None` otherwise
    ///
    /// Details:
    /// - Checks if entry exists and is not expired
    /// - Returns `None` if expired or not found
    /// - Automatically cleans up expired entries
    fn get(&self, key: &K) -> Option<V> {
        self.cleanup_expired();

        let mut cache = match self.cache.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };

        let key_str = key.as_ref();
        let entry = cache.get(key_str)?;

        // Check if expired
        if entry.expires_at < Instant::now() {
            cache.pop(key_str);
            return None;
        }

        // Clone the serialized value before dropping the guard
        let value = entry.value.clone();
        drop(cache); // Early drop of guard

        // Deserialize value
        serde_json::from_slice(&value).ok()
    }

    /// What: Store a value in the cache.
    ///
    /// Inputs:
    /// - `key`: Cache key
    /// - `value`: Value to cache
    /// - `ttl`: Time-to-live duration
    ///
    /// Output:
    /// - `Result<(), CacheError>` indicating success or failure
    ///
    /// Details:
    /// - Serializes value to JSON bytes
    /// - Stores with expiration timestamp
    /// - May evict least recently used entry if cache is full
    fn set(&self, key: &K, value: &V, ttl: Duration) -> Result<(), CacheError> {
        let serialized =
            serde_json::to_vec(value).map_err(|e| CacheError::Serialization(e.to_string()))?;

        let expires_at = Instant::now() + ttl;
        let entry = CacheEntry {
            value: serialized,
            expires_at,
        };
        let key_string = key.as_ref().to_string();

        {
            let mut cache = match self.cache.lock() {
                Ok(guard) => guard,
                Err(poisoned) => poisoned.into_inner(),
            };
            cache.put(key_string, entry);
        }
        Ok(())
    }

    /// What: Invalidate a specific cache entry.
    ///
    /// Inputs:
    /// - `key`: Cache key to invalidate
    ///
    /// Output:
    /// - `Result<(), CacheError>` indicating success or failure
    ///
    /// Details:
    /// - Removes the entry from cache
    /// - Safe to call if key doesn't exist
    fn invalidate(&self, key: &K) -> Result<(), CacheError> {
        let key_str = key.as_ref();
        {
            let mut cache = match self.cache.lock() {
                Ok(guard) => guard,
                Err(poisoned) => poisoned.into_inner(),
            };
            cache.pop(key_str);
        }
        Ok(())
    }

    /// What: Clear all entries from the cache.
    ///
    /// Inputs: None
    ///
    /// Output:
    /// - `Result<(), CacheError>` indicating success or failure
    ///
    /// Details:
    /// - Removes all entries from cache
    fn clear(&self) -> Result<(), CacheError> {
        {
            let mut cache = match self.cache.lock() {
                Ok(guard) => guard,
                Err(poisoned) => poisoned.into_inner(),
            };
            cache.clear();
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread;
    use std::time::Duration as StdDuration;

    // Allow unwrap in tests - test failures should panic
    #[allow(clippy::unwrap_used)]
    #[test]
    fn test_memory_cache_get_set() {
        let cache = MemoryCache::new(10);
        let key = "test_key".to_string();
        let value = "test_value".to_string();

        // Initially empty
        assert!(<MemoryCache as Cache<String, String>>::get(&cache, &key).is_none());

        // Set value
        <MemoryCache as Cache<String, String>>::set(
            &cache,
            &key,
            &value,
            StdDuration::from_secs(60),
        )
        .unwrap();

        // Get value
        let retrieved = <MemoryCache as Cache<String, String>>::get(&cache, &key);
        assert_eq!(retrieved, Some(value));
    }

    // Allow unwrap in tests - test failures should panic
    #[allow(clippy::unwrap_used)]
    #[test]
    fn test_memory_cache_ttl_expiration() {
        let cache = MemoryCache::new(10);
        let key = "test_key".to_string();
        let value = "test_value".to_string();

        // Set value with short TTL
        <MemoryCache as Cache<String, String>>::set(
            &cache,
            &key,
            &value,
            StdDuration::from_millis(100),
        )
        .unwrap();

        // Should be available immediately
        assert!(<MemoryCache as Cache<String, String>>::get(&cache, &key).is_some());

        // Wait for expiration
        thread::sleep(StdDuration::from_millis(150));

        // Should be expired
        assert!(<MemoryCache as Cache<String, String>>::get(&cache, &key).is_none());
    }

    // Allow unwrap in tests - test failures should panic
    #[allow(clippy::unwrap_used)]
    #[test]
    fn test_memory_cache_lru_eviction() {
        let cache = MemoryCache::new(2);
        let value1 = "value1".to_string();
        let value2 = "value2".to_string();
        let value3 = "value3".to_string();

        // Fill cache to capacity
        <MemoryCache as Cache<String, String>>::set(
            &cache,
            &"key1".to_string(),
            &value1,
            StdDuration::from_secs(60),
        )
        .unwrap();
        <MemoryCache as Cache<String, String>>::set(
            &cache,
            &"key2".to_string(),
            &value2,
            StdDuration::from_secs(60),
        )
        .unwrap();

        // Access key1 to make it more recently used
        <MemoryCache as Cache<String, String>>::get(&cache, &"key1".to_string());

        // Add third entry - should evict key2 (least recently used)
        <MemoryCache as Cache<String, String>>::set(
            &cache,
            &"key3".to_string(),
            &value3,
            StdDuration::from_secs(60),
        )
        .unwrap();

        // key1 and key3 should be present, key2 should be evicted
        assert!(<MemoryCache as Cache<String, String>>::get(&cache, &"key1".to_string()).is_some());
        assert!(<MemoryCache as Cache<String, String>>::get(&cache, &"key2".to_string()).is_none());
        assert!(<MemoryCache as Cache<String, String>>::get(&cache, &"key3".to_string()).is_some());
    }

    // Allow unwrap in tests - test failures should panic
    #[allow(clippy::unwrap_used)]
    #[test]
    fn test_memory_cache_invalidate() {
        let cache = MemoryCache::new(10);
        let key = "test_key".to_string();
        let value = "test_value".to_string();

        <MemoryCache as Cache<String, String>>::set(
            &cache,
            &key,
            &value,
            StdDuration::from_secs(60),
        )
        .unwrap();
        assert!(<MemoryCache as Cache<String, String>>::get(&cache, &key).is_some());

        <MemoryCache as Cache<String, String>>::invalidate(&cache, &key).unwrap();
        assert!(<MemoryCache as Cache<String, String>>::get(&cache, &key).is_none());
    }

    // Allow unwrap in tests - test failures should panic
    #[allow(clippy::unwrap_used)]
    #[test]
    fn test_memory_cache_clear() {
        let cache = MemoryCache::new(10);
        let value = "test_value".to_string();

        <MemoryCache as Cache<String, String>>::set(
            &cache,
            &"key1".to_string(),
            &value,
            StdDuration::from_secs(60),
        )
        .unwrap();
        <MemoryCache as Cache<String, String>>::set(
            &cache,
            &"key2".to_string(),
            &value,
            StdDuration::from_secs(60),
        )
        .unwrap();

        <MemoryCache as Cache<String, String>>::clear(&cache).unwrap();

        assert!(<MemoryCache as Cache<String, String>>::get(&cache, &"key1".to_string()).is_none());
        assert!(<MemoryCache as Cache<String, String>>::get(&cache, &"key2".to_string()).is_none());
    }

    // Allow unwrap in tests - test failures should panic
    #[allow(clippy::unwrap_used)]
    #[test]
    fn test_memory_cache_thread_safety() {
        let cache = Arc::new(MemoryCache::new(100));
        let mut handles = vec![];

        // Spawn multiple threads writing to cache
        for i in 0..10 {
            let cache_clone = Arc::clone(&cache);
            let handle = thread::spawn(move || {
                for j in 0..10 {
                    let key = format!("key_{i}_{j}");
                    let value = format!("value_{i}_{j}");
                    <MemoryCache as Cache<String, String>>::set(
                        &cache_clone,
                        &key,
                        &value,
                        StdDuration::from_secs(60),
                    )
                    .unwrap();
                }
            });
            handles.push(handle);
        }

        // Wait for all threads
        for handle in handles {
            handle.join().unwrap();
        }

        // Verify all entries are present
        for i in 0..10 {
            for j in 0..10 {
                let key = format!("key_{i}_{j}");
                let expected = format!("value_{i}_{j}");
                let retrieved = <MemoryCache as Cache<String, String>>::get(&cache, &key);
                assert_eq!(retrieved, Some(expected));
            }
        }
    }
}