Skip to content

Commit bd23777

Browse files
committed
cache the homepage for a minute
1 parent d7bfe85 commit bd23777

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/web/cache.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use http::{header::CACHE_CONTROL, HeaderValue};
66
use std::sync::Arc;
77

88
pub static NO_CACHING: HeaderValue = HeaderValue::from_static("max-age=0");
9+
pub static SHORT: HeaderValue = HeaderValue::from_static("max-age=60");
910

1011
pub static NO_STORE_MUST_REVALIDATE: HeaderValue =
1112
HeaderValue::from_static("no-cache, no-store, must-revalidate, max-age=0");
@@ -24,7 +25,12 @@ pub enum CachePolicy {
2425
/// * enforce revalidation
2526
/// * never store
2627
NoStoreMustRevalidate,
27-
/// cache forever in browser & CDN.
28+
/// cache for a short time in the browser & CDN.
29+
/// right now: one minute.
30+
/// Can be used when the content can be a _little_ outdated,
31+
/// while protecting agains spikes in traffic.
32+
ShortInCdnAndBrowser,
33+
/// cache forever in browser & CDN.
2834
/// Valid when you have hashed / versioned filenames and every rebuild would
2935
/// change the filename.
3036
ForeverInCdnAndBrowser,
@@ -46,6 +52,7 @@ impl CachePolicy {
4652
match *self {
4753
CachePolicy::NoCaching => Some(NO_CACHING.clone()),
4854
CachePolicy::NoStoreMustRevalidate => Some(NO_STORE_MUST_REVALIDATE.clone()),
55+
CachePolicy::ShortInCdnAndBrowser => Some(SHORT.clone()),
4956
CachePolicy::ForeverInCdnAndBrowser => Some(FOREVER_IN_CDN_AND_BROWSER.clone()),
5057
CachePolicy::ForeverInCdn => {
5158
if config.cache_invalidatable_responses {

src/web/releases.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ use std::sync::Arc;
3030
use tracing::{debug, warn};
3131
use url::form_urlencoded;
3232

33+
use super::cache::CachePolicy;
34+
3335
/// Number of release in home page
3436
const RELEASES_IN_HOME: i64 = 15;
3537
/// Releases in /releases page
@@ -271,6 +273,7 @@ struct HomePage {
271273

272274
impl_axum_webpage! {
273275
HomePage = "core/home.html",
276+
cache_policy = |_| CachePolicy::ShortInCdnAndBrowser,
274277
}
275278

276279
pub(crate) async fn home_page(mut conn: DbConnection) -> AxumResult<impl IntoResponse> {
@@ -719,7 +722,8 @@ mod tests {
719722
use super::*;
720723
use crate::registry_api::CrateOwner;
721724
use crate::test::{
722-
assert_redirect, assert_redirect_unchecked, assert_success, wrapper, TestFrontend,
725+
assert_cache_control, assert_redirect, assert_redirect_unchecked, assert_success, wrapper,
726+
TestFrontend,
723727
};
724728
use anyhow::Error;
725729
use chrono::{Duration, TimeZone};
@@ -1484,6 +1488,8 @@ mod tests {
14841488
seen.insert("".to_owned());
14851489

14861490
let resp = web.get("").send()?;
1491+
assert_cache_control(&resp, CachePolicy::ShortInCdnAndBrowser, &env.config());
1492+
14871493
assert!(resp.status().is_success());
14881494

14891495
let html = kuchikiki::parse_html().one(resp.text()?);

src/web/rustdoc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ mod test {
10131013
.build_result_failed()
10141014
.create()?;
10151015
let web = env.frontend();
1016-
assert_success_cached("/", web, CachePolicy::NoCaching, &env.config())?;
1016+
assert_success_cached("/", web, CachePolicy::ShortInCdnAndBrowser, &env.config())?;
10171017
assert_success_cached(
10181018
"/crate/buggy/0.1.0/",
10191019
web,

0 commit comments

Comments
 (0)