From 5b1003c3b3212ca5f429a1c9ac2a24a465fb7646 Mon Sep 17 00:00:00 2001 From: Elizabeth Myers Date: Thu, 21 Mar 2024 14:35:55 -0500 Subject: [PATCH] First steps to making caches configurable. Signed-off-by: Elizabeth Myers --- src/bancache.rs | 11 ++++------- src/urlcache.rs | 16 +++++++++------- src/web/app.rs | 6 ++++-- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/bancache.rs b/src/bancache.rs index c66433a..45d723d 100644 --- a/src/bancache.rs +++ b/src/bancache.rs @@ -24,9 +24,6 @@ use service::Query; // This caches IP bans/allows so we don't hit the database so much. -// TODO: configurable -const CACHE_ENTRIES: u64 = 10_000; - #[derive(Debug, thiserror::Error)] pub enum BanCacheError { #[error(transparent)] @@ -40,13 +37,13 @@ pub struct BanCache { } impl BanCache { - pub(crate) fn new(db: Arc) -> Self { + pub(crate) fn new(db: Arc, entries: u64, ttl: Duration, idle: Duration) -> Self { Self { // XXX - should these cache parameters be configurable? cache: Cache::builder() - .max_capacity(CACHE_ENTRIES) - .time_to_live(Duration::days(1).unsigned_abs()) - .time_to_idle(Duration::minutes(10).unsigned_abs()) + .max_capacity(entries) + .time_to_live(ttl.unsigned_abs()) + .time_to_idle(idle.unsigned_abs()) .support_invalidation_closures() .build(), db, diff --git a/src/urlcache.rs b/src/urlcache.rs index f947ead..aa4afec 100644 --- a/src/urlcache.rs +++ b/src/urlcache.rs @@ -25,9 +25,6 @@ use tracing::trace; use service::Query; -// TODO: configurable -const CACHE_ENTRIES: u64 = 1000; - #[derive(Debug, thiserror::Error)] pub enum UrlCacheError { #[error(transparent)] @@ -53,7 +50,12 @@ pub struct UrlCache { impl UrlCache { // Create a new UrlCache instance and initalise regexes from the database. - pub(crate) async fn new(db: Arc) -> Result { + pub(crate) async fn new( + db: Arc, + entries: u64, + ttl: Duration, + idle: Duration, + ) -> Result { let mut regex = Vec::new(); for url_filter in Query::fetch_all_url_filters(&db).await? { trace!("Adding URL filter {}", url_filter.0.filter); @@ -65,9 +67,9 @@ impl UrlCache { // XXX - should these cache parameters be configurable? regex: Arc::new(RwLock::new(regex)), cache: Cache::builder() - .max_capacity(CACHE_ENTRIES) - .time_to_live(Duration::days(7).unsigned_abs()) - .time_to_idle(Duration::days(1).unsigned_abs()) + .max_capacity(entries) + .time_to_live(ttl.unsigned_abs()) + .time_to_idle(idle.unsigned_abs()) .support_invalidation_closures() .build(), db, diff --git a/src/web/app.rs b/src/web/app.rs index ff25068..0feba74 100644 --- a/src/web/app.rs +++ b/src/web/app.rs @@ -92,8 +92,10 @@ impl App { .sqlx_logging(false); let db = Arc::new(Database::get_with_connect_options(opt).await?); - let bancache = BanCache::new(db.clone()); - let urlcache = UrlCache::new(db.clone()).await?; + // TODO: configurable + let bancache = BanCache::new(db.clone(), 10_000, Duration::days(1), Duration::hours(1)); + let urlcache = + UrlCache::new(db.clone(), 1000, Duration::days(7), Duration::days(1)).await?; let csrf_crypto_engine = CryptoEngine::new(&env.csrf_key.into());