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    /// Look up a result by key only (no hash validation).
38    pub fn lookup_by_key(&self, key: &str) -> Option<&R> {
39        self.entries.get(key).map(|e| &e.result)
40    }
41
42    /// Mutate a cached result by key only (no hash validation).
43    pub fn update_result_by_key(&mut self, key: &str, f: impl FnOnce(&mut R)) {
44        if let Some(entry) = self.entries.get_mut(key) {
45            f(&mut entry.result);
46        }
47    }
48
49    fn path() -> Result<&'static Path, std::io::Error> {
50        if let Some(path) = config::cache_path()
51            && let Some(parent) = path.parent()
52        {
53            std::fs::create_dir_all(parent)?;
54            return Ok(path);
55        }
56
57        Err(Self::no_cache_err())
58    }
59
60    fn no_cache_err() -> std::io::Error {
61        std::io::Error::other("cache not enabled")
62    }
63}
64
65impl<R: std::fmt::Debug + serde::Serialize + serde::de::DeserializeOwned> QueryCache<R> {
66    pub fn save(&self) -> Result<(), std::io::Error> {
67        let path = Self::path()?;
68        let mut file = File::create(path).unwrap();
69        serde_json::to_writer(&mut file, &self.entries).unwrap();
70        Ok(())
71    }
72
73    pub fn load() -> Self {
74        let path = Self::path();
75        if let Ok(path) = path
76            && let Ok(file) = File::open(path)
77        {
78            let entries = serde_json::from_reader(file);
79            if let Ok(entries) = entries {
80                return QueryCache { entries };
81            }
82        }
83        Self::default()
84    }
85}