wp-go/app/pkg/cache/posts.go

92 lines
2.9 KiB
Go
Raw Normal View History

2023-01-12 12:42:16 +00:00
package cache
import (
"context"
"fmt"
2023-11-25 09:49:20 +00:00
"github.com/fthvgb1/wp-go/app/pkg/dao"
2023-05-04 12:36:17 +00:00
"github.com/fthvgb1/wp-go/app/pkg/logs"
"github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/app/wpconfig"
2023-11-25 09:49:20 +00:00
"github.com/fthvgb1/wp-go/cache/cachemanager"
2023-03-06 15:43:58 +00:00
"github.com/fthvgb1/wp-go/helper/number"
2023-01-21 11:31:23 +00:00
"github.com/fthvgb1/wp-go/helper/slice"
2023-03-06 15:43:58 +00:00
str "github.com/fthvgb1/wp-go/helper/strings"
2023-01-12 12:42:16 +00:00
"time"
)
// GetPostById query func see dao.GetPostsByIds
2023-01-12 12:42:16 +00:00
func GetPostById(ctx context.Context, id uint64) (models.Posts, error) {
return cachemanager.GetBy[models.Posts]("postData", ctx, id, time.Second)
2023-01-12 12:42:16 +00:00
}
// GetPostsByIds query func see dao.GetPostsByIds
2023-01-12 12:42:16 +00:00
func GetPostsByIds(ctx context.Context, ids []uint64) ([]models.Posts, error) {
return cachemanager.GetBatchBy[models.Posts]("postData", ctx, ids, time.Second)
2023-01-12 12:42:16 +00:00
}
// SearchPost query func see dao.SearchPostIds
2023-01-12 12:42:16 +00:00
func SearchPost(ctx context.Context, key string, args ...any) (r []models.Posts, total int, err error) {
ids, err := cachemanager.GetBy[dao.PostIds]("searchPostIds", ctx, key, time.Second, args...)
2023-01-12 12:42:16 +00:00
if err != nil {
return
}
total = ids.Length
r, err = GetPostsByIds(ctx, ids.Ids)
return
}
// PostLists query func see dao.SearchPostIds
2023-01-12 12:42:16 +00:00
func PostLists(ctx context.Context, key string, args ...any) (r []models.Posts, total int, err error) {
ids, err := cachemanager.GetBy[dao.PostIds]("listPostIds", ctx, key, time.Second, args...)
2023-01-12 12:42:16 +00:00
if err != nil {
return
}
total = ids.Length
r, err = GetPostsByIds(ctx, ids.Ids)
return
}
// GetMaxPostId query func see dao.GetMaxPostId
func GetMaxPostId(ctx context.Context) (uint64, error) {
2023-11-25 09:49:20 +00:00
return cachemanager.GetVarVal[uint64]("maxPostId", ctx, time.Second)
2023-01-12 12:42:16 +00:00
}
// RecentPosts query func see dao.RecentPosts
2023-02-09 07:43:20 +00:00
func RecentPosts(ctx context.Context, n int) (r []models.Posts) {
2023-02-06 12:50:25 +00:00
nn := n
2023-03-06 15:43:58 +00:00
feedNum := str.ToInteger(wpconfig.GetOption("posts_per_rss"), 10)
nn = number.Max(n, feedNum)
2023-11-25 09:49:20 +00:00
r, err := cachemanager.GetVarVal[[]models.Posts]("recentPosts", ctx, time.Second, nn)
2023-01-12 12:42:16 +00:00
if n < len(r) {
r = r[:n]
}
2023-04-07 14:59:07 +00:00
logs.IfError(err, "get recent post")
2023-01-12 12:42:16 +00:00
return
}
// GetContextPost query func see dao.GetPostContext
2023-01-12 12:42:16 +00:00
func GetContextPost(ctx context.Context, id uint64, date time.Time) (prev, next models.Posts, err error) {
postCtx, err := cachemanager.GetBy[dao.PostContext]("postContext", ctx, id, time.Second, date)
2023-01-12 12:42:16 +00:00
if err != nil {
return models.Posts{}, models.Posts{}, err
}
prev = postCtx.Prev
next = postCtx.Next
return
}
// GetMonthPostIds query func see dao.MonthPost
2023-01-12 12:42:16 +00:00
func GetMonthPostIds(ctx context.Context, year, month string, page, limit int, order string) (r []models.Posts, total int, err error) {
res, err := cachemanager.GetBy[[]uint64]("monthPostIds", ctx, fmt.Sprintf("%s%s", year, month), time.Second, year, month)
2023-01-12 12:42:16 +00:00
if err != nil {
return
}
if order == "desc" {
2023-01-21 11:31:23 +00:00
res = slice.Reverse(res)
2023-01-12 12:42:16 +00:00
}
total = len(res)
2023-01-21 11:31:23 +00:00
rr := slice.Pagination(res, page, limit)
2023-01-12 12:42:16 +00:00
r, err = GetPostsByIds(ctx, rr)
return
}