From 8da678b3b44095e2029a71b30ecde0643d7d9d63 Mon Sep 17 00:00:00 2001 From: xfy Date: Tue, 14 Jul 2026 14:44:00 +0800 Subject: [PATCH] =?UTF-8?q?refactor(cache):=20=E6=8A=BD=E5=8F=96=20invalid?= =?UTF-8?q?ate=5Fpost=5Fmetadata()=20=E5=B0=81=E8=A3=85=204=20=E8=A1=8C?= =?UTF-8?q?=E5=A4=B1=E6=95=88=E7=BB=84=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 文章写操作后总要一起失效 lists/tags/stats/search 四项元数据缓存。 新增 invalidate_post_metadata() 封装这个固定组合,替换 create/delete/ update/trash 中的 8 处重复 4 行序列。 保留定向失效(invalidate_post_by_slug / invalidate_tag_posts_for) 和批量回退路径(invalidate_all_post_caches + invalidate_search_results) 的显式调用——它们是按实际涉及数据细粒度控制的,不应被封装吞掉。 --- src/api/posts/create.rs | 5 +---- src/api/posts/delete.rs | 5 +---- src/api/posts/trash.rs | 25 +++++-------------------- src/api/posts/update.rs | 5 +---- src/cache.rs | 13 +++++++++++++ 5 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/api/posts/create.rs b/src/api/posts/create.rs index fbf841f..43b2d1c 100644 --- a/src/api/posts/create.rs +++ b/src/api/posts/create.rs @@ -130,10 +130,7 @@ pub async fn create_post( tx.commit().await.map_err(AppError::tx)?; // 写入成功后按粒度失效相关缓存。 - crate::cache::invalidate_post_lists(); - crate::cache::invalidate_all_tags(); - crate::cache::invalidate_post_stats(); - crate::cache::invalidate_search_results(); + crate::cache::invalidate_post_metadata(); // 失效按 slug 缓存,避免之前缓存的 404 继续命中。 crate::cache::invalidate_post_by_slug(&final_slug).await; // 失效该文章涉及的所有标签下文章列表缓存。 diff --git a/src/api/posts/delete.rs b/src/api/posts/delete.rs index 8db2c04..5881b20 100644 --- a/src/api/posts/delete.rs +++ b/src/api/posts/delete.rs @@ -67,10 +67,7 @@ pub async fn delete_post(post_id: i32) -> Result Result Result) -> Result = affected_slugs.into_iter().collect(); - crate::cache::invalidate_post_lists(); - crate::cache::invalidate_all_tags(); - crate::cache::invalidate_post_stats(); - crate::cache::invalidate_search_results(); + crate::cache::invalidate_post_metadata(); for slug in &unique_slugs { crate::cache::invalidate_post_by_slug(slug).await; } @@ -327,10 +318,7 @@ pub async fn batch_purge_posts(post_ids: Vec) -> Result Result { tx.commit().await.map_err(AppError::tx)?; if use_precise { - crate::cache::invalidate_post_lists(); - crate::cache::invalidate_all_tags(); - crate::cache::invalidate_post_stats(); - crate::cache::invalidate_search_results(); + crate::cache::invalidate_post_metadata(); for slug in &slugs { crate::cache::invalidate_post_by_slug(slug).await; } diff --git a/src/api/posts/update.rs b/src/api/posts/update.rs index 45f5099..f675c30 100644 --- a/src/api/posts/update.rs +++ b/src/api/posts/update.rs @@ -186,10 +186,7 @@ pub async fn update_post( tx.commit().await.map_err(AppError::tx)?; // 失效文章列表、标签、当前 slug 与统计缓存。 - crate::cache::invalidate_post_lists(); - crate::cache::invalidate_all_tags(); - crate::cache::invalidate_post_stats(); - crate::cache::invalidate_search_results(); + crate::cache::invalidate_post_metadata(); crate::cache::invalidate_post_by_slug(&final_slug).await; // 合并旧标签与新标签,统一失效标签下的文章列表缓存。 diff --git a/src/cache.rs b/src/cache.rs index 3f141c8..864d754 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -501,6 +501,19 @@ pub fn invalidate_all_post_caches() { TAG_POSTS_CACHE.invalidate_all(); } +/// 失效文章「元数据」类缓存:列表、标签、统计、搜索结果。 +/// +/// 这四项在每次文章写操作(创建/更新/删除/恢复/清空回收站)后都需要一起失效。 +/// 单篇正文与标签下文章列表是定向失效(按 slug / tag),不在此处处理,由调用方 +/// 根据实际涉及的 slug/tags 额外调用 `invalidate_post_by_slug` / `invalidate_tag_posts_for`。 +#[cfg(feature = "server")] +pub fn invalidate_post_metadata() { + invalidate_post_lists(); + invalidate_all_tags(); + invalidate_post_stats(); + invalidate_search_results(); +} + /// 按文章主键读取评论列表缓存。 #[cfg(feature = "server")] pub async fn get_comments_by_post(post_id: i32) -> Option> {