rustf 1.0.0

A simple MVC web framework inspired by Total.js
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/// In-memory cache implementation with LRU eviction and TTL support
///
/// Features:
/// - Thread-safe with RwLock
/// - LRU eviction when capacity is reached
/// - TTL support with automatic expiration
/// - Cache statistics and monitoring
/// - Background cleanup of expired entries
use super::{current_timestamp, Cache, CacheConfig, CacheEntry, CacheKey, CacheStats};
use crate::error::{Error, Result};
use log::{debug, info, warn};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::Duration;

/// Thread-safe in-memory cache with LRU eviction
pub struct MemoryCache<T: Clone> {
    data: Arc<RwLock<HashMap<CacheKey, CacheEntry<T>>>>,
    config: CacheConfig,
    stats: Arc<RwLock<CacheStats>>,
}

impl<T: Clone> MemoryCache<T> {
    /// Create a new memory cache with default configuration
    pub fn new() -> Self {
        Self::with_config(CacheConfig::default())
    }

    /// Create a new memory cache with custom configuration
    pub fn with_config(config: CacheConfig) -> Self {
        let mut stats = CacheStats::default();
        stats.max_entries = config.max_entries as u64;

        Self {
            data: Arc::new(RwLock::new(HashMap::new())),
            config,
            stats: Arc::new(RwLock::new(stats)),
        }
    }

    /// Create a new memory cache with capacity limit
    pub fn with_capacity(max_entries: usize) -> Self {
        let mut config = CacheConfig::default();
        config.max_entries = max_entries;
        Self::with_config(config)
    }

    /// Update cache statistics after a hit
    fn record_hit(&self) {
        if self.config.enable_stats {
            if let Ok(mut stats) = self.stats.write() {
                stats.hits += 1;
            }
        }
    }

    /// Update cache statistics after a miss
    fn record_miss(&self) {
        if self.config.enable_stats {
            if let Ok(mut stats) = self.stats.write() {
                stats.misses += 1;
            }
        }
    }

    /// Update cache statistics after an eviction
    fn record_eviction(&self) {
        if self.config.enable_stats {
            if let Ok(mut stats) = self.stats.write() {
                stats.evictions += 1;
            }
        }
    }

    /// Update cache statistics after expired cleanup
    fn record_cleanup(&self, cleaned_count: usize) {
        if self.config.enable_stats {
            if let Ok(mut stats) = self.stats.write() {
                stats.expired_cleanups += cleaned_count as u64;
            }
        }
    }

    /// Update cache statistics entries count
    fn update_entries_count(&self, count: usize) {
        if self.config.enable_stats {
            if let Ok(mut stats) = self.stats.write() {
                stats.entries = count as u64;
            }
        }
    }

    /// Evict least recently used entry to make space
    fn evict_lru(&self, data: &mut HashMap<CacheKey, CacheEntry<T>>) -> Result<()> {
        if data.is_empty() {
            return Ok(());
        }

        // Find the entry with the oldest last_access time
        let lru_key = data
            .iter()
            .min_by_key(|(_, entry)| entry.last_access)
            .map(|(key, _)| key.clone())
            .ok_or_else(|| Error::internal("Failed to find LRU entry for eviction"))?;

        data.remove(&lru_key);
        self.record_eviction();
        debug!("Evicted LRU cache entry: {}", lru_key);

        Ok(())
    }

    /// Remove expired entries from cache
    fn cleanup_expired_internal(&self, data: &mut HashMap<CacheKey, CacheEntry<T>>) -> usize {
        let _now = current_timestamp();
        let mut expired_keys = Vec::new();

        // Collect expired keys
        for (key, entry) in data.iter() {
            if entry.is_expired() {
                expired_keys.push(key.clone());
            }
        }

        // Remove expired entries
        let count = expired_keys.len();
        for key in expired_keys {
            data.remove(&key);
            debug!("Cleaned up expired cache entry: {}", key);
        }

        if count > 0 {
            self.record_cleanup(count);
            debug!("Cleaned up {} expired cache entries", count);
        }

        count
    }

