Skip to content

Commit

Permalink
First steps to making caches configurable.
Browse files Browse the repository at this point in the history
Signed-off-by: Elizabeth Myers <[email protected]>
  • Loading branch information
Elizafox committed Mar 21, 2024
1 parent 710c35c commit 5b1003c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
11 changes: 4 additions & 7 deletions src/bancache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -40,13 +37,13 @@ pub struct BanCache {
}

impl BanCache {
pub(crate) fn new(db: Arc<DbConn>) -> Self {
pub(crate) fn new(db: Arc<DbConn>, 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,
Expand Down
16 changes: 9 additions & 7 deletions src/urlcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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<DbConn>) -> Result<Self, UrlCacheError> {
pub(crate) async fn new(
db: Arc<DbConn>,
entries: u64,
ttl: Duration,
idle: Duration,
) -> Result<Self, UrlCacheError> {
let mut regex = Vec::new();
for url_filter in Query::fetch_all_url_filters(&db).await? {
trace!("Adding URL filter {}", url_filter.0.filter);
Expand All @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions src/web/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down

0 comments on commit 5b1003c

Please sign in to comment.