From 247593cb57fb0c9a5862838fde1f3fc562ec0c1b Mon Sep 17 00:00:00 2001 From: xing Date: Wed, 28 Sep 2022 16:23:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E7=AB=A0=E5=88=97=E8=A1=A8=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/common/common.go | 9 ++++++--- actions/common/posts.go | 36 +++++++++++++++++++++++++++++++----- actions/index.go | 12 ++++++++---- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/actions/common/common.go b/actions/common/common.go index e0eb0c2..9068b45 100644 --- a/actions/common/common.go +++ b/actions/common/common.go @@ -22,6 +22,7 @@ var recentCommentsCaches *cache.SliceCache[models.WpComments] var postCommentCaches *cache.MapCache[uint64, []models.WpComments] var postsCache *cache.MapCache[uint64, models.WpPosts] var monthPostsCache *cache.MapCache[string, []uint64] +var searchPostIdsCache *cache.MapCache[string, PostIds] func InitActionsCommonCache() { archivesCaches = &Arch{ @@ -29,12 +30,14 @@ func InitActionsCommonCache() { setCacheFunc: archives, } + searchPostIdsCache = cache.NewMapCache[string, PostIds](searchPostIds, time.Hour) + monthPostsCache = cache.NewMapCache[string, []uint64](monthPost, time.Hour) postContextCache = cache.NewMapCache[uint64, PostContext](getPostContext, vars.Conf.ContextPostCacheTime) - postsCache = cache.NewMapBatchCache[uint64, models.WpPosts](getPosts, time.Hour) - postsCache.SetCacheFunc(getPost) + postsCache = cache.NewMapBatchCache[uint64, models.WpPosts](getPostsByIds, time.Hour) + postsCache.SetCacheFunc(getPostById) categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime) @@ -54,7 +57,7 @@ func GetMonthPostIds(ctx context.Context, year, month string, page, limit int, o } total = len(res) rr := helper.SlicePagination(res, page, limit) - r, err = GetPosts(ctx, rr) + r, err = GetPostsByIds(ctx, rr) return } diff --git a/actions/common/posts.go b/actions/common/posts.go index 670315c..8fc2617 100644 --- a/actions/common/posts.go +++ b/actions/common/posts.go @@ -14,11 +14,11 @@ func GetPostAndCache(ctx context.Context, id uint64) (models.WpPosts, error) { return postsCache.GetCache(ctx, id, time.Second, id) } -func GetPost(id uint64) models.WpPosts { +func GetPostById(id uint64) models.WpPosts { return postsCache.Get(id) } -func GetPosts(ctx context.Context, ids []uint64) ([]models.WpPosts, error) { +func GetPostsByIds(ctx context.Context, ids []uint64) ([]models.WpPosts, error) { return postsCache.GetCacheBatch(ctx, ids, time.Second, ids) } @@ -30,9 +30,9 @@ func SetPostCache(ids []models.WpPosts) error { return postsCache.SetByBatchFn(arg) } -func getPost(id ...any) (post models.WpPosts, err error) { +func getPostById(id ...any) (post models.WpPosts, err error) { Id := id[0].(uint64) - posts, err := getPosts([]uint64{Id}) + posts, err := getPostsByIds([]uint64{Id}) if err != nil { return models.WpPosts{}, err } @@ -40,7 +40,7 @@ func getPost(id ...any) (post models.WpPosts, err error) { return } -func getPosts(ids ...any) (m map[uint64]models.WpPosts, err error) { +func getPostsByIds(ids ...any) (m map[uint64]models.WpPosts, err error) { m = make(map[uint64]models.WpPosts) id := ids[0].([]uint64) arg := helper.SliceMap(id, helper.ToAny[uint64]) @@ -88,3 +88,29 @@ func getPosts(ids ...any) (m map[uint64]models.WpPosts, err error) { } return } + +func SearchPostIds(ctx context.Context, key string, args ...any) (r []models.WpPosts, total int, err error) { + ids, err := searchPostIdsCache.GetCache(ctx, key, time.Second, args...) + if err != nil { + return + } + total = ids.Length + r, err = GetPostsByIds(ctx, ids.Ids) + return +} + +func searchPostIds(args ...any) (ids PostIds, err error) { + where := args[0].(models.SqlBuilder) + page := args[1].(int) + limit := args[2].(int) + order := args[3].(models.SqlBuilder) + join := args[4].(models.SqlBuilder) + postType := args[5].([]any) + postStatus := args[6].([]any) + res, total, err := models.SimplePagination[models.WpPosts](where, "ID", "", page, limit, order, join, postType, postStatus) + for _, posts := range res { + ids.Ids = append(ids.Ids, posts.Id) + } + ids.Length = total + return +} diff --git a/actions/index.go b/actions/index.go index c89f23b..a7a8f97 100644 --- a/actions/index.go +++ b/actions/index.go @@ -28,7 +28,7 @@ type indexHandle struct { category string categoryType string where models.SqlBuilder - orderBy models.SqlBuilder + orderBy string order string join models.SqlBuilder postType []any @@ -51,7 +51,7 @@ func newIndexHandle(ctx *gin.Context) *indexHandle { {"post_type", "in", ""}, {"post_status", "in", ""}, }, - orderBy: models.SqlBuilder{}, + orderBy: "post_date", join: models.SqlBuilder{}, postType: []any{"post"}, status: []any{"publish"}, @@ -68,6 +68,10 @@ func (h *indexHandle) getTitle() string { return h.title } +func (h *indexHandle) getSearchKey() string { + return fmt.Sprintf("action:%s|%s|%s|%s|%s|%d|%d", h.search, h.orderBy, h.order, h.category, h.categoryType, h.page, h.pageSize) +} + func (h *indexHandle) parseParams() { h.order = h.c.Query("order") if !helper.IsContainInArr(h.order, []string{"asc", "desc"}) { @@ -177,7 +181,7 @@ func Index(c *gin.Context) { return } } else { - postIds, totalRaw, err = models.SimplePagination[models.WpPosts](h.where, "ID", "", h.page, h.pageSize, h.orderBy, h.join, h.postType, h.status) + postIds, totalRaw, err = common.SearchPostIds(c, h.getSearchKey(), h.where, h.page, h.pageSize, models.SqlBuilder{{h.orderBy, h.order}}, h.join, h.postType, h.status) } defer func() { @@ -198,7 +202,7 @@ func Index(c *gin.Context) { pw := h.session.Get("post_password") plug := plugins.NewPostPlugin(c, h.scene) for i, v := range postIds { - post := common.GetPost(v.Id) + post := common.GetPostById(v.Id) postIds[i] = post common.PasswordProjectTitle(&postIds[i]) if post.PostPassword != "" && pw != post.PostPassword {