rbdc/common/
statement_cache.rs

1use lru::LruCache;
2use std::num::NonZeroUsize;
3
4/// A cache for prepared statements. When full, the least recently used
5/// statement gets removed.
6#[derive(Debug)]
7pub struct StatementCache<T> {
8    inner: LruCache<String, T>,
9}
10
11impl<T> StatementCache<T> {
12    /// Create a new cache with the given capacity.
13    pub fn new(capacity: usize) -> Self {
14        Self {
15            inner: LruCache::new(NonZeroUsize::new(capacity).unwrap()),
16        }
17    }
18
19    /// Returns a mutable reference to the value corresponding to the given key
20    /// in the cache, if any.
21    pub fn get_mut(&mut self, k: &str) -> Option<&mut T> {
22        self.inner.get_mut(k)
23    }
24
25    /// Inserts a new statement to the cache, returning the least recently used
26    /// statement id if the cache is full, or if inserting with an existing key,
27    /// the replaced existing statement.
28    pub fn insert(&mut self, k: &str, v: T) -> Option<T> {
29        let mut lru_item = None;
30
31        if self.capacity() == self.len() && !self.contains_key(k) {
32            lru_item = self.remove_lru();
33        } else if self.contains_key(k) {
34            let entry = self.inner.pop_entry(k);
35            match entry {
36                None => {}
37                Some(v) => {
38                    lru_item = Some(v.1);
39                }
40            }
41        }
42
43        self.inner.put(k.into(), v);
44
45        lru_item
46    }
47
48    /// The number of statements in the cache.
49    pub fn len(&self) -> usize {
50        self.inner.len()
51    }
52
53    /// Removes the least recently used item from the cache.
54    pub fn remove_lru(&mut self) -> Option<T> {
55        self.inner.pop_lru().map(|v| v.1)
56    }
57
58    /// Clear all cached statements from the cache.
59    pub fn clear(&mut self) {
60        self.inner.clear();
61    }
62
63    /// True if cache has a value for the given key.
64    pub fn contains_key(&mut self, k: &str) -> bool {
65        self.inner.contains(k)
66    }
67
68    /// Returns the maximum number of statements the cache can hold.
69    pub fn capacity(&self) -> usize {
70        self.inner.cap().get()
71    }
72
73    /// Returns true if the cache capacity is more than 0.
74    #[allow(dead_code)] // Only used for some `cfg`s
75    pub fn is_enabled(&self) -> bool {
76        self.capacity() > 0
77    }
78}