Skip to content

Commit

Permalink
Expire images after 1 day
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Dec 11, 2024
1 parent 6cba9ab commit 65999cc
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/net/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
Expand Down Expand Up @@ -157,6 +160,7 @@ async fn http_cache_get(context: &Context, url: &str) -> Result<Option<Response>
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,
Expand All @@ -168,7 +172,7 @@ async fn http_cache_get(context: &Context, url: &str) -> Result<Option<Response>
.sql
.execute(
"UPDATE http_cache SET expires=? WHERE url=?",
(http_url_cache_expires(url), url),
(expires, url),
)
.await?;

Expand Down

0 comments on commit 65999cc

Please sign in to comment.