    /// Get cache entry without updating access statistics (internal use)
    fn get_entry_internal(&self, key: &CacheKey) -> Option<CacheEntry<T>> {
        if let Ok(data) = self.data.read() {
            if let Some(entry) = data.get(key) {
                if !entry.is_expired() {
                    return Some(entry.clone());
                }
                // Entry is expired, will be cleaned up later
                debug!("Cache entry '{}' is expired", key);
            }
        }
        None
    }
}

impl<T: Clone> Cache<T> for MemoryCache<T> {
    fn get(&self, key: &CacheKey) -> Option<T> {
        // First try to get the entry
        if let Some(mut entry) = self.get_entry_internal(key) {
            // Update access statistics
            entry.mark_accessed();

            // Update the entry in the cache with new access statistics
            if let Ok(mut data) = self.data.write() {
                if let Some(cache_entry) = data.get_mut(key) {
                    cache_entry.mark_accessed();
                }
            }

            self.record_hit();
            return Some(entry.value);
        }

        self.record_miss();
        None
    }

    fn put(&self, key: CacheKey, value: T, ttl: Option<Duration>) -> Result<()> {
        let ttl = ttl.or(self.config.default_ttl);
        let entry = CacheEntry::new(value, ttl);

        if let Ok(mut data) = self.data.write() {
            // Check if we need to make space
            while data.len() >= self.config.max_entries && !data.is_empty() {
                self.evict_lru(&mut data)?;
            }

            data.insert(key.clone(), entry);
            self.update_entries_count(data.len());

            debug!("Cached entry '{}' (TTL: {:?})", key, ttl);
        } else {
            return Err(Error::internal("Failed to acquire write lock for cache"));
        }

        Ok(())
    }

    fn remove(&self, key: &CacheKey) -> Option<T> {
        if let Ok(mut data) = self.data.write() {
            if let Some(entry) = data.remove(key) {
                self.update_entries_count(data.len());
                debug!("Removed cache entry '{}'", key);
                return Some(entry.value);
            }
        }
        None
    }

    fn clear(&self) {
        if let Ok(mut data) = self.data.write() {
            let count = data.len();
            data.clear();
            self.update_entries_count(0);
            info!("Cleared cache ({} entries)", count);
        }
    }

    fn stats(&self) -> CacheStats {
        let mut stats = if let Ok(stats) = self.stats.read() {
            stats.clone()
        } else {
            CacheStats::default()
        };

        // Update real-time statistics
        if let Ok(data) = self.data.read() {
            stats.entries = data.len() as u64;

            // Calculate average entry age
            if !data.is_empty() {
                let total_age: u64 = data.values().map(|e| e.age_seconds()).sum();
                stats.average_entry_age = total_age as f64 / data.len() as f64;

                // Calculate total access count
                stats.total_access_count = data.values().map(|e| e.access_count).sum();
            }
        }

        stats
    }

    fn contains_key(&self, key: &CacheKey) -> bool {
        if let Ok(data) = self.data.read() {
            if let Some(entry) = data.get(key) {
                return !entry.is_expired();
            }
        }
        false
    }

    fn len(&self) -> usize {
        if let Ok(data) = self.data.read() {
            data.len()
        } else {
            0
        }
    }

    fn cleanup_expired(&self) -> usize {
        if let Ok(mut data) = self.data.write() {
            let count = self.cleanup_expired_internal(&mut data);
            self.update_entries_count(data.len());
            count
        } else {
            warn!("Failed to acquire write lock for cleanup");
            0
        }
    }
}

