From c9bbb5234e30da10297bce64cb6eef056766f37c Mon Sep 17 00:00:00 2001 From: slonkazoid Date: Sun, 29 Dec 2024 13:37:48 +0300 Subject: [PATCH] add cache ttl handling to cleanup --- src/post/blag.rs | 2 +- src/post/cache.rs | 13 +++++++++++-- src/post/markdown_posts.rs | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/post/blag.rs b/src/post/blag.rs index b4b12c4..9c1ae2a 100644 --- a/src/post/blag.rs +++ b/src/post/blag.rs @@ -307,7 +307,7 @@ impl PostManager for Blag { async fn cleanup(&self) { if let Some(cache) = &self.cache { cache - .retain(|key, value| { + .cleanup(|key, value| { let mtime = std::fs::metadata( self.root .join(self.as_raw(&key.name).unwrap_or_else(|| unreachable!())), diff --git a/src/post/cache.rs b/src/post/cache.rs index 0fc8c3d..36ffcab 100644 --- a/src/post/cache.rs +++ b/src/post/cache.rs @@ -56,7 +56,7 @@ impl Cache { mtime <= cached.mtime && self .ttl - .is_some_and(|ttl| cached.cached_at + u64::from(ttl) as u128 >= now()) + .is_none_or(|ttl| cached.cached_at + u64::from(ttl) as u128 >= now()) } #[instrument(level = "debug", skip(self), fields(entry_mtime))] @@ -158,7 +158,6 @@ impl Cache { r } - #[instrument(level = "debug", name = "cleanup", skip_all)] pub async fn retain(&self, predicate: impl Fn(&CacheKey, &CacheValue) -> bool) { let old_size = self.map.len(); let mut i = 0; @@ -181,6 +180,16 @@ impl Cache { debug!("removed {i} entries ({old_size} -> {new_size} entries)"); } + #[instrument(level = "debug", skip_all)] + pub async fn cleanup(&self, predicate: impl Fn(&CacheKey, &CacheValue) -> bool) { + self.retain(|k, v| { + self.ttl + .is_none_or(|ttl| v.cached_at + u64::from(ttl) as u128 >= now()) + && predicate(k, v) + }) + .await + } + pub fn len(&self) -> usize { self.map.len() } diff --git a/src/post/markdown_posts.rs b/src/post/markdown_posts.rs index df4141c..45e601c 100644 --- a/src/post/markdown_posts.rs +++ b/src/post/markdown_posts.rs @@ -297,7 +297,7 @@ impl PostManager for MarkdownPosts { async fn cleanup(&self) { if let Some(cache) = &self.cache { cache - .retain(|CacheKey { name, extra }, value| { + .cleanup(|CacheKey { name, extra }, value| { // nuke entries with different render options if self.render_hash != *extra { return false;