television_previewers/previewers/
cache.rs

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
use rustc_hash::FxHashMap;
use std::sync::Arc;

use crate::previewers::Preview;
use television_utils::cache::RingSet;
use tracing::debug;

/// Default size of the preview cache: 100 entries.
///
/// This does seem kind of arbitrary for now, will need to play around with it.
/// At the moment, files over 4 MB are not previewed, so the cache size
/// should never exceed 400 MB.
const DEFAULT_PREVIEW_CACHE_SIZE: usize = 100;

/// A cache for previews.
/// The cache is implemented as an LRU cache with a fixed size.
#[derive(Debug)]
pub struct PreviewCache {
    entries: FxHashMap<String, Arc<Preview>>,
    ring_set: RingSet<String>,
}

impl PreviewCache {
    /// Create a new preview cache with the given capacity.
    pub fn new(capacity: usize) -> Self {
        PreviewCache {
            entries: FxHashMap::default(),
            ring_set: RingSet::with_capacity(capacity),
        }
    }

    pub fn get(&self, key: &str) -> Option<Arc<Preview>> {
        self.entries.get(key).cloned()
    }

    /// Insert a new preview into the cache.
    /// If the cache is full, the oldest entry will be removed.
    /// If the key is already in the cache, the preview will be updated.
    pub fn insert(&mut self, key: String, preview: &Arc<Preview>) {
        debug!("Inserting preview into cache: {}", key);
        self.entries.insert(key.clone(), Arc::clone(preview));
        if let Some(oldest_key) = self.ring_set.push(key) {
            debug!("Cache full, removing oldest entry: {}", oldest_key);
            self.entries.remove(&oldest_key);
        }
    }

    /// Get the preview for the given key, or insert a new preview if it doesn't exist.
    #[allow(dead_code)]
    pub fn get_or_insert<F>(&mut self, key: String, f: F) -> Arc<Preview>
    where
        F: FnOnce() -> Preview,
    {
        if let Some(preview) = self.get(&key) {
            preview
        } else {
            let preview = Arc::new(f());
            self.insert(key, &preview);
            preview
        }
    }
}

impl Default for PreviewCache {
    fn default() -> Self {
        PreviewCache::new(DEFAULT_PREVIEW_CACHE_SIZE)
    }
}