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            && let Some(parent) = path.parent()
40        {
41            std::fs::create_dir_all(parent)?;
42            return Ok(path);
43        }
44
45        Err(Self::no_cache_err())
46    }
47
48    fn no_cache_err() -> std::io::Error {
49        std::io::Error::other("cache not enabled")
50    }
51}
52
53impl<R: std::fmt::Debug + serde::Serialize + serde::de::DeserializeOwned> QueryCache<R> {
54    pub fn save(&self) -> Result<(), std::io::Error> {
55        let path = Self::path()?;
56        let mut file = File::create(path).unwrap();
57        serde_json::to_writer(&mut file, &self.entries).unwrap();
58        Ok(())
59    }
60
61    pub fn load() -> Self {
62        let path = Self::path();
63        if let Ok(path) = path
64            && let Ok(file) = File::open(path)
65        {
66            let entries = serde_json::from_reader(file);
67            if let Ok(entries) = entries {
68                return QueryCache { entries };
69            }
70        }
71        Self::default()
72    }
73}