Skip to main content

greentic_flow/cache/
config.rs

1use std::path::PathBuf;
2
3#[derive(Clone, Debug)]
4pub struct CacheConfig {
5    pub root: PathBuf,
6    pub disk_enabled: bool,
7    pub memory_enabled: bool,
8    pub disk_max_bytes: Option<u64>,
9    pub memory_max_bytes: u64,
10    pub lfu_protect_hits: u64,
11}
12
13impl CacheConfig {
14    pub fn disk_root(&self, engine_profile_id: &str) -> PathBuf {
15        self.root.join("v1").join(engine_profile_id)
16    }
17}
18
19impl Default for CacheConfig {
20    fn default() -> Self {
21        let root = std::env::var_os("GREENTIC_CACHE_DIR")
22            .map(PathBuf::from)
23            .unwrap_or_else(|| PathBuf::from(".greentic/cache/components"));
24        let cache_disabled =
25            env_flag_set("GREENTIC_NO_CACHE") || env_flag_set("GREENTIC_DISABLE_CACHE");
26        Self {
27            root,
28            disk_enabled: !cache_disabled,
29            memory_enabled: !cache_disabled,
30            disk_max_bytes: Some(5 * 1024 * 1024 * 1024),
31            memory_max_bytes: 512 * 1024 * 1024,
32            lfu_protect_hits: 3,
33        }
34    }
35}
36
37fn env_flag_set(key: &str) -> bool {
38    std::env::var(key)
39        .ok()
40        .map(|value| matches!(value.as_str(), "1" | "true" | "TRUE" | "yes" | "YES"))
41        .unwrap_or(false)
42}