From c780247d176b2d9de442c788bf41b7437d6cb4cf Mon Sep 17 00:00:00 2001 From: xfy Date: Wed, 17 Jun 2026 17:21:01 +0800 Subject: [PATCH] feat(posts): invalidate search cache on writes --- src/api/posts/create.rs | 1 + src/api/posts/delete.rs | 1 + src/api/posts/rebuild.rs | 1 + src/api/posts/trash.rs | 8 ++++++++ src/api/posts/update.rs | 1 + 5 files changed, 12 insertions(+) diff --git a/src/api/posts/create.rs b/src/api/posts/create.rs index b83bf66..d2e52b8 100644 --- a/src/api/posts/create.rs +++ b/src/api/posts/create.rs @@ -143,6 +143,7 @@ pub async fn create_post( crate::cache::invalidate_post_lists(); crate::cache::invalidate_all_tags(); crate::cache::invalidate_post_stats(); + crate::cache::invalidate_search_results(); // 失效按 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 b4f4dbe..44e4019 100644 --- a/src/api/posts/delete.rs +++ b/src/api/posts/delete.rs @@ -80,6 +80,7 @@ pub async fn delete_post(post_id: i32) -> Result Result 0 { crate::cache::invalidate_all_post_caches(); + crate::cache::invalidate_search_results(); } Ok(RebuildResult { diff --git a/src/api/posts/trash.rs b/src/api/posts/trash.rs index 6ca570c..2c33af6 100644 --- a/src/api/posts/trash.rs +++ b/src/api/posts/trash.rs @@ -90,6 +90,7 @@ pub async fn restore_post(post_id: i32) -> Result Result) -> Result) -> Result) -> Result) -> Result Result { crate::cache::invalidate_post_lists(); crate::cache::invalidate_all_tags(); crate::cache::invalidate_post_stats(); + crate::cache::invalidate_search_results(); for slug in &slugs { crate::cache::invalidate_post_by_slug(slug).await; } @@ -462,6 +469,7 @@ pub async fn empty_trash() -> Result { } else { // 影响集过大时回退到全量失效,避免大量串行缓存操作。 crate::cache::invalidate_all_post_caches(); + crate::cache::invalidate_search_results(); } Ok(CreatePostResponse { diff --git a/src/api/posts/update.rs b/src/api/posts/update.rs index d00f30c..9e6477d 100644 --- a/src/api/posts/update.rs +++ b/src/api/posts/update.rs @@ -199,6 +199,7 @@ pub async fn update_post( crate::cache::invalidate_post_lists(); crate::cache::invalidate_all_tags(); crate::cache::invalidate_post_stats(); + crate::cache::invalidate_search_results(); crate::cache::invalidate_post_by_slug(&final_slug).await; // 合并旧标签与新标签,统一失效标签下的文章列表缓存。