feat: update post list cache to store total count

This commit is contained in:
xfy 2026-06-10 13:59:41 +08:00
parent 72c1efa566
commit 116f3281a4

View File

@ -44,7 +44,7 @@ pub enum CacheKey {
// ============================================================================ // ============================================================================
#[cfg(feature = "server")] #[cfg(feature = "server")]
pub type PostListCache = Cache<CacheKey, Vec<Post>>; pub type PostListCache = Cache<CacheKey, (Vec<Post>, i64)>;
#[cfg(feature = "server")] #[cfg(feature = "server")]
pub type TagListCache = Cache<CacheKey, Vec<Tag>>; pub type TagListCache = Cache<CacheKey, Vec<Tag>>;
@ -100,13 +100,13 @@ static TAG_POSTS_CACHE: LazyLock<PostListCache> = LazyLock::new(|| {
// ============================================================================ // ============================================================================
#[cfg(feature = "server")] #[cfg(feature = "server")]
pub async fn get_post_list(key: &CacheKey) -> Option<Vec<Post>> { pub async fn get_post_list(key: &CacheKey) -> Option<(Vec<Post>, i64)> {
POST_LIST_CACHE.get(key).await POST_LIST_CACHE.get(key).await
} }
#[cfg(feature = "server")] #[cfg(feature = "server")]
pub async fn set_post_list(key: &CacheKey, posts: Vec<Post>) { pub async fn set_post_list(key: &CacheKey, posts: Vec<Post>, total: i64) {
let _ = POST_LIST_CACHE.insert(key.clone(), posts).await; let _ = POST_LIST_CACHE.insert(key.clone(), (posts, total)).await;
} }
#[cfg(feature = "server")] #[cfg(feature = "server")]
@ -134,16 +134,16 @@ pub async fn set_post_by_slug(slug: &str, post: Option<Post>) {
} }
#[cfg(feature = "server")] #[cfg(feature = "server")]
pub async fn get_posts_by_tag(tag: &str) -> Option<Vec<Post>> { pub async fn get_posts_by_tag(tag: &str) -> Option<(Vec<Post>, i64)> {
TAG_POSTS_CACHE TAG_POSTS_CACHE
.get(&CacheKey::PostsByTag(tag.to_string())) .get(&CacheKey::PostsByTag(tag.to_string()))
.await .await
} }
#[cfg(feature = "server")] #[cfg(feature = "server")]
pub async fn set_posts_by_tag(tag: &str, posts: Vec<Post>) { pub async fn set_posts_by_tag(tag: &str, posts: Vec<Post>, total: i64) {
let _ = TAG_POSTS_CACHE let _ = TAG_POSTS_CACHE
.insert(CacheKey::PostsByTag(tag.to_string()), posts) .insert(CacheKey::PostsByTag(tag.to_string()), (posts, total))
.await; .await;
} }
@ -218,11 +218,13 @@ mod tests {
let key = CacheKey::PublishedPosts { page: 999, per_page: 99 }; let key = CacheKey::PublishedPosts { page: 999, per_page: 99 };
let posts = vec![]; 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; let cached = get_post_list(&key).await;
assert!(cached.is_some()); 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] #[tokio::test]