From 1092fbb3ce01567157ad02ad2cc9048cbc9f609d Mon Sep 17 00:00:00 2001 From: xfy Date: Wed, 17 Jun 2026 16:44:32 +0800 Subject: [PATCH] fix(posts): ensure rebuild invalidates tag posts cache --- src/api/posts/delete.rs | 4 ++-- src/api/posts/rebuild.rs | 21 +++++++-------------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/api/posts/delete.rs b/src/api/posts/delete.rs index d54df43..bfc0aba 100644 --- a/src/api/posts/delete.rs +++ b/src/api/posts/delete.rs @@ -1,7 +1,7 @@ //! 删除文章接口。 //! //! 采用软删除方式,将 posts.deleted_at 设置为当前时间, -//! 同时清空所有文章相关缓存。 +//! 并按影响范围失效相关缓存。 //! Dioxus server function,注册在 `/api` 路径下。 //! 仅在 `feature = "server"` 启用的服务端构建中执行删除与缓存失效。 @@ -18,7 +18,7 @@ use crate::db::pool::get_conn; /// 删除指定文章。 /// /// 仅 admin 可调用;通过设置 deleted_at 实现软删除, -/// 成功后清空全部文章缓存。 +/// 成功后按影响范围失效文章列表、标签云、统计、slug 及相关标签文章缓存。 #[server(DeletePost, "/api")] pub async fn delete_post(post_id: i32) -> Result { let _user = get_current_admin_user().await?; diff --git a/src/api/posts/rebuild.rs b/src/api/posts/rebuild.rs index c6f6afa..fe2d79d 100644 --- a/src/api/posts/rebuild.rs +++ b/src/api/posts/rebuild.rs @@ -34,14 +34,14 @@ pub async fn rebuild_content_html(rebuild_all: bool) -> Result Result = Vec::new(); - let mut rebuilt_slugs: Vec = Vec::with_capacity(rows.len()); for row in &rows { let id: i32 = row.get(0); - let slug: String = row.get(1); - let content_md: String = row.get(2); + let content_md: String = row.get(1); // 捕获 Markdown 渲染 panic,避免单条记录导致整批失败。 let rendered = match std::panic::catch_unwind(|| { @@ -95,7 +93,6 @@ pub async fn rebuild_content_html(rebuild_all: bool) -> Result { rebuilt += 1; - rebuilt_slugs.push(slug); } Err(_) => { failed += 1; @@ -106,14 +103,10 @@ pub async fn rebuild_content_html(rebuild_all: bool) -> Result 0 { - crate::cache::invalidate_post_lists(); - crate::cache::invalidate_all_tags(); - crate::cache::invalidate_post_stats(); - for slug in &rebuilt_slugs { - crate::cache::invalidate_post_by_slug(slug).await; - } + crate::cache::invalidate_all_post_caches(); } Ok(RebuildResult {