aboutsummaryrefslogtreecommitdiff
path: root/cache/src/backends
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-12-08 19:53:12 +0100
committermetamuffin <metamuffin@disroot.org>2025-12-08 19:53:12 +0100
commit6edf0fd93abf7e58b4c0974e3d3e54bcf8517946 (patch)
tree32577db9d987897d4037ba9af0084b95b55e145c /cache/src/backends
parente4584a8135584e6591bac7d5397cf227cf3cff92 (diff)
downloadjellything-6edf0fd93abf7e58b4c0974e3d3e54bcf8517946.tar
jellything-6edf0fd93abf7e58b4c0974e3d3e54bcf8517946.tar.bz2
jellything-6edf0fd93abf7e58b4c0974e3d3e54bcf8517946.tar.zst
human-readable cache keys
Diffstat (limited to 'cache/src/backends')
-rw-r--r--cache/src/backends/filesystem.rs20
-rw-r--r--cache/src/backends/mod.rs5
2 files changed, 10 insertions, 15 deletions
diff --git a/cache/src/backends/filesystem.rs b/cache/src/backends/filesystem.rs
index 39fb7a2..9a9db9c 100644
--- a/cache/src/backends/filesystem.rs
+++ b/cache/src/backends/filesystem.rs
@@ -4,12 +4,11 @@
Copyright (C) 2025 metamuffin <metamuffin.org>
*/
-use crate::{CacheKey, Config, backends::CacheStorage};
+use crate::{Config, backends::CacheStorage};
use anyhow::Result;
-use base64::Engine;
use rand::random;
use std::{
- fs::{File, rename},
+ fs::{File, create_dir_all, rename},
io::{ErrorKind, Read, Write},
path::PathBuf,
};
@@ -20,25 +19,22 @@ impl Filesystem {
pub fn new(config: &Config) -> Self {
Self(config.path.clone())
}
- fn path(&self, key: CacheKey) -> PathBuf {
- let filename = base64::engine::general_purpose::URL_SAFE.encode(key.0);
- let filename = &filename[..30]; // 180 bits
- self.0.join(filename)
- }
fn temp_path(&self) -> PathBuf {
self.0.join(format!("temp-{:016x}", random::<u128>()))
}
}
impl CacheStorage for Filesystem {
- fn store(&self, key: CacheKey, value: &[u8]) -> Result<()> {
+ fn store(&self, key: String, value: &[u8]) -> Result<()> {
let temp = self.temp_path();
+ let out = self.0.join(&key);
+ create_dir_all(out.parent().unwrap())?;
File::create(&temp)?.write_all(value)?;
- rename(temp, self.path(key))?;
+ rename(temp, out)?;
Ok(())
}
- fn read(&self, key: CacheKey) -> Result<Option<Vec<u8>>> {
- match File::open(self.path(key)) {
+ fn read(&self, key: &str) -> Result<Option<Vec<u8>>> {
+ match File::open(self.0.join(key)) {
Ok(mut f) => {
let mut data = Vec::new();
f.read_to_end(&mut data)?;
diff --git a/cache/src/backends/mod.rs b/cache/src/backends/mod.rs
index 370c5ab..6b7dac3 100644
--- a/cache/src/backends/mod.rs
+++ b/cache/src/backends/mod.rs
@@ -5,10 +5,9 @@
*/
pub mod filesystem;
-use crate::CacheKey;
use anyhow::Result;
pub(crate) trait CacheStorage: Send + Sync + 'static {
- fn store(&self, key: CacheKey, value: &[u8]) -> Result<()>;
- fn read(&self, key: CacheKey) -> Result<Option<Vec<u8>>>;
+ fn store(&self, key: String, value: &[u8]) -> Result<()>;
+ fn read(&self, key: &str) -> Result<Option<Vec<u8>>>;
}