Skip to content

Commit

Permalink
remove latest-version caching logic in favor for new full page caching
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Sep 24, 2022
1 parent 420f88d commit 0b4ed16
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 130 deletions.
14 changes: 0 additions & 14 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,6 @@ pub struct Config {
// generate just that directive. Values are in seconds.
pub(crate) cache_control_stale_while_revalidate: Option<u32>,
pub(crate) cache_control_max_age: Option<u32>,
pub(crate) cache_control_s_max_age: Option<u32>,

// Cache-Control header, for /latest/ URLs.
// Same conditions as above apply.
pub(crate) cache_control_stale_while_revalidate_latest: Option<u32>,
pub(crate) cache_control_max_age_latest: Option<u32>,
pub(crate) cache_control_s_max_age_latest: Option<u32>,

pub(crate) cdn_backend: CdnKind,

Expand Down Expand Up @@ -153,13 +146,6 @@ impl Config {
"CACHE_CONTROL_STALE_WHILE_REVALIDATE",
)?,
cache_control_max_age: maybe_env("CACHE_CONTROL_MAX_AGE")?,
cache_control_s_max_age: maybe_env("CACHE_CONTROL_S_MAX_AGE")?,

cache_control_stale_while_revalidate_latest: maybe_env(
"CACHE_CONTROL_STALE_WHILE_REVALIDATE_LATEST",
)?,
cache_control_max_age_latest: maybe_env("CACHE_CONTROL_MAX_AGE_LATEST")?,
cache_control_s_max_age_latest: maybe_env("CACHE_CONTROL_S_MAX_AGE_LATEST")?,

cdn_backend: env("DOCSRS_CDN_BACKEND", CdnKind::Dummy)?,

Expand Down
135 changes: 19 additions & 116 deletions src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,6 @@ static DOC_RUST_LANG_ORG_REDIRECTS: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
])
});

fn generate_cache_directives_for(
max_age: Option<u32>,
s_max_age: Option<u32>,
stale_while_revalidate: Option<u32>,
) -> Option<CacheControl> {
let mut directives = vec![];

if let Some(seconds) = stale_while_revalidate {
directives.push(CacheDirective::Extension(
"stale-while-revalidate".to_string(),
Some(format!("{}", seconds)),
));
}

if let Some(seconds) = max_age {
directives.push(CacheDirective::MaxAge(seconds));
}

if let Some(seconds) = s_max_age {
directives.push(CacheDirective::SMaxAge(seconds));
}

if !directives.is_empty() {
Some(CacheControl(directives))
} else {
None
}
}

/// Handler called for `/:crate` and `/:crate/:version` URLs. Automatically redirects to the docs
/// or crate details page based on whether the given crate version was successfully built.
pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
Expand Down Expand Up @@ -287,21 +258,26 @@ impl RustdocPage {
let mut response = Response::with((Status::Ok, html));
response.headers.set(ContentType::html());

let cache_control = if is_latest_url {
generate_cache_directives_for(
Some(config.cache_control_max_age_latest.unwrap_or(0)),
config.cache_control_s_max_age_latest,
config.cache_control_stale_while_revalidate_latest,
)
if is_latest_url {
response
.headers
.set(CacheControl(vec![CacheDirective::MaxAge(0)]));
} else {
generate_cache_directives_for(
config.cache_control_max_age,
config.cache_control_s_max_age,
config.cache_control_stale_while_revalidate,
)
};
if let Some(cache_control) = cache_control {
response.headers.set(cache_control);
let mut directives = vec![];
if let Some(seconds) = config.cache_control_stale_while_revalidate {
directives.push(CacheDirective::Extension(
"stale-while-revalidate".to_string(),
Some(format!("{}", seconds)),
));
}

if let Some(seconds) = config.cache_control_max_age {
directives.push(CacheDirective::MaxAge(seconds));
}

if !directives.is_empty() {
response.headers.set(CacheControl(directives));
}
}
Ok(response)
}
Expand Down Expand Up @@ -909,39 +885,6 @@ mod test {
})
}

#[test]
fn cache_headers_only_latest() {
wrapper(|env| {
env.override_config(|config| {
config.cache_control_max_age_latest = Some(600);
config.cache_control_stale_while_revalidate_latest = Some(2592000);
});

env.fake_release()
.name("dummy")
.version("0.1.0")
.archive_storage(true)
.rustdoc_file("dummy/index.html")
.create()?;

let web = env.frontend();

{
let resp = web.get("/dummy/latest/dummy/").send()?;
assert_eq!(
resp.headers().get("Cache-Control").unwrap(),
&"stale-while-revalidate=2592000, max-age=600"
);
}

{
let resp = web.get("/dummy/0.1.0/dummy/").send()?;
assert!(resp.headers().get("Cache-Control").is_none());
}
Ok(())
})
}

#[test]
fn cache_headers_on_version() {
wrapper(|env| {
Expand Down Expand Up @@ -975,46 +918,6 @@ mod test {
})
}

#[test]
fn cache_headers_latest_and_version() {
wrapper(|env| {
env.override_config(|config| {
config.cache_control_max_age = Some(666);
config.cache_control_s_max_age = Some(777);
config.cache_control_max_age_latest = Some(999);
config.cache_control_s_max_age_latest = Some(888);
config.cache_control_stale_while_revalidate = Some(2222222);
config.cache_control_stale_while_revalidate_latest = Some(3333333);
});

env.fake_release()
.name("dummy")
.version("0.1.0")
.archive_storage(true)
.rustdoc_file("dummy/index.html")
.create()?;

let web = env.frontend();

{
let resp = web.get("/dummy/latest/dummy/").send()?;
assert_eq!(
resp.headers().get("Cache-Control").unwrap(),
&"stale-while-revalidate=3333333, max-age=999, s-maxage=888"
);
}

{
let resp = web.get("/dummy/0.1.0/dummy/").send()?;
assert_eq!(
resp.headers().get("Cache-Control").unwrap(),
&"stale-while-revalidate=2222222, max-age=666, s-maxage=777"
);
}
Ok(())
})
}

#[test_case(true)]
#[test_case(false)]
fn go_to_latest_version(archive_storage: bool) {
Expand Down

0 comments on commit 0b4ed16

Please sign in to comment.