oximedia-recommend 0.1.8

Content recommendation engine for media libraries
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
435
//! Score caching layer for recommendation systems.
//!
//! This module provides an in-memory cache for pre-computed recommendation
//! scores, supporting TTL-based expiration and LRU-like eviction to avoid
//! recomputing expensive similarity and ranking scores on every request.

#![allow(dead_code)]

use std::collections::HashMap;
use uuid::Uuid;

/// A single cached score entry with expiration metadata.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CacheEntry {
    /// The cached score value
    pub score: f64,
    /// Unix timestamp when this entry was created
    pub created_at: i64,
    /// Unix timestamp when this entry expires
    pub expires_at: i64,
    /// Number of times this entry was accessed
    pub access_count: u64,
    /// Unix timestamp of last access
    pub last_accessed: i64,
}

impl CacheEntry {
    /// Creates a new cache entry with the given TTL in seconds.
    #[must_use]
    pub fn new(score: f64, now: i64, ttl_secs: i64) -> Self {
        Self {
            score,
            created_at: now,
            expires_at: now + ttl_secs,
            access_count: 0,
            last_accessed: now,
        }
    }

    /// Returns true if this entry has expired at the given timestamp.
    #[must_use]
    pub fn is_expired(&self, now: i64) -> bool {
        now >= self.expires_at
    }

    /// Returns the remaining TTL in seconds, or 0 if expired.
    #[must_use]
    pub fn remaining_ttl(&self, now: i64) -> i64 {
        (self.expires_at - now).max(0)
    }

    /// Returns the age of this entry in seconds.
    #[must_use]
    pub fn age(&self, now: i64) -> i64 {
        now - self.created_at
    }

    /// Records an access, updating the access count and timestamp.
    pub fn record_access(&mut self, now: i64) {
        self.access_count += 1;
        self.last_accessed = now;
    }
}

/// Cache key combining user and content identifiers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CacheKey {
    /// User ID
    pub user_id: Uuid,
    /// Content ID
    pub content_id: Uuid,
}

impl CacheKey {
    /// Creates a new cache key.
    #[must_use]
    pub fn new(user_id: Uuid, content_id: Uuid) -> Self {
        Self {
            user_id,
            content_id,
        }
    }
}

/// Cache statistics for monitoring.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct CacheStats {
    /// Total number of cache lookups
    pub lookups: u64,
    /// Number of cache hits
    pub hits: u64,
    /// Number of cache misses
    pub misses: u64,
    /// Number of entries evicted
    pub evictions: u64,
    /// Number of expired entries removed
    pub expirations: u64,
}

impl CacheStats {
    /// Returns the hit rate as a fraction (0.0-1.0).
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn hit_rate(&self) -> f64 {
        if self.lookups == 0 {
            return 0.0;
        }
        self.hits as f64 / self.lookups as f64
    }
}

/// An in-memory score cache with TTL-based expiration and capacity limits.
///
/// Supports `get_or_compute()` for transparent caching: if a score is
/// already cached and valid, it is returned; otherwise, the provided
/// compute function is called and its result is stored.
#[derive(Debug)]
pub struct ScoreCache {
    /// Cached entries
    entries: HashMap<CacheKey, CacheEntry>,
    /// Default TTL in seconds
    default_ttl_secs: i64,
    /// Maximum number of entries
    max_capacity: usize,
    /// Cache statistics
    stats: CacheStats,
}

impl ScoreCache {
    /// Creates a new score cache.
    ///
    /// # Arguments
    ///
    /// * `default_ttl_secs` - Default time-to-live for entries in seconds.
    /// * `max_capacity` - Maximum number of cached entries.
    #[must_use]
    pub fn new(default_ttl_secs: i64, max_capacity: usize) -> Self {
        Self {
            entries: HashMap::new(),
            default_ttl_secs,
            max_capacity,
            stats: CacheStats::default(),
        }
    }

    /// Looks up a cached score.
    ///
    /// Returns `Some(score)` if the entry exists and is not expired.
    /// Returns `None` on miss or expiration.
    pub fn get(&mut self, key: &CacheKey, now: i64) -> Option<f64> {
        self.stats.lookups += 1;

        let entry = if let Some(e) = self.entries.get_mut(key) {
            e
        } else {
            self.stats.misses += 1;
            return None;
        };
        if entry.is_expired(now) {
            self.stats.misses += 1;
            // Remove expired entry
            self.entries.remove(key);
            self.stats.expirations += 1;
            return None;
        }

        entry.record_access(now);
        self.stats.hits += 1;
        Some(entry.score)
    }

