Skip to content

Commit

Permalink
cache the homepage for a minute
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Nov 24, 2023
1 parent d7bfe85 commit bd23777
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/web/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use http::{header::CACHE_CONTROL, HeaderValue};
use std::sync::Arc;

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

pub static NO_STORE_MUST_REVALIDATE: HeaderValue =
HeaderValue::from_static("no-cache, no-store, must-revalidate, max-age=0");
Expand All @@ -24,7 +25,12 @@ pub enum CachePolicy {
/// * enforce revalidation
/// * never store
NoStoreMustRevalidate,
/// cache forever in browser & CDN.
/// cache for a short time in the browser & CDN.
/// right now: one minute.
/// Can be used when the content can be a _little_ outdated,
/// while protecting agains spikes in traffic.
ShortInCdnAndBrowser,
/// cache forever in browser & CDN.
/// Valid when you have hashed / versioned filenames and every rebuild would
/// change the filename.
ForeverInCdnAndBrowser,
Expand All @@ -46,6 +52,7 @@ impl CachePolicy {
match *self {
CachePolicy::NoCaching => Some(NO_CACHING.clone()),
CachePolicy::NoStoreMustRevalidate => Some(NO_STORE_MUST_REVALIDATE.clone()),
CachePolicy::ShortInCdnAndBrowser => Some(SHORT.clone()),
CachePolicy::ForeverInCdnAndBrowser => Some(FOREVER_IN_CDN_AND_BROWSER.clone()),
CachePolicy::ForeverInCdn => {
if config.cache_invalidatable_responses {
Expand Down
8 changes: 7 additions & 1 deletion src/web/releases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use std::sync::Arc;
use tracing::{debug, warn};
use url::form_urlencoded;

use super::cache::CachePolicy;

/// Number of release in home page
const RELEASES_IN_HOME: i64 = 15;
/// Releases in /releases page
Expand Down Expand Up @@ -271,6 +273,7 @@ struct HomePage {

impl_axum_webpage! {
HomePage = "core/home.html",
cache_policy = |_| CachePolicy::ShortInCdnAndBrowser,
}

pub(crate) async fn home_page(mut conn: DbConnection) -> AxumResult<impl IntoResponse> {
Expand Down Expand Up @@ -719,7 +722,8 @@ mod tests {
use super::*;
use crate::registry_api::CrateOwner;
use crate::test::{
assert_redirect, assert_redirect_unchecked, assert_success, wrapper, TestFrontend,
assert_cache_control, assert_redirect, assert_redirect_unchecked, assert_success, wrapper,
TestFrontend,
};
use anyhow::Error;
use chrono::{Duration, TimeZone};
Expand Down Expand Up @@ -1484,6 +1488,8 @@ mod tests {
seen.insert("".to_owned());

let resp = web.get("").send()?;
assert_cache_control(&resp, CachePolicy::ShortInCdnAndBrowser, &env.config());

assert!(resp.status().is_success());

let html = kuchikiki::parse_html().one(resp.text()?);
Expand Down
2 changes: 1 addition & 1 deletion src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ mod test {
.build_result_failed()
.create()?;
let web = env.frontend();
assert_success_cached("/", web, CachePolicy::NoCaching, &env.config())?;
assert_success_cached("/", web, CachePolicy::ShortInCdnAndBrowser, &env.config())?;
assert_success_cached(
"/crate/buggy/0.1.0/",
web,
Expand Down

0 comments on commit bd23777

Please sign in to comment.