From 52d3b89d27ae8697c72c699ff39d41bb9e79f489 Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 10 Oct 2022 22:46:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/common/comments.go | 18 ++++++++++++++---- actions/common/common.go | 4 ++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/actions/common/comments.go b/actions/common/comments.go index 69b772e..f99a84d 100644 --- a/actions/common/comments.go +++ b/actions/common/comments.go @@ -27,18 +27,28 @@ func recentComments(...any) (r []models.WpComments, err error) { } func PostComments(ctx context.Context, Id uint64) ([]models.WpComments, error) { - return postCommentCaches.GetCache(ctx, Id, time.Second, Id) + ids, err := postCommentCaches.GetCache(ctx, Id, time.Second, Id) + if err != nil { + return nil, err + } + return GetCommentByIds(ctx, ids) } -func postComments(args ...any) ([]models.WpComments, error) { +func postComments(args ...any) ([]uint64, error) { postId := args[0].(uint64) - return models.Find[models.WpComments](models.SqlBuilder{ + r, err := models.Find[models.WpComments](models.SqlBuilder{ {"comment_approved", "1"}, {"comment_post_ID", "=", strconv.FormatUint(postId, 10), "int"}, - }, "*", "", models.SqlBuilder{ + }, "comment_ID", "", models.SqlBuilder{ {"comment_date_gmt", "asc"}, {"comment_ID", "asc"}, }, nil, 0) + if err != nil { + return nil, err + } + return helper.SliceMap(r, func(t models.WpComments) uint64 { + return t.CommentId + }), err } func GetCommentById(ctx context.Context, id uint64) (models.WpComments, error) { diff --git a/actions/common/common.go b/actions/common/common.go index ce0e8f1..c7c8150 100644 --- a/actions/common/common.go +++ b/actions/common/common.go @@ -16,7 +16,7 @@ var archivesCaches *Arch var categoryCaches *cache.SliceCache[models.WpTermsMy] var recentPostsCaches *cache.SliceCache[models.WpPosts] var recentCommentsCaches *cache.SliceCache[models.WpComments] -var postCommentCaches *cache.MapCache[uint64, []models.WpComments] +var postCommentCaches *cache.MapCache[uint64, []uint64] var postsCache *cache.MapCache[uint64, models.WpPosts] var monthPostsCache *cache.MapCache[string, []uint64] var postListIdsCache *cache.MapCache[string, PostIds] @@ -48,7 +48,7 @@ func InitActionsCommonCache() { recentCommentsCaches = cache.NewSliceCache[models.WpComments](recentComments, vars.Conf.RecentCommentsCacheTime) - postCommentCaches = cache.NewMapCacheByFn[uint64, []models.WpComments](postComments, vars.Conf.PostCommentsCacheTime) + postCommentCaches = cache.NewMapCacheByFn[uint64, []uint64](postComments, vars.Conf.PostCommentsCacheTime) maxPostIdCache = cache.NewSliceCache[uint64](getMaxPostId, vars.Conf.MaxPostIdCacheTime)