aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-04-26 11:49:21 +0200
committermetamuffin <metamuffin@disroot.org>2025-04-26 11:49:21 +0200
commit13ec27a3ee729e5786fee0e8e41ede4f138b4cfc (patch)
tree35041802b4f021471d63bac432b808f356361d9e
parent2c6f753797b8e223c5982c4790f1be8b08fe63d6 (diff)
downloadjellything-13ec27a3ee729e5786fee0e8e41ede4f138b4cfc.tar
jellything-13ec27a3ee729e5786fee0e8e41ede4f138b4cfc.tar.bz2
jellything-13ec27a3ee729e5786fee0e8e41ede4f138b4cfc.tar.zst
cache subdirs for each kind
-rw-r--r--base/src/cache.rs16
1 files changed, 11 insertions, 5 deletions
diff --git a/base/src/cache.rs b/base/src/cache.rs
index 9c12d3d..02c42c8 100644
--- a/base/src/cache.rs
+++ b/base/src/cache.rs
@@ -59,7 +59,7 @@ pub fn cache_location(kind: &str, key: impl Hash) -> (usize, CachePath) {
d[0] as usize | ((d[1] as usize) << 8) | ((d[2] as usize) << 16) | ((d[3] as usize) << 24);
let fname = base64::engine::general_purpose::URL_SAFE.encode(d);
let fname = &fname[..30]; // 180 bits
- let fname = format!("{}-{}", kind, fname);
+ let fname = format!("{}/{}", kind, fname);
(n, CachePath(fname.into()))
}
@@ -77,11 +77,12 @@ where
Fut: Future<Output = Result<(), anyhow::Error>>,
{
let (bucket, location) = cache_location(kind, key);
+ let loc_abs = location.abs();
// we need a lock even if it exists since somebody might be still in the process of writing.
let _guard = CACHE_GENERATION_LOCKS[bucket % CACHE_GENERATION_BUCKET_COUNT]
.lock()
.await;
- let exists = tokio::fs::try_exists(&location.abs())
+ let exists = tokio::fs::try_exists(&loc_abs)
.await
.context("unable to test for cache file existance")?;
if !exists {
@@ -97,7 +98,10 @@ where
return Err(e);
}
}
- tokio::fs::rename(temp_path, &location.abs())
+ tokio::fs::create_dir_all(loc_abs.parent().unwrap())
+ .await
+ .context("create kind dir")?;
+ tokio::fs::rename(temp_path, &loc_abs)
.await
.context("rename cache")?;
}
@@ -116,6 +120,7 @@ where
Fun: FnMut(std::fs::File) -> Result<(), anyhow::Error>,
{
let (bucket, location) = cache_location(kind, key);
+ let loc_abs = location.abs();
// we need a lock even if it exists since somebody might be still in the process of writing.
let already_within = WITHIN_CACHE_FILE.with(|a| a.swap(true, Ordering::Relaxed));
let _guard = if already_within {
@@ -126,7 +131,7 @@ where
} else {
Some(CACHE_GENERATION_LOCKS[bucket % CACHE_GENERATION_BUCKET_COUNT].blocking_lock())
};
- if !location.abs().exists() {
+ if !loc_abs.exists() {
let temp_path = CONF.cache_path.join(format!("temp-{:x}", random::<u128>()));
let f = std::fs::File::create(&temp_path).context("creating new cache file")?;
match generate(f) {
@@ -137,7 +142,8 @@ where
return Err(e);
}
}
- rename(temp_path, location.abs()).context("rename cache")?;
+ std::fs::create_dir_all(loc_abs.parent().unwrap()).context("create kind dir")?;
+ rename(temp_path, loc_abs).context("rename cache")?;
}
if !already_within {
WITHIN_CACHE_FILE.with(|a| a.swap(false, Ordering::Relaxed));