From 116f3281a4c62147c24ca9f18a4bb67e0bfda8e0 Mon Sep 17 00:00:00 2001 From: xfy Date: Wed, 10 Jun 2026 13:59:41 +0800 Subject: [PATCH] feat: update post list cache to store total count --- src/cache.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index 56320c0..36b525a 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -44,7 +44,7 @@ pub enum CacheKey { // ============================================================================ #[cfg(feature = "server")] -pub type PostListCache = Cache>; +pub type PostListCache = Cache, i64)>; #[cfg(feature = "server")] pub type TagListCache = Cache>; @@ -100,13 +100,13 @@ static TAG_POSTS_CACHE: LazyLock = LazyLock::new(|| { // ============================================================================ #[cfg(feature = "server")] -pub async fn get_post_list(key: &CacheKey) -> Option> { +pub async fn get_post_list(key: &CacheKey) -> Option<(Vec, i64)> { POST_LIST_CACHE.get(key).await } #[cfg(feature = "server")] -pub async fn set_post_list(key: &CacheKey, posts: Vec) { - let _ = POST_LIST_CACHE.insert(key.clone(), posts).await; +pub async fn set_post_list(key: &CacheKey, posts: Vec, total: i64) { + let _ = POST_LIST_CACHE.insert(key.clone(), (posts, total)).await; } #[cfg(feature = "server")] @@ -134,16 +134,16 @@ pub async fn set_post_by_slug(slug: &str, post: Option) { } #[cfg(feature = "server")] -pub async fn get_posts_by_tag(tag: &str) -> Option> { +pub async fn get_posts_by_tag(tag: &str) -> Option<(Vec, i64)> { TAG_POSTS_CACHE .get(&CacheKey::PostsByTag(tag.to_string())) .await } #[cfg(feature = "server")] -pub async fn set_posts_by_tag(tag: &str, posts: Vec) { +pub async fn set_posts_by_tag(tag: &str, posts: Vec, total: i64) { let _ = TAG_POSTS_CACHE - .insert(CacheKey::PostsByTag(tag.to_string()), posts) + .insert(CacheKey::PostsByTag(tag.to_string()), (posts, total)) .await; } @@ -218,11 +218,13 @@ mod tests { let key = CacheKey::PublishedPosts { page: 999, per_page: 99 }; let posts = vec![]; - set_post_list(&key, posts.clone()).await; + set_post_list(&key, posts.clone(), 0).await; let cached = get_post_list(&key).await; assert!(cached.is_some()); - assert_eq!(cached.unwrap().len(), 0); + let (cached_posts, cached_total) = cached.unwrap(); + assert_eq!(cached_posts.len(), 0); + assert_eq!(cached_total, 0); } #[tokio::test]