diff --git a/src/api/posts/rebuild.rs b/src/api/posts/rebuild.rs index 2142bde..c6f6afa 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 content_md: String = row.get(1); + let slug: String = row.get(1); + let content_md: String = row.get(2); // 捕获 Markdown 渲染 panic,避免单条记录导致整批失败。 let rendered = match std::panic::catch_unwind(|| { @@ -91,7 +93,10 @@ pub async fn rebuild_content_html(rebuild_all: bool) -> Result rebuilt += 1, + Ok(_) => { + rebuilt += 1; + rebuilt_slugs.push(slug); + } Err(_) => { failed += 1; if errors.len() < MAX_DISPLAY_ERRORS { @@ -101,9 +106,14 @@ pub async fn rebuild_content_html(rebuild_all: bool) -> Result 0 || failed > 0 { - crate::cache::invalidate_all_post_caches(); + // 只要有文章被更新,就按影响范围失效缓存:列表、标签云、统计,以及每篇被重建文章的 slug 缓存。 + if rebuilt > 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; + } } Ok(RebuildResult {