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> {