From 312d9360c3f91d2bde1f0ba6ddb394976019855a Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Wed, 7 Aug 2019 21:26:12 +0200 Subject: [PATCH] Cache redirects for 5 minutes in CDN and local --- backend/src/cache.rs | 3 ++- backend/src/main.rs | 7 +++++-- backend/src/service.rs | 12 ++++++++++-- backend/src/statics.rs | 3 ++- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/backend/src/cache.rs b/backend/src/cache.rs index dd0a36c..a190484 100644 --- a/backend/src/cache.rs +++ b/backend/src/cache.rs @@ -1,3 +1,4 @@ +use crate::statics::REDIRECT_AGE; use std::{ collections::HashMap, hash::Hash, @@ -17,7 +18,7 @@ where pub(crate) fn new() -> Self { Self { cache: HashMap::new(), - duration: Duration::from_secs(5 * 60), + duration: REDIRECT_AGE, } } diff --git a/backend/src/main.rs b/backend/src/main.rs index 7eb5b3a..9aceba8 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -21,7 +21,7 @@ use crate::{ data::{FilePath, State}, error::Result, service::{Bitbucket, GitLab, Github, Service}, - statics::{FAVICON, OPT}, + statics::{FAVICON, OPT, REDIRECT_AGE}, }; use actix_files; use actix_web::{ @@ -80,7 +80,10 @@ fn redirect( LOCATION, T::redirect_url(&data.user, &data.repo, &head, &data.file).as_str(), ) - .set(CacheControl(vec![CacheDirective::Private])) + .set(CacheControl(vec![ + CacheDirective::Public, + CacheDirective::MaxAge(REDIRECT_AGE.as_secs() as u32), + ])) .finish() })); } diff --git a/backend/src/service.rs b/backend/src/service.rs index 9be1623..b01044b 100644 --- a/backend/src/service.rs +++ b/backend/src/service.rs @@ -108,7 +108,10 @@ pub(crate) trait Service: Sized { ) .as_str(), ) - .set(CacheControl(vec![CacheDirective::Private])) + .set(CacheControl(vec![ + CacheDirective::Public, + CacheDirective::MaxAge(REDIRECT_AGE.as_secs() as u32), + ])) .finish() }) .from_err(), @@ -272,7 +275,12 @@ impl Service for GitLab { ) .as_str(), ) - .set(CacheControl(vec![CacheDirective::Private])) + .set(CacheControl(vec![ + CacheDirective::Public, + CacheDirective::MaxAge( + REDIRECT_AGE.as_secs() as u32 + ), + ])) .finish() }) .from_err(), diff --git a/backend/src/statics.rs b/backend/src/statics.rs index 93da011..0d065fe 100644 --- a/backend/src/statics.rs +++ b/backend/src/statics.rs @@ -1,8 +1,9 @@ use crate::{config::Opt, service::Github}; -use std::env; +use std::{env, time::Duration}; use structopt::StructOpt; const VERSION: &str = env!("CARGO_PKG_VERSION"); +pub(crate) const REDIRECT_AGE: Duration = Duration::from_secs(5 * 60); pub(crate) const FAVICON: &[u8] = include_bytes!("../static/favicon32.png"); lazy_static! { pub(crate) static ref USER_AGENT: String = format!("gitache/{}", VERSION);