flux_common/
cache.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::{fs::File, path::PathBuf};

use flux_config as config;
use rustc_hash::FxHashMap;

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct QueryVal<R> {
    constr_hash: u64,
    result: R,
}

pub struct QueryCache<R> {
    entries: FxHashMap<String, QueryVal<R>>,
}

impl<R> Default for QueryCache<R> {
    fn default() -> Self {
        Self::new()
    }
}

impl<R> QueryCache<R> {
    pub fn new() -> Self {
        QueryCache { entries: FxHashMap::default() }
    }

    pub fn insert(&mut self, key: String, constr_hash: u64, result: R) {
        let val = QueryVal { constr_hash, result };
        self.entries.insert(key, val);
    }

    pub fn lookup(&self, key: &String, constr_hash: u64) -> Option<&R> {
        let val = self.entries.get(key)?;
        if val.constr_hash == constr_hash {
            Some(&val.result)
        } else {
            None
        }
    }

    fn path() -> Result<PathBuf, std::io::Error> {
        if config::is_cache_enabled() {
            let path = config::cache_path();
            if let Some(parent) = path.parent() {
                std::fs::create_dir_all(parent)?;
                return Ok(path);
            }
        }
        Err(Self::no_cache_err())
    }

    fn no_cache_err() -> std::io::Error {
        std::io::Error::new(std::io::ErrorKind::Other, "cache not enabled")
    }
}

impl<R: std::fmt::Debug + serde::Serialize + serde::de::DeserializeOwned> QueryCache<R> {
    pub fn save(&self) -> Result<(), std::io::Error> {
        let path = Self::path()?;
        let mut file = File::create(path).unwrap();
        serde_json::to_writer(&mut file, &self.entries).unwrap();
        Ok(())
    }

    pub fn load() -> Self {
        let path = Self::path();
        if let Ok(path) = path {
            if let Ok(file) = File::open(path) {
                let entries = serde_json::from_reader(file);
                if let Ok(entries) = entries {
                    return QueryCache { entries };
                }
            }
        }
        Self::default()
    }
}