Skip to main content

verso/reader/
cache.rs

1use lru::LruCache;
2use std::num::NonZeroUsize;
3
4use super::page::Page;
5
6type Key = (u32, u16, String); // (spine_idx, column_width, theme)
7
8pub struct PageCache {
9    inner: LruCache<Key, Vec<Page>>,
10}
11
12impl PageCache {
13    pub fn new(cap: usize) -> Self {
14        Self {
15            inner: LruCache::new(NonZeroUsize::new(cap.max(1)).unwrap()),
16        }
17    }
18    pub fn get(&mut self, spine_idx: u32, width: u16, theme: &str) -> Option<&Vec<Page>> {
19        self.inner.get(&(spine_idx, width, theme.to_string()))
20    }
21    pub fn put(&mut self, spine_idx: u32, width: u16, theme: &str, pages: Vec<Page>) {
22        self.inner.put((spine_idx, width, theme.to_string()), pages);
23    }
24}