Cache route-D project previews by expanded source

This commit is contained in:
gloamxun committed 2026-10-24 22:47:19 +08:00
1 parent 874a327dca
commit 0651e15111
5 files changed
+160 -5

No files matched your search

+109 -1
View File
@@ -1,9 +1,12 @@
use std::{
collections::hash_map::DefaultHasher,
env,
fmt::Write as _,
fs,
hash::{Hash, Hasher},
path::{Path, PathBuf},
process,
sync::{Mutex, OnceLock},
};
use tiny_http::{Header, Method, Request, Response, Server, StatusCode};
@@ -555,6 +558,69 @@ fn sanitize_package(source: &str) -> String {
.join("\n")
}
fn route_d_cache_key(id: &str, source: &str) -> (String, u64) {
let mut hasher = DefaultHasher::new();
source.hash(&mut hasher);
(id.to_string(), hasher.finish())
}
fn route_d_cache() -> &'static Mutex<Vec<((String, u64), String)>> {
static CACHE: OnceLock<Mutex<Vec<((String, u64), String)>>> = OnceLock::new();
CACHE.get_or_init(|| Mutex::new(Vec::new()))
}
fn route_d_cached_preview(key: &(String, u64)) -> Option<String> {
let cache = route_d_cache().lock().ok()?;
cache
.iter()
.find(|(cached, _)| cached == key)
.map(|(_, json)| json.clone())
}
fn replace_number_field(json: &str, field: &str, value: u64) -> String {
let Some(start) = json.find(field) else {
return json.to_string();
};
let Some(colon) = json[start..].find(':') else {
return json.to_string();
};
let value_start = start + colon + 1;
let rest = &json[value_start..];
let Some(end) = rest.find([',', '}']) else {
return json.to_string();
};
format!("{}{value}{}", &json[..value_start], &rest[end..])
}
fn replace_array_field(json: &str, field: &str) -> String {
let Some(start) = json.find(field) else {
return json.to_string();
};
let Some(open_rel) = json[start..].find('[') else {
return json.to_string();
};
let open = start + open_rel;
let Some(close_rel) = json[open..].find(']') else {
return json.to_string();
};
let close = open + close_rel;
format!("{}[]{}", &json[..open], &json[close + 1..])
}
fn preview_without_changes(json: &str) -> String {
let json = replace_number_field(json, "\"recomputedPages\"", 0);
replace_array_field(&json, "\"changedPages\"")
}
fn store_route_d_preview(key: (String, u64), json: String) {
if let Ok(mut cache) = route_d_cache().lock() {
cache.push((key, json));
if cache.len() > 32 {
cache.remove(0);
}
}
}
fn expand_project_source(source: &str, store: &projects::ProjectStore, id: &str) -> String {
let expanded = expand_project_inputs(source, store, id);
let expanded = expand_project_packages(&expanded, store, id);
@@ -664,8 +730,50 @@ fn handle_project_request(
};
if query_param(url, "engine").as_deref() == Some("route-d") {
let expanded = expand_project_source(&source, &store, id);
let key = route_d_cache_key(id, &expanded);
if let Some(json) = route_d_cached_preview(&key) {
return respond_json(request, 200, &preview_without_changes(&json));
}
fn replace_number_field(json: &str, field: &str, value: u64) -> String {
let Some(start) = json.find(field) else {
return json.to_string();
};
let Some(colon) = json[start..].find(':') else {
return json.to_string();
};
let value_start = start + colon + 1;
let rest = &json[value_start..];
let Some(end) = rest.find([',', '}']) else {
return json.to_string();
};
format!("{}{value}{}", &json[..value_start], &rest[end..])
}
fn replace_array_field(json: &str, field: &str) -> String {
let Some(start) = json.find(field) else {
return json.to_string();
};
let Some(open_rel) = json[start..].find('[') else {
return json.to_string();
};
let open = start + open_rel;
let Some(close_rel) = json[open..].find(']') else {
return json.to_string();
};
let close = open + close_rel;
format!("{}[]{}", &json[..open], &json[close + 1..])
}
fn preview_without_changes(json: &str) -> String {
let json = replace_number_field(json, "\"recomputedPages\"", 0);
replace_array_field(&json, "\"changedPages\"")
}
return match route_d::project_preview_json(id, &expanded) {
Ok(json) => respond_json(request, 200, &json),
Ok(json) => {
store_route_d_preview(key, json.clone());
respond_json(request, 200, &json)
}
Err(error) => respond_api_error(request, 422, &error),
};
}