Skip to main content

next_rust_cache/
file.rs

1use std::collections::BTreeMap;
2use std::path::{Path, PathBuf};
3use std::time::{Duration, SystemTime, UNIX_EPOCH};
4
5use serde::{Deserialize, Serialize};
6
7use crate::{BoxFuture, CacheEntry, CacheError, CacheKey, CacheStore};
8
9/// Persistent store: one `<hash>.bin` (value) and `<hash>.json` (metadata)
10/// pair per key. Writes are atomic (write to a temp file, then rename).
11///
12/// Tag invalidation scans the metadata files, so it is O(entries); this is
13/// adequate for single-node deployments. Use a networked store for fleets.
14pub struct FileStore {
15    dir: PathBuf,
16}
17
18#[derive(Serialize, Deserialize)]
19struct Meta {
20    key: String,
21    created_ms: u64,
22    revalidate_ms: Option<u64>,
23    tags: Vec<String>,
24    meta: BTreeMap<String, String>,
25}
26
27impl FileStore {
28    pub fn new(dir: impl AsRef<Path>) -> Self {
29        FileStore { dir: dir.as_ref().to_path_buf() }
30    }
31
32    fn paths(&self, key: &CacheKey) -> (PathBuf, PathBuf) {
33        let h = next_rust_assets::content_hash(key.0.as_bytes());
34        (self.dir.join(format!("{h}.bin")), self.dir.join(format!("{h}.json")))
35    }
36}
37
38async fn write_atomic(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
39    let tmp = path.with_extension(format!("tmp{}", std::process::id()));
40    tokio::fs::write(&tmp, bytes).await?;
41    tokio::fs::rename(&tmp, path).await
42}
43
44fn not_found(e: &std::io::Error) -> bool {
45    e.kind() == std::io::ErrorKind::NotFound
46}
47
48impl CacheStore for FileStore {
49    fn get<'a>(&'a self, key: &'a CacheKey) -> BoxFuture<'a, Result<Option<CacheEntry>, CacheError>> {
50        Box::pin(async move {
51            let (bin, json) = self.paths(key);
52            let meta = match tokio::fs::read(&json).await {
53                Ok(m) => m,
54                Err(e) if not_found(&e) => return Ok(None),
55                Err(e) => return Err(e.into()),
56            };
57            let meta: Meta = serde_json::from_slice(&meta).map_err(|e| CacheError::Serde(e.to_string()))?;
58            if meta.key != key.0 {
59                return Ok(None); // hash collision
60            }
61            let value = match tokio::fs::read(&bin).await {
62                Ok(v) => v,
63                Err(e) if not_found(&e) => return Ok(None),
64                Err(e) => return Err(e.into()),
65            };
66            Ok(Some(CacheEntry {
67                value: value.into(),
68                created: UNIX_EPOCH + Duration::from_millis(meta.created_ms),
69                revalidate: meta.revalidate_ms.map(Duration::from_millis),
70                tags: meta.tags,
71                meta: meta.meta,
72            }))
73        })
74    }
75
76    fn set(&self, key: CacheKey, entry: CacheEntry) -> BoxFuture<'_, Result<(), CacheError>> {
77        Box::pin(async move {
78            tokio::fs::create_dir_all(&self.dir).await?;
79            let (bin, json) = self.paths(&key);
80            let meta = Meta {
81                key: key.0,
82                created_ms: entry.created.duration_since(UNIX_EPOCH).unwrap_or_default().as_millis() as u64,
83                revalidate_ms: entry.revalidate.map(|d| d.as_millis() as u64),
84                tags: entry.tags,
85                meta: entry.meta,
86            };
87            write_atomic(&bin, &entry.value).await?;
88            let json_bytes = serde_json::to_vec(&meta).map_err(|e| CacheError::Serde(e.to_string()))?;
89            write_atomic(&json, &json_bytes).await?;
90            Ok(())
91        })
92    }
93
94    fn delete<'a>(&'a self, key: &'a CacheKey) -> BoxFuture<'a, Result<bool, CacheError>> {
95        Box::pin(async move {
96            let (bin, json) = self.paths(key);
97            let existed = match tokio::fs::remove_file(&json).await {
98                Ok(()) => true,
99                Err(e) if not_found(&e) => false,
100                Err(e) => return Err(e.into()),
101            };
102            let _ = tokio::fs::remove_file(&bin).await;
103            Ok(existed)
104        })
105    }
106
107    fn delete_tag<'a>(&'a self, tag: &'a str) -> BoxFuture<'a, Result<usize, CacheError>> {
108        Box::pin(async move {
109            let mut rd = match tokio::fs::read_dir(&self.dir).await {
110                Ok(rd) => rd,
111                Err(e) if not_found(&e) => return Ok(0),
112                Err(e) => return Err(e.into()),
113            };
114            let mut removed = 0;
115            while let Some(entry) = rd.next_entry().await? {
116                let path = entry.path();
117                if path.extension().is_none_or(|e| e != "json") {
118                    continue;
119                }
120                let Ok(bytes) = tokio::fs::read(&path).await else { continue };
121                let Ok(meta) = serde_json::from_slice::<Meta>(&bytes) else { continue };
122                if meta.tags.iter().any(|t| t == tag) && self.delete(&CacheKey(meta.key)).await? {
123                    removed += 1;
124                }
125            }
126            Ok(removed)
127        })
128    }
129
130    fn clear(&self) -> BoxFuture<'_, Result<(), CacheError>> {
131        Box::pin(async move {
132            match tokio::fs::remove_dir_all(&self.dir).await {
133                Ok(()) => Ok(()),
134                Err(e) if not_found(&e) => Ok(()),
135                Err(e) => Err(e.into()),
136            }
137        })
138    }
139}
140
141#[allow(dead_code)]
142fn now_ms() -> u64 {
143    SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_millis() as u64
144}