impl<T: Clone> Default for MemoryCache<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Clone> Clone for MemoryCache<T> {
    fn clone(&self) -> Self {
        Self {
            data: Arc::clone(&self.data),
            config: self.config.clone(),
            stats: Arc::clone(&self.stats),
        }
    }
}

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

    #[test]
    fn test_basic_cache_operations() {
        let cache: MemoryCache<String> = MemoryCache::new();

        // Test put and get
        cache
            .put("key1".to_string(), "value1".to_string(), None)
            .unwrap();
        assert_eq!(cache.get(&"key1".to_string()), Some("value1".to_string()));

        // Test contains_key
        assert!(cache.contains_key(&"key1".to_string()));
        assert!(!cache.contains_key(&"key2".to_string()));

        // Test remove
        assert_eq!(
            cache.remove(&"key1".to_string()),
            Some("value1".to_string())
        );
        assert_eq!(cache.get(&"key1".to_string()), None);
    }

    #[test]
    fn test_cache_capacity_and_eviction() {
        let cache: MemoryCache<String> = MemoryCache::with_capacity(2);

        // Fill cache to capacity
        cache
            .put("key1".to_string(), "value1".to_string(), None)
            .unwrap();
        // Small delay to ensure different millisecond timestamps
        thread::sleep(Duration::from_millis(10));
        cache
            .put("key2".to_string(), "value2".to_string(), None)
            .unwrap();
        assert_eq!(cache.len(), 2);

        // Adding third item should evict LRU (key1)
        cache
            .put("key3".to_string(), "value3".to_string(), None)
            .unwrap();
        assert_eq!(cache.len(), 2);

        // key1 should be evicted (LRU)
        assert_eq!(cache.get(&"key1".to_string()), None);
        assert_eq!(cache.get(&"key2".to_string()), Some("value2".to_string()));
        assert_eq!(cache.get(&"key3".to_string()), Some("value3".to_string()));
    }

    #[test]
    fn test_ttl_expiration() {
        let cache: MemoryCache<String> = MemoryCache::new();

        // Put with 1 second TTL (cache now uses millisecond precision)
        cache
            .put(
                "key1".to_string(),
                "value1".to_string(),
                Some(Duration::from_secs(1)),
            )
            .unwrap();
        assert_eq!(cache.get(&"key1".to_string()), Some("value1".to_string()));

        // Wait for expiration (slightly more than TTL to account for timing)
        thread::sleep(Duration::from_secs(2));
        assert_eq!(cache.get(&"key1".to_string()), None);
    }

    #[test]
    fn test_cache_statistics() {
        let cache: MemoryCache<String> = MemoryCache::new();

        // Generate some cache activity
        cache
            .put("key1".to_string(), "value1".to_string(), None)
            .unwrap();
        cache.get(&"key1".to_string()); // hit
        cache.get(&"key2".to_string()); // miss

        let stats = cache.stats();
        assert_eq!(stats.hits, 1);
        assert_eq!(stats.misses, 1);
        assert_eq!(stats.entries, 1);
        assert_eq!(stats.hit_rate(), 0.5);
    }

    #[test]
    fn test_cleanup_expired() {
        let cache: MemoryCache<String> = MemoryCache::new();

        // Add entries with different TTLs (cache uses second precision)
        cache
            .put(
                "key1".to_string(),
                "value1".to_string(),
                Some(Duration::from_secs(1)),
            )
            .unwrap();
        cache
            .put("key2".to_string(), "value2".to_string(), None)
            .unwrap(); // No expiration

        // Wait for expiration
        thread::sleep(Duration::from_secs(2));

        let cleaned = cache.cleanup_expired();
        assert_eq!(cleaned, 1);
        assert_eq!(cache.len(), 1);
        assert_eq!(cache.get(&"key2".to_string()), Some("value2".to_string()));
    }

    #[test]
    fn test_thread_safety() {
        let cache: MemoryCache<String> = MemoryCache::new();
        let cache_clone = cache.clone();

        let handle = thread::spawn(move || {
            for i in 0..100 {
                cache_clone
                    .put(format!("key{}", i), format!("value{}", i), None)
                    .unwrap();
            }
        });

        for i in 0..100 {
            cache
                .put(
                    format!("thread1_key{}", i),
                    format!("thread1_value{}", i),
                    None,
                )
                .unwrap();
        }

        handle.join().unwrap();

        // Both threads should have successfully added entries
        assert!(cache.len() > 0);
        let stats = cache.stats();
        assert!(stats.entries > 0);
    }
}