1
Fork 0

add option to bust cache globally

This commit is contained in:
リキ萌 2025-01-10 14:10:23 +01:00
parent cf29293fd6
commit 62c272ff62
5 changed files with 31 additions and 6 deletions

View file

@ -17,6 +17,13 @@ use crate::{
vfs::{self, Content, Dir, DynDir, ImageSize, VPath, VPathBuf},
};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct VfsConfig {
/// Cache salt string. Passed to `Blake3ContentVersionCache` as a salt for content version hashes.
/// Can be changed to bust cached assets for all clients.
pub cache_salt: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Config {
/// Website root; used when generating links.

View file

@ -8,6 +8,7 @@ use tracing::{error, info_span};
use tracing_subscriber::layer::SubscriberExt as _;
use tracing_subscriber::util::SubscriberInitExt as _;
use treehouse::cli::serve::serve;
use treehouse::config::VfsConfig;
use treehouse::dirs::Dirs;
use treehouse::generate;
use treehouse::sources::Sources;
@ -25,7 +26,7 @@ use treehouse::{
vfs::{BufferedFile, MemDir, VPath},
};
fn vfs_sources() -> anyhow::Result<DynDir> {
fn vfs_sources(config: &VfsConfig) -> anyhow::Result<DynDir> {
let mut root = MemDir::new();
root.add(
@ -51,7 +52,7 @@ fn vfs_sources() -> anyhow::Result<DynDir> {
PhysicalDir::new(PathBuf::from("content")).to_dyn(),
);
let root = Blake3ContentVersionCache::new(root);
let root = Blake3ContentVersionCache::new(config.cache_salt.as_bytes().to_owned(), root);
let root = ImageSizeCache::new(root);
Ok(root.to_dyn())
@ -61,7 +62,12 @@ async fn fallible_main(
args: ProgramArgs,
flush_guard: Option<tracing_chrome::FlushGuard>,
) -> anyhow::Result<()> {
let src = vfs_sources()?;
let vfs_config = toml_edit::de::from_str(
&fs::read_to_string("vfs.toml").context("failed to read vfs.toml")?,
)
.context("failed to deserialize vfs.toml")?;
let src = vfs_sources(&vfs_config)?;
let dirs = Arc::new(Dirs {
root: src.clone(),
content: Cd::new(src.clone(), VPathBuf::new("content")).to_dyn(),

View file

@ -6,13 +6,15 @@ use tracing::{info_span, instrument};
use super::{query, Content, ContentVersion, Dir, Query, VPath, VPathBuf};
pub struct Blake3ContentVersionCache<T> {
salt: Vec<u8>,
inner: T,
cache: DashMap<VPathBuf, ContentVersion>,
}
impl<T> Blake3ContentVersionCache<T> {
pub fn new(inner: T) -> Self {
pub fn new(salt: Vec<u8>, inner: T) -> Self {
Self {
salt,
inner,
cache: DashMap::new(),
}
@ -29,7 +31,10 @@ where
let _span = info_span!("cache_miss").entered();
let version = query::<Content>(&self.inner, path).map(|content| {
let hash = blake3::hash(&content.bytes()).to_hex();
let mut hasher = blake3::Hasher::new();
hasher.update(&self.salt);
hasher.update(&content.bytes());
let hash = hasher.finalize().to_hex();
ContentVersion {
string: format!("b3-{}", &hash[0..8]),
}

View file

@ -126,7 +126,7 @@ body {
Other assets are referenced rarely enough that caching probably isn't gonna make too much of
an impact.
It's unlikely I'll ever update the font anyways, so eh, whatever. */
src: url("../font/Recursive_VF_1.085.woff2?v=b3-445487d5");
src: url("../font/Recursive_VF_1.085.woff2?v=b3-41236e2f");
}
body,

7
vfs.toml Normal file
View file

@ -0,0 +1,7 @@
# treehouse virtual file system settings.
# These are loaded during VFS construction (earlier than treehouse.toml) and can be used to
# influence the virtual file system.
# NOTE: As a convention, this string is just an integer.
# To bust all caches, increment this by 1.
cache_salt = "1"