    /// Inserts a score into the cache.
    ///
    /// If the cache is at capacity, evicts the least-recently-accessed entry first.
    pub fn put(&mut self, key: CacheKey, score: f64, now: i64) {
        self.put_with_ttl(key, score, now, self.default_ttl_secs);
    }

    /// Inserts a score with a custom TTL.
    pub fn put_with_ttl(&mut self, key: CacheKey, score: f64, now: i64, ttl_secs: i64) {
        if self.entries.len() >= self.max_capacity && !self.entries.contains_key(&key) {
            self.evict_lru();
        }
        self.entries
            .insert(key, CacheEntry::new(score, now, ttl_secs));
    }

    /// Gets a cached score, or computes and caches it if missing/expired.
    ///
    /// The `compute` closure is only called on a cache miss.
    pub fn get_or_compute<F>(&mut self, key: CacheKey, now: i64, compute: F) -> f64
    where
        F: FnOnce() -> f64,
    {
        if let Some(score) = self.get(&key, now) {
            return score;
        }
        let score = compute();
        self.put(key, score, now);
        score
    }

    /// Removes all expired entries from the cache.
    pub fn evict_expired(&mut self, now: i64) {
        let before = self.entries.len();
        self.entries.retain(|_, entry| !entry.is_expired(now));
        let removed = before - self.entries.len();
        self.stats.expirations += removed as u64;
    }

    /// Evicts the least-recently-accessed entry.
    fn evict_lru(&mut self) {
        if self.entries.is_empty() {
            return;
        }

        let lru_key = self
            .entries
            .iter()
            .min_by_key(|(_, entry)| entry.last_accessed)
            .map(|(k, _)| *k);

        if let Some(key) = lru_key {
            self.entries.remove(&key);
            self.stats.evictions += 1;
        }
    }

    /// Invalidates (removes) a specific entry.
    pub fn invalidate(&mut self, key: &CacheKey) -> bool {
        self.entries.remove(key).is_some()
    }

    /// Invalidates all entries for a given user.
    pub fn invalidate_user(&mut self, user_id: Uuid) {
        self.entries.retain(|k, _| k.user_id != user_id);
    }

    /// Invalidates all entries for a given content item.
    pub fn invalidate_content(&mut self, content_id: Uuid) {
        self.entries.retain(|k, _| k.content_id != content_id);
    }

    /// Clears all entries from the cache.
    pub fn clear(&mut self) {
        self.entries.clear();
    }

    /// Returns the number of entries currently in the cache.
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Returns true if the cache is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Returns a reference to cache statistics.
    #[must_use]
    pub fn stats(&self) -> &CacheStats {
        &self.stats
    }

