diff options
-rw-r--r-- | base/src/cache.rs | 16 |
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)); |