Cache route-D project PDF exports
This commit is contained in:
1 parent
6d5a3eb686
commit
4b0e4eba0c
5 files changed
+102
-5
No files matched your search
+50
-1
@@ -395,6 +395,23 @@ fn respond_with_cache(
|
||||
.map_err(|error| format!("could not send response: {error}"))
|
||||
}
|
||||
|
||||
fn respond_pdf_with_cache(request: Request, pdf: Vec<u8>, cache_state: &str) -> Result<(), String> {
|
||||
let content_type = Header::from_bytes("Content-Type", "application/pdf")
|
||||
.map_err(|_| "invalid PDF response header".to_string())?;
|
||||
let disposition = Header::from_bytes("Content-Disposition", "inline; filename=preview.pdf")
|
||||
.map_err(|_| "invalid PDF 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_data(pdf)
|
||||
.with_header(content_type)
|
||||
.with_header(disposition)
|
||||
.with_header(cache),
|
||||
)
|
||||
.map_err(|error| format!("could not send PDF response: {error}"))
|
||||
}
|
||||
|
||||
fn respond_pdf(request: Request, pdf: Vec<u8>) -> Result<(), String> {
|
||||
let content_type = Header::from_bytes("Content-Type", "application/pdf")
|
||||
.map_err(|_| "invalid PDF response header".to_string())?;
|
||||
@@ -694,6 +711,31 @@ fn load_project_class(
|
||||
Some(sanitize_package(&apply_class_options(&expanded, options)))
|
||||
}
|
||||
|
||||
fn route_d_pdf_cache() -> &'static Mutex<Vec<((String, u64), Vec<u8>)>> {
|
||||
static CACHE: OnceLock<Mutex<Vec<((String, u64), Vec<u8>)>>> = OnceLock::new();
|
||||
CACHE.get_or_init(|| Mutex::new(Vec::new()))
|
||||
}
|
||||
|
||||
fn route_d_cached_pdf(key: &(String, u64)) -> Option<Vec<u8>> {
|
||||
let cache = route_d_pdf_cache().lock().expect("pdf cache");
|
||||
cache
|
||||
.iter()
|
||||
.find(|(cached, _)| cached == key)
|
||||
.map(|(_, pdf)| pdf.clone())
|
||||
}
|
||||
|
||||
fn store_route_d_pdf(key: (String, u64), pdf: &[u8]) {
|
||||
const MAX_PDFS: usize = 8;
|
||||
let mut cache = route_d_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 route_d_cache_key(id: &str, source: &str) -> (String, u64) {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
source.hash(&mut hasher);
|
||||
@@ -930,8 +972,15 @@ fn handle_project_request(
|
||||
};
|
||||
if uses_route_d(url) {
|
||||
let expanded = expand_project_source(&source, &store, id);
|
||||
let key = route_d_cache_key(id, &expanded);
|
||||
if let Some(pdf) = route_d_cached_pdf(&key) {
|
||||
return respond_pdf_with_cache(request, pdf, "hit");
|
||||
}
|
||||
return match route_d::pdf_bytes(&expanded) {
|
||||
Ok(pdf) => respond_pdf(request, pdf),
|
||||
Ok(pdf) => {
|
||||
store_route_d_pdf(key, &pdf);
|
||||
respond_pdf_with_cache(request, pdf, "miss")
|
||||
}
|
||||
Err(error) => respond(request, 422, "text/plain; charset=utf-8", &error),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user