flux_common/
cache.rs

1use std::{fs::File, path::Path};
2
3use flux_config as config;
4use rustc_hash::FxHashMap;
5
6#[derive(Debug, serde::Serialize, serde::Deserialize)]
7struct QueryVal<R> {
8    constr_hash: u64,
9    result: R,
10}
11
12pub struct QueryCache<R> {
13    entries: FxHashMap<String, QueryVal<R>>,
14}
15
16impl<R> Default for QueryCache<R> {
17    fn default() -> Self {
18        Self::new()
19    }
20}
21
22impl<R> QueryCache<R> {
23    pub fn new() -> Self {
24        QueryCache { entries: FxHashMap::default() }
25    }
26
27    pub fn insert(&mut self, key: String, constr_hash: u64, result: R) {
28        let val = QueryVal { constr_hash, result };
29        self.entries.insert(key, val);
30    }
31
32    pub fn lookup(&self, key: &String, constr_hash: u64) -> Option<&R> {
33        let val = self.entries.get(key)?;
34        if val.constr_hash == constr_hash { Some(&val.result) } else { None }
35    }
36
37    fn path() -> Result<&'static Path, std::io::Error> {
38        if let Some(path) = config::cache_path() {
39            if let Some(parent) = path.parent() {
40                std::fs::create_dir_all(parent)?;
41                return Ok(path);
42            }
43        }
44        Err(Self::no_cache_err())
45    }
46
47    fn no_cache_err() -> std::io::Error {
48        std::io::Error::new(std::io::ErrorKind::Other, "cache not enabled")
49    }
50}
51
52impl<R: std::fmt::Debug + serde::Serialize + serde::de::DeserializeOwned> QueryCache<R> {
53    pub fn save(&self) -> Result<(), std::io::Error> {
54        let path = Self::path()?;
55        let mut file = File::create(path).unwrap();
56        serde_json::to_writer(&mut file, &self.entries).unwrap();
57        Ok(())
58    }
59
60    pub fn load() -> Self {
61        let path = Self::path();
62        if let Ok(path) = path {
63            if let Ok(file) = File::open(path) {
64                let entries = serde_json::from_reader(file);
65                if let Ok(entries) = entries {
66                    return QueryCache { entries };
67                }
68            }
69        }
70        Self::default()
71    }
72}