implement a blake3 content version layer for cache busting

works pretty much the same as our previous `?cache` parameter, but is implemented in a less ad-hoc way
This commit is contained in:
liquidex 2024-11-23 18:29:03 +01:00
parent 32f25ce863
commit 7169e65244
7 changed files with 86 additions and 4 deletions

15
Cargo.lock generated
View file

@ -438,6 +438,20 @@ dependencies = [
"typenum",
]
[[package]]
name = "dashmap"
version = "6.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf"
dependencies = [
"cfg-if",
"crossbeam-utils",
"hashbrown",
"lock_api",
"once_cell",
"parking_lot_core",
]
[[package]]
name = "digest"
version = "0.10.7"
@ -1583,6 +1597,7 @@ dependencies = [
"clap",
"codespan-reporting",
"copy_dir",
"dashmap",
"env_logger",
"git2",
"handlebars",

View file

@ -6,7 +6,7 @@
<style>
@font-face {
font-family: "Determination Sans";
src: url('/static/font/DTM-Sans.otf?cache=b3-4fe96c14');
src: url('/static/font/DTM-Sans.otf?v=b3-4fe96c14');
}
.undertale-save-box {

View file

@ -15,6 +15,7 @@ chrono = "0.4.35"
clap = { version = "4.3.22", features = ["derive"] }
codespan-reporting = "0.11.1"
copy_dir = "0.1.3"
dashmap = "6.1.0"
env_logger = "0.10.0"
git2 = { version = "0.19.0", default-features = false, features = ["vendored-libgit2"] }
handlebars = "4.3.7"

View file

@ -9,7 +9,7 @@ use treehouse::cli::serve::serve;
use treehouse::dirs::Dirs;
use treehouse::generate::{self, Sources};
use treehouse::vfs::asynch::AsyncDir;
use treehouse::vfs::{AnchoredAtExt, DynDir, ToDynDir, VPathBuf};
use treehouse::vfs::{AnchoredAtExt, Blake3ContentVersionCache, DynDir, ToDynDir, VPathBuf};
use treehouse::vfs::{Cd, PhysicalDir};
use treehouse::{
cli::{
@ -42,6 +42,8 @@ fn vfs_sources() -> anyhow::Result<DynDir> {
PhysicalDir::new(PathBuf::from("content")).to_dyn(),
);
let root = Blake3ContentVersionCache::new(root);
Ok(root.to_dyn())
}

View file

@ -52,6 +52,7 @@ use std::{
mod anchored;
pub mod asynch;
mod cd;
mod content_version_cache;
mod edit;
mod empty;
mod file;
@ -62,6 +63,7 @@ mod physical;
pub use anchored::*;
pub use cd::*;
pub use content_version_cache::*;
pub use edit::*;
pub use empty::*;
pub use file::*;

View file

@ -0,0 +1,62 @@
use std::fmt::{self, Debug};
use dashmap::DashMap;
use log::debug;
use super::{Dir, DirEntry, EditPath, VPath, VPathBuf};
pub struct Blake3ContentVersionCache<T> {
inner: T,
cache: DashMap<VPathBuf, Option<String>>,
}
impl<T> Blake3ContentVersionCache<T> {
pub fn new(inner: T) -> Self {
Self {
inner,
cache: DashMap::new(),
}
}
}
impl<T> Dir for Blake3ContentVersionCache<T>
where
T: Dir,
{
fn dir(&self, path: &VPath) -> Vec<DirEntry> {
self.inner.dir(path)
}
fn content(&self, path: &VPath) -> Option<Vec<u8>> {
self.inner.content(path)
}
fn content_version(&self, path: &VPath) -> Option<String> {
self.cache
.entry(path.to_owned())
.or_insert_with(|| {
self.content(path).map(|content| {
let hash = blake3::hash(&content).to_hex();
format!("b3-{}", &hash[0..8])
})
})
.clone()
}
fn anchor(&self, path: &VPath) -> Option<VPathBuf> {
self.inner.anchor(path)
}
fn edit_path(&self, path: &VPath) -> Option<EditPath> {
self.inner.edit_path(path)
}
}
impl<T> fmt::Debug for Blake3ContentVersionCache<T>
where
T: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Blake3ContentVersionCache({:?})", self.inner)
}
}

View file

@ -117,12 +117,12 @@ 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?cache=b3-445487d5");
src: url("../font/Recursive_VF_1.085.woff2?v=b3-445487d5");
}
@font-face {
font-family: "RecVarMono";
src: url("../font/Recursive_VF_1.085.woff2?cache=b3-445487d5");
src: url("../font/Recursive_VF_1.085.woff2?v=b3-445487d5");
font-variation-settings: "MONO" 1;
}