    /// Resets cache statistics to zero.
    pub fn reset_stats(&mut self) {
        self.stats = CacheStats::default();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn uid() -> Uuid {
        Uuid::new_v4()
    }

    #[test]
    fn test_cache_entry_creation() {
        let entry = CacheEntry::new(0.85, 1000, 60);
        assert!((entry.score - 0.85).abs() < f64::EPSILON);
        assert_eq!(entry.expires_at, 1060);
        assert_eq!(entry.access_count, 0);
    }

    #[test]
    fn test_cache_entry_expiration() {
        let entry = CacheEntry::new(1.0, 1000, 60);
        assert!(!entry.is_expired(1050));
        assert!(entry.is_expired(1060));
        assert!(entry.is_expired(1100));
    }

    #[test]
    fn test_cache_entry_remaining_ttl() {
        let entry = CacheEntry::new(1.0, 1000, 60);
        assert_eq!(entry.remaining_ttl(1000), 60);
        assert_eq!(entry.remaining_ttl(1030), 30);
        assert_eq!(entry.remaining_ttl(1070), 0);
    }

    #[test]
    fn test_cache_entry_age() {
        let entry = CacheEntry::new(1.0, 1000, 60);
        assert_eq!(entry.age(1000), 0);
        assert_eq!(entry.age(1025), 25);
    }

    #[test]
    fn test_cache_entry_record_access() {
        let mut entry = CacheEntry::new(1.0, 1000, 60);
        entry.record_access(1010);
        assert_eq!(entry.access_count, 1);
        assert_eq!(entry.last_accessed, 1010);
        entry.record_access(1020);
        assert_eq!(entry.access_count, 2);
    }

    #[test]
    fn test_cache_put_and_get() {
        let mut cache = ScoreCache::new(300, 100);
        let key = CacheKey::new(uid(), uid());
        cache.put(key, 0.92, 1000);
        let val = cache.get(&key, 1001).expect("should succeed in test");
        assert!((val - 0.92).abs() < f64::EPSILON);
    }

    #[test]
    fn test_cache_miss() {
        let mut cache = ScoreCache::new(300, 100);
        let key = CacheKey::new(uid(), uid());
        assert!(cache.get(&key, 1000).is_none());
        assert_eq!(cache.stats().misses, 1);
    }

    #[test]
    fn test_cache_expired_entry_returns_none() {
        let mut cache = ScoreCache::new(60, 100);
        let key = CacheKey::new(uid(), uid());
        cache.put(key, 0.5, 1000);
        assert!(cache.get(&key, 1061).is_none());
    }

    #[test]
    fn test_get_or_compute_miss() {
        let mut cache = ScoreCache::new(300, 100);
        let key = CacheKey::new(uid(), uid());
        let val = cache.get_or_compute(key, 1000, || 0.77);
        assert!((val - 0.77).abs() < f64::EPSILON);
        assert_eq!(cache.len(), 1);
    }

    #[test]
    fn test_get_or_compute_hit() {
        let mut cache = ScoreCache::new(300, 100);
        let key = CacheKey::new(uid(), uid());
        cache.put(key, 0.5, 1000);
        let val = cache.get_or_compute(key, 1001, || 0.99);
        // Should return cached value, not compute
        assert!((val - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn test_evict_expired() {
        let mut cache = ScoreCache::new(60, 100);
        let k1 = CacheKey::new(uid(), uid());
        let k2 = CacheKey::new(uid(), uid());
        cache.put(k1, 0.1, 1000);
        cache.put(k2, 0.2, 1050);
        cache.evict_expired(1061);
        // k1 expired (created at 1000, ttl 60 => expires 1060), k2 alive (expires 1110)
        assert_eq!(cache.len(), 1);
    }

    #[test]
    fn test_capacity_eviction() {
        let mut cache = ScoreCache::new(300, 2);
        let k1 = CacheKey::new(uid(), uid());
        let k2 = CacheKey::new(uid(), uid());
        let k3 = CacheKey::new(uid(), uid());
        cache.put(k1, 0.1, 1000);
        cache.put(k2, 0.2, 1001);
        // Access k1 so k2 becomes LRU
        let _ = cache.get(&k1, 1002);
        cache.put(k3, 0.3, 1003);
        assert_eq!(cache.len(), 2);
        assert_eq!(cache.stats().evictions, 1);
    }

    #[test]
    fn test_invalidate_specific() {
        let mut cache = ScoreCache::new(300, 100);
        let key = CacheKey::new(uid(), uid());
        cache.put(key, 0.5, 1000);
        assert!(cache.invalidate(&key));
        assert!(cache.is_empty());
    }

    #[test]
    fn test_invalidate_user() {
        let mut cache = ScoreCache::new(300, 100);
        let u = uid();
        let k1 = CacheKey::new(u, uid());
        let k2 = CacheKey::new(u, uid());
        let k3 = CacheKey::new(uid(), uid());
        cache.put(k1, 0.1, 1000);
        cache.put(k2, 0.2, 1000);
        cache.put(k3, 0.3, 1000);
        cache.invalidate_user(u);
        assert_eq!(cache.len(), 1);
    }

    #[test]
    fn test_hit_rate() {
        let mut cache = ScoreCache::new(300, 100);
        let key = CacheKey::new(uid(), uid());
        cache.put(key, 0.5, 1000);
        let _ = cache.get(&key, 1001); // hit
        let _ = cache.get(&CacheKey::new(uid(), uid()), 1001); // miss
        assert!((cache.stats().hit_rate() - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn test_clear_cache() {
        let mut cache = ScoreCache::new(300, 100);
        cache.put(CacheKey::new(uid(), uid()), 0.1, 1000);
        cache.put(CacheKey::new(uid(), uid()), 0.2, 1000);
        cache.clear();
        assert!(cache.is_empty());
        assert_eq!(cache.len(), 0);
    }
}