From 65999ccb8675ea978b00b1a4171f4729c04bb1b4 Mon Sep 17 00:00:00 2001 From: link2xt Date: Wed, 11 Dec 2024 16:22:21 +0000 Subject: [PATCH] Expire images after 1 day --- src/net/http.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/net/http.rs b/src/net/http.rs index 7727e8a598..570e6ec9dd 100644 --- a/src/net/http.rs +++ b/src/net/http.rs @@ -95,11 +95,14 @@ where } /// Converts the URL to expiration timestamp. -fn http_url_cache_expires(url: &str) -> i64 { +fn http_url_cache_expires(url: &str, mimetype: Option<&str>) -> i64 { let now = time(); if url.ends_with(".xdc") { // WebXDCs expire in 5 weeks. now + 3600 * 24 * 35 + } else if mimetype.is_some_and(|s| s.starts_with("image/")) { + // Cache images for 1 day. + now + 3600 * 24 } else { // Cache everything else for 1 hour. now + 3600 @@ -122,7 +125,7 @@ async fn http_cache_put(context: &Context, url: &str, response: &Response) -> Re VALUES (?, ?, ?, ?, ?)", ( url, - http_url_cache_expires(url), + http_url_cache_expires(url, response.mimetype.as_deref()), blob.as_name(), response.mimetype.as_deref().unwrap_or_default(), response.encoding.as_deref().unwrap_or_default(), @@ -157,6 +160,7 @@ async fn http_cache_get(context: &Context, url: &str) -> Result let blob_abs_path = blob_object.to_abs_path(); let blob = fs::read(blob_abs_path).await?; + let expires = http_url_cache_expires(url, mimetype.as_deref()); let response = Response { blob, mimetype, @@ -168,7 +172,7 @@ async fn http_cache_get(context: &Context, url: &str) -> Result .sql .execute( "UPDATE http_cache SET expires=? WHERE url=?", - (http_url_cache_expires(url), url), + (expires, url), ) .await?;