文章列表缓存

This commit is contained in:
xing 2022-09-28 16:23:20 +08:00
parent fa7fc10a6c
commit 247593cb57
3 changed files with 45 additions and 12 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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 {