Memoise the flat route-D render endpoints

This commit is contained in:
gloamxun committed 2026-10-24 23:45:28 +08:00
1 parent 476be05163
commit cca108dc15
5 files changed
+115 -6

No files matched your search

+86 -2
View File
@@ -179,8 +179,15 @@ fn handle_request(
);
}
};
let key = route_d_flat_cache_key(&source);
if let Some(pdf) = route_d_cached_flat_pdf(key) {
return respond_pdf_with_cache(request, pdf, "hit");
}
match route_d::pdf_bytes(&source) {
Ok(pdf) => respond_pdf(request, pdf),
Ok(pdf) => {
store_route_d_flat_pdf(key, &pdf);
respond_pdf_with_cache(request, pdf, "miss")
}
Err(error) => respond(request, 422, "application/json; charset=utf-8", &error),
}
}
@@ -200,8 +207,15 @@ fn handle_request(
);
}
};
let key = route_d_flat_cache_key(&source);
if let Some(json) = route_d_cached_flat_preview(key) {
return respond_json_with_cache(request, &json, "hit");
}
match route_d::preview_json(&source) {
Ok(json) => respond(request, 200, "application/json; charset=utf-8", &json),
Ok(json) => {
store_route_d_flat_preview(key, &json);
respond_json_with_cache(request, &json, "miss")
}
Err(error) => respond(request, 422, "application/json; charset=utf-8", &error),
}
}
@@ -809,6 +823,76 @@ fn store_route_d_pdf(key: (String, u64), pdf: &[u8]) {
}
}
fn route_d_flat_cache_key(source: &str) -> u64 {
let mut hasher = DefaultHasher::new();
source.hash(&mut hasher);
hasher.finish()
}
fn route_d_flat_preview_cache() -> &'static Mutex<Vec<(u64, String)>> {
static CACHE: OnceLock<Mutex<Vec<(u64, String)>>> = OnceLock::new();
CACHE.get_or_init(|| Mutex::new(Vec::new()))
}
fn route_d_cached_flat_preview(key: u64) -> Option<String> {
let cache = route_d_flat_preview_cache().lock().expect("preview cache");
cache
.iter()
.find(|(cached, _)| *cached == key)
.map(|(_, json)| json.clone())
}
fn store_route_d_flat_preview(key: u64, json: &str) {
const MAX_PREVIEWS: usize = 32;
let mut cache = route_d_flat_preview_cache().lock().expect("preview cache");
if let Some(position) = cache.iter().position(|(cached, _)| *cached == key) {
cache.remove(position);
}
cache.push((key, json.to_string()));
while cache.len() > MAX_PREVIEWS {
cache.remove(0);
}
}
fn route_d_flat_pdf_cache() -> &'static Mutex<Vec<(u64, Vec<u8>)>> {
static CACHE: OnceLock<Mutex<Vec<(u64, Vec<u8>)>>> = OnceLock::new();
CACHE.get_or_init(|| Mutex::new(Vec::new()))
}
fn route_d_cached_flat_pdf(key: u64) -> Option<Vec<u8>> {
let cache = route_d_flat_pdf_cache().lock().expect("pdf cache");
cache
.iter()
.find(|(cached, _)| *cached == key)
.map(|(_, pdf)| pdf.clone())
}
fn store_route_d_flat_pdf(key: u64, pdf: &[u8]) {
const MAX_PDFS: usize = 8;
let mut cache = route_d_flat_pdf_cache().lock().expect("pdf cache");
if let Some(position) = cache.iter().position(|(cached, _)| *cached == key) {
cache.remove(position);
}
cache.push((key, pdf.to_vec()));
while cache.len() > MAX_PDFS {
cache.remove(0);
}
}
fn respond_json_with_cache(request: Request, json: &str, cache_state: &str) -> Result<(), String> {
let content_type = Header::from_bytes("Content-Type", "application/json; charset=utf-8")
.map_err(|_| "invalid JSON response header".to_string())?;
let cache = Header::from_bytes("X-Texrra-Cache", cache_state)
.map_err(|_| "invalid cache response header".to_string())?;
request
.respond(
Response::from_string(json)
.with_header(content_type)
.with_header(cache),
)
.map_err(|error| format!("could not send JSON response: {error}"))
}
fn route_d_cache_key(
id: &str,
source: &str,