cache ttl

This commit is contained in:
JMARyA 2024-04-15 09:42:08 +02:00
parent b0ce294ec8
commit 2cc0bfbb09
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
3 changed files with 37 additions and 5 deletions

View file

@ -9,7 +9,9 @@ mirrors = [
# Local cache directory # Local cache directory
cache_dir = "./data" cache_dir = "./data"
#ttl = "3h"
# Time in minutes before a file is counted as out of date
ttl = "180"
# Regex for paths which will never be served from cache # Regex for paths which will never be served from cache
no_cache = '.*(?:db|db\.sig)$' no_cache = '.*(?:db|db\.sig)$'

View file

@ -7,10 +7,16 @@ pub struct Config {
pub mirrors: Vec<String>, pub mirrors: Vec<String>,
pub cache_dir: String, pub cache_dir: String,
pub no_cache: String, pub no_cache: String,
pub ttl: usize,
} }
impl Config { impl Config {
pub fn to_proxy(&self) -> Mirror { pub fn to_proxy(&self) -> Mirror {
Mirror::new(self.mirrors.clone(), &self.cache_dir, &self.no_cache) Mirror::new(
self.mirrors.clone(),
&self.cache_dir,
&self.no_cache,
self.ttl,
)
} }
} }

View file

@ -8,15 +8,17 @@ use std::{
pub struct Mirror { pub struct Mirror {
mirrors: Vec<Arc<String>>, mirrors: Vec<Arc<String>>,
data_dir: String, data_dir: String,
ttl: usize,
no_cache: regex::Regex, no_cache: regex::Regex,
} }
impl Mirror { impl Mirror {
pub fn new(mirrors: Vec<String>, data_dir: &str, no_cache: &str) -> Self { pub fn new(mirrors: Vec<String>, data_dir: &str, no_cache: &str, ttl: usize) -> Self {
Self { Self {
mirrors: mirrors.into_iter().map(Arc::new).collect(), mirrors: mirrors.into_iter().map(Arc::new).collect(),
data_dir: data_dir.to_string(), data_dir: data_dir.to_string(),
no_cache: regex::Regex::new(no_cache).unwrap(), no_cache: regex::Regex::new(no_cache).unwrap(),
ttl,
} }
} }
@ -49,6 +51,29 @@ impl Mirror {
} }
} }
pub fn is_cache_invalid(&self, p: &Path) -> bool {
let try_is_cache_invalid = || {
let modified = p.metadata().ok()?.modified().ok()?;
let current_time = std::time::SystemTime::now();
let elapsed_time = current_time.duration_since(modified).ok()?;
let threshold_duration =
std::time::Duration::from_secs((self.ttl * 60).try_into().unwrap());
if elapsed_time > threshold_duration {
log::info!(
"Cached file is {} minutes old. Older than TTL {}.",
(elapsed_time.as_secs() / 60),
self.ttl
);
Some(true)
} else {
Some(false)
}
};
try_is_cache_invalid().unwrap_or(false)
}
/// Asynchronously retrieves content from the specified path, either from cache or mirrors. /// Asynchronously retrieves content from the specified path, either from cache or mirrors.
/// ///
/// This function attempts to retrieve content from the specified `path`. If caching is enabled /// This function attempts to retrieve content from the specified `path`. If caching is enabled
@ -69,7 +94,7 @@ impl Mirror {
let p = std::path::Path::new(&self.data_dir).join(p); let p = std::path::Path::new(&self.data_dir).join(p);
// check if cache should be used // check if cache should be used
if !self.no_cache.is_match(path) { if !self.no_cache.is_match(path) || !self.is_cache_invalid(&p) {
Self::create_cache_dir(p.parent().unwrap()); Self::create_cache_dir(p.parent().unwrap());
// use cache if present // use cache if present
@ -90,7 +115,6 @@ impl Mirror {
pub async fn fetch_cache(&self, p: &PathBuf, req: &HttpRequest) -> Option<HttpResponse> { pub async fn fetch_cache(&self, p: &PathBuf, req: &HttpRequest) -> Option<HttpResponse> {
if p.exists() { if p.exists() {
// todo : refresh caches
if p.is_dir() { if p.is_dir() {
return Some( return Some(
actix_files::NamedFile::open_async(p.join("index")) actix_files::NamedFile::open_async(p.join("index"))