optimize cache add set cache function
This commit is contained in:
parent
7bbc961f72
commit
547d8e59e6
|
@ -1,8 +1,8 @@
|
|||
FROM golang:latest as gobulidIso
|
||||
FROM golang:1.21.4-alpine as gobulidIso
|
||||
COPY ./ /go/src/wp-go
|
||||
WORKDIR /go/src/wp-go
|
||||
ENV GOPROXY="https://goproxy.cn"
|
||||
RUN go build -ldflags "-w" -tags netgo -o wp-go internal/cmd/main.go
|
||||
#ENV GOPROXY="https://goproxy.cn"
|
||||
RUN go build -ldflags "-w" -tags netgo -o wp-go app/cmd/main.go
|
||||
|
||||
FROM alpine:latest
|
||||
WORKDIR /opt/wp-go
|
||||
|
|
|
@ -57,7 +57,7 @@ func PostFeed(c *gin.Context) {
|
|||
c.Status(http.StatusNotModified)
|
||||
return
|
||||
}
|
||||
s, err := postFeed.GetCache(c, id, time.Second, c, id)
|
||||
s, err := postFeed.GetCache(c, id, time.Second)
|
||||
if err != nil {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
c.Abort()
|
||||
|
|
64
app/pkg/cache/cache.go
vendored
64
app/pkg/cache/cache.go
vendored
|
@ -6,82 +6,56 @@ import (
|
|||
"github.com/fthvgb1/wp-go/app/pkg/dao"
|
||||
"github.com/fthvgb1/wp-go/app/pkg/logs"
|
||||
"github.com/fthvgb1/wp-go/app/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/cache"
|
||||
"github.com/fthvgb1/wp-go/cache/cachemanager"
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
"github.com/fthvgb1/wp-go/safety"
|
||||
"time"
|
||||
)
|
||||
|
||||
var postContextCache *cache.MapCache[uint64, dao.PostContext]
|
||||
var categoryAndTagsCaches *cache.MapCache[string, []models.TermsMy]
|
||||
var recentPostsCaches *cache.VarCache[[]models.Posts]
|
||||
var recentCommentsCaches *cache.VarCache[[]models.Comments]
|
||||
var postCommentCaches *cache.MapCache[uint64, []uint64]
|
||||
var postsCache *cache.MapCache[uint64, models.Posts]
|
||||
|
||||
var postMetaCache *cache.MapCache[uint64, map[string]any]
|
||||
|
||||
var monthPostsCache *cache.MapCache[string, []uint64]
|
||||
var postListIdsCache *cache.MapCache[string, dao.PostIds]
|
||||
var searchPostIdsCache *cache.MapCache[string, dao.PostIds]
|
||||
var maxPostIdCache *cache.VarCache[uint64]
|
||||
|
||||
var usersNameCache *cache.MapCache[string, models.Users]
|
||||
var feedCache *cache.VarCache[[]string]
|
||||
|
||||
var postFeedCache *cache.MapCache[string, string]
|
||||
|
||||
var commentsFeedCache *cache.VarCache[[]string]
|
||||
|
||||
var newCommentCache *cache.MapCache[string, string]
|
||||
|
||||
var allUsernameCache *cache.VarCache[map[string]struct{}]
|
||||
|
||||
func InitActionsCommonCache() {
|
||||
c := config.GetConfig()
|
||||
|
||||
searchPostIdsCache = cachemanager.NewMemoryMapCache(nil, dao.SearchPostIds, c.CacheTime.SearchPostCacheTime, "searchPostIds", func() time.Duration {
|
||||
cachemanager.NewMemoryMapCache(nil, dao.SearchPostIds, c.CacheTime.SearchPostCacheTime, "searchPostIds", func() time.Duration {
|
||||
return config.GetConfig().CacheTime.SearchPostCacheTime
|
||||
})
|
||||
|
||||
postListIdsCache = cachemanager.NewMemoryMapCache(nil, dao.SearchPostIds, c.CacheTime.PostListCacheTime, "listPostIds", func() time.Duration {
|
||||
cachemanager.NewMemoryMapCache(nil, dao.SearchPostIds, c.CacheTime.PostListCacheTime, "listPostIds", func() time.Duration {
|
||||
return config.GetConfig().CacheTime.PostListCacheTime
|
||||
})
|
||||
|
||||
monthPostsCache = cachemanager.NewMemoryMapCache(nil, dao.MonthPost, c.CacheTime.MonthPostCacheTime, "monthPostIds", func() time.Duration {
|
||||
cachemanager.NewMemoryMapCache(nil, dao.MonthPost, c.CacheTime.MonthPostCacheTime, "monthPostIds", func() time.Duration {
|
||||
return config.GetConfig().CacheTime.MonthPostCacheTime
|
||||
})
|
||||
|
||||
postContextCache = cachemanager.NewMemoryMapCache(nil, dao.GetPostContext, c.CacheTime.ContextPostCacheTime, "postContext", func() time.Duration {
|
||||
cachemanager.NewMemoryMapCache(nil, dao.GetPostContext, c.CacheTime.ContextPostCacheTime, "postContext", func() time.Duration {
|
||||
return config.GetConfig().CacheTime.ContextPostCacheTime
|
||||
})
|
||||
|
||||
postsCache = cachemanager.NewMemoryMapCache(dao.GetPostsByIds, nil, c.CacheTime.PostDataCacheTime, "postData", func() time.Duration {
|
||||
cachemanager.NewMemoryMapCache(dao.GetPostsByIds, nil, c.CacheTime.PostDataCacheTime, "postData", func() time.Duration {
|
||||
return config.GetConfig().CacheTime.PostDataCacheTime
|
||||
})
|
||||
|
||||
postMetaCache = cachemanager.NewMemoryMapCache(dao.GetPostMetaByPostIds, nil, c.CacheTime.PostDataCacheTime, "postMetaData", func() time.Duration {
|
||||
cachemanager.NewMemoryMapCache(dao.GetPostMetaByPostIds, nil, c.CacheTime.PostDataCacheTime, "postMetaData", func() time.Duration {
|
||||
return config.GetConfig().CacheTime.PostDataCacheTime
|
||||
})
|
||||
|
||||
categoryAndTagsCaches = cachemanager.NewMemoryMapCache(nil, dao.CategoriesAndTags, c.CacheTime.CategoryCacheTime, "categoryAndTagsData", func() time.Duration {
|
||||
cachemanager.NewMemoryMapCache(nil, dao.CategoriesAndTags, c.CacheTime.CategoryCacheTime, "categoryAndTagsData", func() time.Duration {
|
||||
return config.GetConfig().CacheTime.CategoryCacheTime
|
||||
})
|
||||
|
||||
recentPostsCaches = cachemanager.NewVarMemoryCache(dao.RecentPosts, c.CacheTime.RecentPostCacheTime, "recentPosts", func() time.Duration {
|
||||
cachemanager.NewVarMemoryCache(dao.RecentPosts, c.CacheTime.RecentPostCacheTime, "recentPosts", func() time.Duration {
|
||||
return config.GetConfig().CacheTime.RecentPostCacheTime
|
||||
})
|
||||
|
||||
recentCommentsCaches = cachemanager.NewVarMemoryCache(dao.RecentComments, c.CacheTime.RecentCommentsCacheTime, "recentComments", func() time.Duration {
|
||||
cachemanager.NewVarMemoryCache(dao.RecentComments, c.CacheTime.RecentCommentsCacheTime, "recentComments", func() time.Duration {
|
||||
return config.GetConfig().CacheTime.RecentCommentsCacheTime
|
||||
})
|
||||
|
||||
postCommentCaches = cachemanager.NewMemoryMapCache(nil, dao.PostComments, c.CacheTime.PostCommentsCacheTime, "postCommentIds", func() time.Duration {
|
||||
cachemanager.NewMemoryMapCache(nil, dao.PostComments, c.CacheTime.PostCommentsCacheTime, "postCommentIds", func() time.Duration {
|
||||
return config.GetConfig().CacheTime.PostCommentsCacheTime
|
||||
})
|
||||
|
||||
maxPostIdCache = cachemanager.NewVarMemoryCache(dao.GetMaxPostId, c.CacheTime.MaxPostIdCacheTime, "maxPostId", func() time.Duration {
|
||||
cachemanager.NewVarMemoryCache(dao.GetMaxPostId, c.CacheTime.MaxPostIdCacheTime, "maxPostId", func() time.Duration {
|
||||
return config.GetConfig().CacheTime.MaxPostIdCacheTime
|
||||
})
|
||||
|
||||
|
@ -89,7 +63,7 @@ func InitActionsCommonCache() {
|
|||
return config.GetConfig().CacheTime.UserInfoCacheTime
|
||||
})
|
||||
|
||||
usersNameCache = cachemanager.NewMemoryMapCache(nil, dao.GetUserByName, c.CacheTime.UserInfoCacheTime, "usernameMapToUserData", func() time.Duration {
|
||||
cachemanager.NewMemoryMapCache(nil, dao.GetUserByName, c.CacheTime.UserInfoCacheTime, "usernameMapToUserData", func() time.Duration {
|
||||
return config.GetConfig().CacheTime.UserInfoCacheTime
|
||||
})
|
||||
|
||||
|
@ -97,17 +71,17 @@ func InitActionsCommonCache() {
|
|||
return config.GetConfig().CacheTime.CommentsCacheTime
|
||||
})
|
||||
|
||||
allUsernameCache = cachemanager.NewVarMemoryCache(dao.AllUsername, c.CacheTime.UserInfoCacheTime, "allUsername", func() time.Duration {
|
||||
cachemanager.NewVarMemoryCache(dao.AllUsername, c.CacheTime.UserInfoCacheTime, "allUsername", func() time.Duration {
|
||||
return config.GetConfig().CacheTime.UserInfoCacheTime
|
||||
})
|
||||
|
||||
feedCache = cachemanager.NewVarMemoryCache(feed, time.Hour, "feed")
|
||||
cachemanager.NewVarMemoryCache(feed, time.Hour, "feed")
|
||||
|
||||
postFeedCache = cachemanager.NewMemoryMapCache(nil, postFeed, time.Hour, "postFeed")
|
||||
cachemanager.NewMemoryMapCache(nil, postFeed, time.Hour, "postFeed")
|
||||
|
||||
commentsFeedCache = cachemanager.NewVarMemoryCache(commentsFeed, time.Hour, "commentsFeed")
|
||||
cachemanager.NewVarMemoryCache(commentsFeed, time.Hour, "commentsFeed")
|
||||
|
||||
newCommentCache = cachemanager.NewMemoryMapCache[string, string](nil, nil, 15*time.Minute, "NewComment")
|
||||
cachemanager.NewMemoryMapCache[string, string](nil, nil, 15*time.Minute, "NewComment")
|
||||
|
||||
InitFeed()
|
||||
}
|
||||
|
@ -149,7 +123,7 @@ func CategoriesTags(ctx context.Context, t ...string) []models.TermsMy {
|
|||
if len(t) > 0 {
|
||||
tt = t[0]
|
||||
}
|
||||
r, err := categoryAndTagsCaches.GetCache(ctx, tt, time.Second, ctx, tt)
|
||||
r, err := cachemanager.Get[[]models.TermsMy]("categoryAndTagsData", ctx, tt, time.Second)
|
||||
logs.IfError(err, "get category fail")
|
||||
return r
|
||||
}
|
||||
|
@ -158,7 +132,7 @@ func AllCategoryTagsNames(ctx context.Context, t ...string) map[string]struct{}
|
|||
if len(t) > 0 {
|
||||
tt = t[0]
|
||||
}
|
||||
r, err := categoryAndTagsCaches.GetCache(ctx, tt, time.Second, ctx, tt)
|
||||
r, err := cachemanager.Get[[]models.TermsMy]("categoryAndTagsData", ctx, tt, time.Second)
|
||||
if err != nil {
|
||||
logs.Error(err, "get category fail")
|
||||
return nil
|
||||
|
|
7
app/pkg/cache/comments.go
vendored
7
app/pkg/cache/comments.go
vendored
|
@ -12,7 +12,7 @@ import (
|
|||
|
||||
func RecentComments(ctx context.Context, n int) (r []models.Comments) {
|
||||
nn := number.Max(n, 10)
|
||||
r, err := recentCommentsCaches.GetCache(ctx, time.Second, ctx, nn)
|
||||
r, err := cachemanager.GetVarVal[[]models.Comments]("recentComments", ctx, time.Second, ctx, nn)
|
||||
if len(r) > n {
|
||||
r = r[0:n]
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ func RecentComments(ctx context.Context, n int) (r []models.Comments) {
|
|||
}
|
||||
|
||||
func PostComments(ctx context.Context, Id uint64) ([]models.Comments, error) {
|
||||
ids, err := postCommentCaches.GetCache(ctx, Id, time.Second, ctx, Id)
|
||||
ids, err := cachemanager.Get[[]uint64]("postCommentIds", ctx, Id, time.Second)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -37,5 +37,6 @@ func GetCommentByIds(ctx context.Context, ids []uint64) ([]models.Comments, erro
|
|||
}
|
||||
|
||||
func NewCommentCache() *cache.MapCache[string, string] {
|
||||
return newCommentCache
|
||||
r, _ := cachemanager.GetMapCache[string, string]("NewComment")
|
||||
return r
|
||||
}
|
||||
|
|
13
app/pkg/cache/feed.go
vendored
13
app/pkg/cache/feed.go
vendored
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/fthvgb1/wp-go/app/plugins/wpposts"
|
||||
"github.com/fthvgb1/wp-go/app/wpconfig"
|
||||
"github.com/fthvgb1/wp-go/cache"
|
||||
"github.com/fthvgb1/wp-go/cache/cachemanager"
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||
"github.com/fthvgb1/wp-go/plugin/digest"
|
||||
|
@ -34,15 +35,18 @@ func InitFeed() {
|
|||
}
|
||||
|
||||
func CommentsFeedCache() *cache.VarCache[[]string] {
|
||||
return commentsFeedCache
|
||||
r, _ := cachemanager.GetVarCache[[]string]("commentsFeed")
|
||||
return r
|
||||
}
|
||||
|
||||
func FeedCache() *cache.VarCache[[]string] {
|
||||
return feedCache
|
||||
r, _ := cachemanager.GetVarCache[[]string]("feed")
|
||||
return r
|
||||
}
|
||||
|
||||
func PostFeedCache() *cache.MapCache[string, string] {
|
||||
return postFeedCache
|
||||
r, _ := cachemanager.GetMapCache[string, string]("postFeed")
|
||||
return r
|
||||
}
|
||||
|
||||
func feed(c context.Context, _ ...any) (xml []string, err error) {
|
||||
|
@ -91,8 +95,7 @@ func feed(c context.Context, _ ...any) (xml []string, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func postFeed(c context.Context, id string, arg ...any) (x string, err error) {
|
||||
id = arg[1].(string)
|
||||
func postFeed(c context.Context, id string, _ ...any) (x string, err error) {
|
||||
ID := str.ToInteger[uint64](id, 0)
|
||||
maxId, err := GetMaxPostId(c)
|
||||
logs.IfError(err, "get max post id")
|
||||
|
|
11
app/pkg/cache/postmeta.go
vendored
11
app/pkg/cache/postmeta.go
vendored
|
@ -2,14 +2,13 @@ package cache
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/fthvgb1/wp-go/cache/cachemanager"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GetPostMetaByPostIds(ctx context.Context, ids []uint64) (r []map[string]any, err error) {
|
||||
r, err = postMetaCache.GetCacheBatch(ctx, ids, time.Second, ctx, ids)
|
||||
return
|
||||
func GetPostMetaByPostIds(ctx context.Context, ids []uint64) ([]map[string]any, error) {
|
||||
return cachemanager.GetMultiple[map[string]any]("postMetaData", ctx, ids, time.Second)
|
||||
}
|
||||
func GetPostMetaByPostId(ctx context.Context, id uint64) (r map[string]any, err error) {
|
||||
r, err = postMetaCache.GetCache(ctx, id, time.Second, ctx, id)
|
||||
return
|
||||
func GetPostMetaByPostId(ctx context.Context, id uint64) (map[string]any, error) {
|
||||
return cachemanager.Get[map[string]any]("postMetaData", ctx, id, time.Second)
|
||||
}
|
||||
|
|
20
app/pkg/cache/posts.go
vendored
20
app/pkg/cache/posts.go
vendored
|
@ -3,9 +3,11 @@ package cache
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/fthvgb1/wp-go/app/pkg/dao"
|
||||
"github.com/fthvgb1/wp-go/app/pkg/logs"
|
||||
"github.com/fthvgb1/wp-go/app/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/app/wpconfig"
|
||||
"github.com/fthvgb1/wp-go/cache/cachemanager"
|
||||
"github.com/fthvgb1/wp-go/helper/number"
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||
|
@ -13,17 +15,15 @@ import (
|
|||
)
|
||||
|
||||
func GetPostById(ctx context.Context, id uint64) (models.Posts, error) {
|
||||
//return cachemanager.Get[models.Posts]("postData", ctx, id, time.Second)
|
||||
return postsCache.GetCache(ctx, id, time.Second)
|
||||
return cachemanager.Get[models.Posts]("postData", ctx, id, time.Second)
|
||||
}
|
||||
|
||||
func GetPostsByIds(ctx context.Context, ids []uint64) ([]models.Posts, error) {
|
||||
//return cachemanager.GetMultiple[models.Posts]("postData", ctx, ids, time.Second)
|
||||
return postsCache.GetCacheBatch(ctx, ids, time.Second)
|
||||
return cachemanager.GetMultiple[models.Posts]("postData", ctx, ids, time.Second)
|
||||
}
|
||||
|
||||
func SearchPost(ctx context.Context, key string, args ...any) (r []models.Posts, total int, err error) {
|
||||
ids, err := searchPostIdsCache.GetCache(ctx, key, time.Second, args...)
|
||||
ids, err := cachemanager.Get[dao.PostIds]("searchPostIds", ctx, key, time.Second, args...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ func SearchPost(ctx context.Context, key string, args ...any) (r []models.Posts,
|
|||
}
|
||||
|
||||
func PostLists(ctx context.Context, key string, args ...any) (r []models.Posts, total int, err error) {
|
||||
ids, err := postListIdsCache.GetCache(ctx, key, time.Second, args...)
|
||||
ids, err := cachemanager.Get[dao.PostIds]("listPostIds", ctx, key, time.Second, args...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -43,14 +43,14 @@ func PostLists(ctx context.Context, key string, args ...any) (r []models.Posts,
|
|||
}
|
||||
|
||||
func GetMaxPostId(ctx context.Context) (uint64, error) {
|
||||
return maxPostIdCache.GetCache(ctx, time.Second, ctx)
|
||||
return cachemanager.GetVarVal[uint64]("maxPostId", ctx, time.Second)
|
||||
}
|
||||
|
||||
func RecentPosts(ctx context.Context, n int) (r []models.Posts) {
|
||||
nn := n
|
||||
feedNum := str.ToInteger(wpconfig.GetOption("posts_per_rss"), 10)
|
||||
nn = number.Max(n, feedNum)
|
||||
r, err := recentPostsCaches.GetCache(ctx, time.Second, ctx, nn)
|
||||
r, err := cachemanager.GetVarVal[[]models.Posts]("recentPosts", ctx, time.Second, nn)
|
||||
if n < len(r) {
|
||||
r = r[:n]
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ func RecentPosts(ctx context.Context, n int) (r []models.Posts) {
|
|||
}
|
||||
|
||||
func GetContextPost(ctx context.Context, id uint64, date time.Time) (prev, next models.Posts, err error) {
|
||||
postCtx, err := postContextCache.GetCache(ctx, id, time.Second, ctx, date)
|
||||
postCtx, err := cachemanager.Get[dao.PostContext]("postContext", ctx, id, time.Second, date)
|
||||
if err != nil {
|
||||
return models.Posts{}, models.Posts{}, err
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ func GetContextPost(ctx context.Context, id uint64, date time.Time) (prev, next
|
|||
}
|
||||
|
||||
func GetMonthPostIds(ctx context.Context, year, month string, page, limit int, order string) (r []models.Posts, total int, err error) {
|
||||
res, err := monthPostsCache.GetCache(ctx, fmt.Sprintf("%s%s", year, month), time.Second, ctx, year, month)
|
||||
res, err := cachemanager.Get[[]uint64]("monthPostIds", ctx, fmt.Sprintf("%s%s", year, month), time.Second, year, month)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
12
app/pkg/cache/users.go
vendored
12
app/pkg/cache/users.go
vendored
|
@ -5,23 +5,15 @@ import (
|
|||
"github.com/fthvgb1/wp-go/app/pkg/logs"
|
||||
"github.com/fthvgb1/wp-go/app/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/cache/cachemanager"
|
||||
"github.com/fthvgb1/wp-go/model"
|
||||
"time"
|
||||
)
|
||||
|
||||
func getUserById(a ...any) (r models.Users, err error) {
|
||||
ctx := a[0].(context.Context)
|
||||
uid := a[1].(uint64)
|
||||
r, err = model.FindOneById[models.Users](ctx, uid)
|
||||
return
|
||||
}
|
||||
|
||||
func GetUserByName(ctx context.Context, username string) (models.Users, error) {
|
||||
return usersNameCache.GetCache(ctx, username, time.Second, ctx, username)
|
||||
return cachemanager.Get[models.Users]("usernameMapToUserData", ctx, username, time.Second)
|
||||
}
|
||||
|
||||
func GetAllUsername(ctx context.Context) (map[string]struct{}, error) {
|
||||
return allUsernameCache.GetCache(ctx, time.Second, ctx)
|
||||
return cachemanager.GetVarVal[map[string]struct{}]("allUsername", ctx, time.Second)
|
||||
}
|
||||
|
||||
func GetUserById(ctx context.Context, uid uint64) models.Users {
|
||||
|
|
|
@ -99,9 +99,9 @@ func GetPostsByIds(ctx context.Context, ids []uint64, _ ...any) (m map[uint64]mo
|
|||
}
|
||||
|
||||
func SearchPostIds(ctx context.Context, _ string, args ...any) (ids PostIds, err error) {
|
||||
q := args[1].(*model.QueryCondition)
|
||||
page := args[2].(int)
|
||||
pageSize := args[3].(int)
|
||||
q := args[0].(*model.QueryCondition)
|
||||
page := args[1].(int)
|
||||
pageSize := args[2].(int)
|
||||
q.Fields = "ID"
|
||||
res, total, err := model.Pagination[models.Posts](ctx, q, page, pageSize)
|
||||
for _, posts := range res {
|
||||
|
@ -116,7 +116,7 @@ func SearchPostIds(ctx context.Context, _ string, args ...any) (ids PostIds, err
|
|||
return
|
||||
}
|
||||
|
||||
func GetMaxPostId(ctx context.Context, a ...any) (uint64, error) {
|
||||
func GetMaxPostId(ctx context.Context, _ ...any) (uint64, error) {
|
||||
r, err := model.SimpleFind[models.Posts](ctx,
|
||||
model.SqlBuilder{{"post_type", "post"}, {"post_status", "publish"}},
|
||||
"max(ID) ID",
|
||||
|
@ -129,7 +129,7 @@ func GetMaxPostId(ctx context.Context, a ...any) (uint64, error) {
|
|||
}
|
||||
|
||||
func RecentPosts(ctx context.Context, a ...any) (r []models.Posts, err error) {
|
||||
num := a[1].(int)
|
||||
num := helper.ParseArgs(10, a...)
|
||||
r, err = model.Finds[models.Posts](ctx, model.Conditions(
|
||||
model.Where(model.SqlBuilder{
|
||||
{"post_type", "post"},
|
||||
|
@ -143,7 +143,7 @@ func RecentPosts(ctx context.Context, a ...any) (r []models.Posts, err error) {
|
|||
}
|
||||
|
||||
func GetPostContext(ctx context.Context, _ uint64, arg ...any) (r PostContext, err error) {
|
||||
t := arg[1].(time.Time)
|
||||
t := arg[0].(time.Time)
|
||||
next, err := model.FirstOne[models.Posts](ctx, model.SqlBuilder{
|
||||
{"post_date", ">", t.Format("2006-01-02 15:04:05")},
|
||||
{"post_status", "in", ""},
|
||||
|
@ -174,7 +174,7 @@ func GetPostContext(ctx context.Context, _ uint64, arg ...any) (r PostContext, e
|
|||
}
|
||||
|
||||
func MonthPost(ctx context.Context, _ string, args ...any) (r []uint64, err error) {
|
||||
year, month := args[1].(string), args[2].(string)
|
||||
year, month := args[0].(string), args[1].(string)
|
||||
where := model.SqlBuilder{
|
||||
{"post_type", "post"},
|
||||
{"post_status", "publish"},
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"fmt"
|
||||
"github.com/fthvgb1/wp-go/app/pkg/config"
|
||||
"github.com/fthvgb1/wp-go/app/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/cache"
|
||||
"github.com/fthvgb1/wp-go/cache/cachemanager"
|
||||
"github.com/fthvgb1/wp-go/helper"
|
||||
"github.com/fthvgb1/wp-go/plugin/digest"
|
||||
|
@ -15,14 +14,12 @@ import (
|
|||
"unicode/utf8"
|
||||
)
|
||||
|
||||
var digestCache *cache.MapCache[uint64, string]
|
||||
|
||||
var more = regexp.MustCompile("<!--more(.*?)?-->")
|
||||
|
||||
var removeWpBlock = regexp.MustCompile("<!-- /?wp:.*-->")
|
||||
|
||||
func InitDigestCache() {
|
||||
digestCache = cachemanager.NewMemoryMapCache(nil, digestRaw, config.GetConfig().CacheTime.DigestCacheTime, "digestPlugin", func() time.Duration {
|
||||
cachemanager.NewMemoryMapCache(nil, digestRaw, config.GetConfig().CacheTime.DigestCacheTime, "digestPlugin", func() time.Duration {
|
||||
return config.GetConfig().CacheTime.DigestCacheTime
|
||||
})
|
||||
}
|
||||
|
@ -74,7 +71,7 @@ func PostsMore(id uint64, content, closeTag string) string {
|
|||
}
|
||||
|
||||
func Digest(ctx context.Context, post *models.Posts, limit int) {
|
||||
content, _ := digestCache.GetCache(ctx, post.Id, time.Second, ctx, post.PostContent, post.Id, limit)
|
||||
content, _ := cachemanager.Get[string]("digestPlugin", ctx, post.Id, time.Second, ctx, post.PostContent, post.Id, limit)
|
||||
post.PostContent = content
|
||||
}
|
||||
|
||||
|
|
|
@ -89,14 +89,14 @@ func (i *IndexHandle) GetIndexData() (posts []models.Posts, totalRaw int, err er
|
|||
switch i.scene {
|
||||
case constraints.Home, constraints.Category, constraints.Tag, constraints.Author:
|
||||
|
||||
posts, totalRaw, err = cache.PostLists(i.C, i.Param.CacheKey, i.C, q, i.Param.Page, i.Param.PageSize)
|
||||
posts, totalRaw, err = cache.PostLists(i.C, i.Param.CacheKey, q, i.Param.Page, i.Param.PageSize)
|
||||
if i.scene == constraints.Home && i.Param.Page == 1 {
|
||||
i.MarkSticky(&posts)
|
||||
}
|
||||
|
||||
case constraints.Search:
|
||||
|
||||
posts, totalRaw, err = cache.SearchPost(i.C, i.Param.CacheKey, i.C, q, i.Param.Page, i.Param.PageSize)
|
||||
posts, totalRaw, err = cache.SearchPost(i.C, i.Param.CacheKey, q, i.Param.Page, i.Param.PageSize)
|
||||
|
||||
case constraints.Archive:
|
||||
i.ginH["archiveYear"] = i.Param.Year
|
||||
|
|
145
cache/cachemanager/manger.go
vendored
145
cache/cachemanager/manger.go
vendored
|
@ -13,18 +13,41 @@ import (
|
|||
var ctx = context.Background()
|
||||
|
||||
var mapFlush = safety.NewMap[string, func(any)]()
|
||||
var getSingleFn = safety.NewMap[string, func(context.Context, any, time.Duration, ...any) (any, error)]()
|
||||
var getBatchFn = safety.NewMap[string, func(context.Context, any, time.Duration, ...any) (any, error)]()
|
||||
var getBatchToMapFn = safety.NewMap[string, func(context.Context, any, time.Duration, ...any) (any, error)]()
|
||||
var anyFlush = safety.NewMap[string, func()]()
|
||||
|
||||
var getVar = safety.NewMap[string, func(context.Context, time.Duration, ...any) (any, error)]()
|
||||
|
||||
var expiredTime = safety.NewMap[string, expire]()
|
||||
|
||||
var varCache = safety.NewMap[string, any]()
|
||||
var mapCache = safety.NewMap[string, any]()
|
||||
|
||||
func SetVarCache[T any](name string, v *cache.VarCache[T]) error {
|
||||
vv, ok := varCache.Load(name)
|
||||
if !ok {
|
||||
varCache.Store(name, v)
|
||||
return nil
|
||||
}
|
||||
_, ok = vv.(*cache.VarCache[T])
|
||||
if ok {
|
||||
varCache.Store(name, v)
|
||||
return nil
|
||||
}
|
||||
return errors.New(str.Join("cache ", name, " type err"))
|
||||
}
|
||||
|
||||
func SetMapCache[K comparable, V any](name string, ca *cache.MapCache[K, V]) error {
|
||||
v, ok := mapCache.Load(name)
|
||||
if !ok {
|
||||
mapCache.Store(name, ca)
|
||||
return nil
|
||||
}
|
||||
_, ok = v.(*cache.MapCache[K, V])
|
||||
if !ok {
|
||||
return errors.New(str.Join("cache ", name, " type err"))
|
||||
}
|
||||
mapCache.Store(name, ca)
|
||||
return nil
|
||||
}
|
||||
|
||||
type expire struct {
|
||||
fn func() time.Duration
|
||||
p *safety.Var[time.Duration]
|
||||
|
@ -66,8 +89,7 @@ func FlushAnyVal(name ...string) {
|
|||
}
|
||||
}
|
||||
|
||||
func pushFlushMap[K comparable, V any](m *cache.MapCache[K, V], args ...any) {
|
||||
name, _ := parseArgs(args...)
|
||||
func PushMangerMap[K comparable, V any](name string, m *cache.MapCache[K, V]) {
|
||||
if name == "" {
|
||||
return
|
||||
}
|
||||
|
@ -81,70 +103,57 @@ func pushFlushMap[K comparable, V any](m *cache.MapCache[K, V], args ...any) {
|
|||
m.Del(ctx, k...)
|
||||
}
|
||||
})
|
||||
getSingleFn.Store(name, func(ct context.Context, k any, t time.Duration, a ...any) (any, error) {
|
||||
kk, ok := k.(K)
|
||||
if !ok {
|
||||
return nil, errors.New(str.Join("cache ", name, " key type err"))
|
||||
}
|
||||
return m.GetCache(ct, kk, t, a...)
|
||||
})
|
||||
getBatchFn.Store(name, func(ct context.Context, k any, t time.Duration, a ...any) (any, error) {
|
||||
kk, ok := k.([]K)
|
||||
if !ok {
|
||||
return nil, errors.New(str.Join("cache ", name, " key type err"))
|
||||
}
|
||||
return m.GetCacheBatch(ct, kk, t, a...)
|
||||
})
|
||||
getBatchToMapFn.Store(name, func(ct context.Context, k any, t time.Duration, a ...any) (any, error) {
|
||||
kk, ok := k.([]K)
|
||||
if !ok {
|
||||
return nil, errors.New(str.Join("cache ", name, " key type err"))
|
||||
}
|
||||
return m.GetBatchToMap(ct, kk, t, a...)
|
||||
})
|
||||
FlushPush()
|
||||
}
|
||||
|
||||
func Get[T, K any](name string, ct context.Context, key K, timeout time.Duration, params ...any) (r T, err error) {
|
||||
func Get[T any, K comparable](name string, ct context.Context, key K, timeout time.Duration, params ...any) (r T, err error) {
|
||||
ct = context.WithValue(ct, "getCache", name)
|
||||
v, ok := getSingleFn.Load(name)
|
||||
if !ok {
|
||||
err = errors.New(str.Join("cache ", name, " doesn't exist"))
|
||||
return
|
||||
}
|
||||
vv, err := v(ct, key, timeout, params...)
|
||||
ca, err := getMap[K, T](name)
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
r = vv.(T)
|
||||
return
|
||||
}
|
||||
func GetMultiple[T, K any](name string, ct context.Context, key []K, timeout time.Duration, params ...any) (r []T, err error) {
|
||||
ct = context.WithValue(ct, "getCache", name)
|
||||
v, ok := getBatchFn.Load(name)
|
||||
if !ok {
|
||||
err = errors.New(str.Join("cache ", name, " doesn't exist"))
|
||||
return
|
||||
}
|
||||
vv, err := v(ct, key, timeout, params...)
|
||||
vv, err := ca.GetCache(ct, key, timeout, params...)
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
r = vv.([]T)
|
||||
r = vv
|
||||
return
|
||||
}
|
||||
|
||||
func getMap[K comparable, T any](name string) (*cache.MapCache[K, T], error) {
|
||||
m, ok := mapCache.Load(name)
|
||||
if !ok {
|
||||
return nil, errors.New(str.Join("cache ", name, " doesn't exist"))
|
||||
}
|
||||
vk, ok := m.(*cache.MapCache[K, T])
|
||||
if !ok {
|
||||
return nil, errors.New(str.Join("cache ", name, " type error"))
|
||||
}
|
||||
return vk, nil
|
||||
}
|
||||
func GetMultiple[T any, K comparable](name string, ct context.Context, key []K, timeout time.Duration, params ...any) (r []T, err error) {
|
||||
ct = context.WithValue(ct, "getCache", name)
|
||||
ca, err := getMap[K, T](name)
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
vv, err := ca.GetCacheBatch(ct, key, timeout, params...)
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
r = vv
|
||||
return
|
||||
}
|
||||
func GetMultipleToMap[T any, K comparable](name string, ct context.Context, key []K, timeout time.Duration, params ...any) (r map[K]T, err error) {
|
||||
ct = context.WithValue(ct, "getCache", name)
|
||||
v, ok := getBatchToMapFn.Load(name)
|
||||
if !ok {
|
||||
err = errors.New(str.Join("cache ", name, " doesn't exist"))
|
||||
return
|
||||
}
|
||||
vv, err := v(ct, key, timeout, params...)
|
||||
ca, err := getMap[K, T](name)
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
r = vv.(map[K]T)
|
||||
vv, err := ca.GetBatchToMap(ct, key, timeout, params...)
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
r = vv
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -168,10 +177,12 @@ func parseArgs(args ...any) (string, func() time.Duration) {
|
|||
|
||||
func NewMapCache[K comparable, V any](data cache.Cache[K, V], batchFn cache.MapBatchFn[K, V], fn cache.MapSingleFn[K, V], args ...any) *cache.MapCache[K, V] {
|
||||
m := cache.NewMapCache[K, V](data, fn, batchFn)
|
||||
pushFlushMap(m, args...)
|
||||
FlushPush(m)
|
||||
ClearPush(m)
|
||||
name, f := parseArgs(args...)
|
||||
if name != "" {
|
||||
PushMangerMap(name, m)
|
||||
}
|
||||
if f != nil && name != "" {
|
||||
SetExpireTime(any(data).(cache.SetTime), name, 0, f)
|
||||
}
|
||||
|
@ -249,9 +260,6 @@ func NewVarCache[T any](c cache.AnyCache[T], fn func(context.Context, ...any) (T
|
|||
name, _ := parseArgs(a...)
|
||||
if name != "" {
|
||||
varCache.Store(name, v)
|
||||
getVar.Store(name, func(c context.Context, duration time.Duration, a ...any) (any, error) {
|
||||
return v.GetCache(c, duration, a...)
|
||||
})
|
||||
}
|
||||
cc, ok := any(c).(clearExpired)
|
||||
if ok {
|
||||
|
@ -262,21 +270,16 @@ func NewVarCache[T any](c cache.AnyCache[T], fn func(context.Context, ...any) (T
|
|||
|
||||
func GetVarVal[T any](name string, ctx context.Context, duration time.Duration, a ...any) (r T, err error) {
|
||||
ctx = context.WithValue(ctx, "getCache", name)
|
||||
fn, ok := getVar.Load(name)
|
||||
ca, ok := GetVarCache[T](name)
|
||||
if !ok {
|
||||
err = errors.New(str.Join("cache ", name, " is not exist"))
|
||||
return
|
||||
}
|
||||
v, err := fn(ctx, duration, a...)
|
||||
v, err := ca.GetCache(ctx, duration, a...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
vv, ok := v.(T)
|
||||
if !ok {
|
||||
err = errors.New(str.Join("cache ", name, " value wanted can't match got"))
|
||||
return
|
||||
}
|
||||
r = vv
|
||||
r = v
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -298,10 +301,6 @@ func GetVarCache[T any](name string) (*cache.VarCache[T], bool) {
|
|||
}
|
||||
|
||||
func GetMapCache[K comparable, V any](name string) (*cache.MapCache[K, V], bool) {
|
||||
v, ok := mapCache.Load(name)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
vv, ok := v.(*cache.MapCache[K, V])
|
||||
return vv, ok
|
||||
vv, err := getMap[K, V](name)
|
||||
return vv, err != nil
|
||||
}
|
||||
|
|
99
cache/cachemanager/manger_test.go
vendored
99
cache/cachemanager/manger_test.go
vendored
|
@ -8,6 +8,7 @@ import (
|
|||
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||
"github.com/fthvgb1/wp-go/taskPools"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
@ -90,3 +91,101 @@ func TestSetExpireTime(t *testing.T) {
|
|||
fmt.Println(reflect.DeepEqual(v, vv))
|
||||
})
|
||||
}
|
||||
|
||||
func TestSetMapCache(t *testing.T) {
|
||||
t.Run("t1", func(t *testing.T) {
|
||||
NewMemoryMapCache(nil, func(ctx2 context.Context, k string, a ...any) (string, error) {
|
||||
fmt.Println("memory cache")
|
||||
return strings.Repeat(k, 2), nil
|
||||
}, time.Hour, "test")
|
||||
fmt.Println(Get[string]("test", ctx, "test", time.Second))
|
||||
|
||||
cc := NewMapCache[string, string](xx[string, string]{m: map[string]string{}}, nil, func(ctx2 context.Context, k string, a ...any) (string, error) {
|
||||
fmt.Println("other cache drives. eg: redis,file.....")
|
||||
return strings.Repeat(k, 2), nil
|
||||
}, "kkk", time.Hour)
|
||||
|
||||
if err := SetMapCache("test", cc); err != nil {
|
||||
t.Errorf("SetMapCache() error = %v, wantErr %v", err, nil)
|
||||
}
|
||||
fmt.Println(Get[string]("test", ctx, "test", time.Second))
|
||||
})
|
||||
}
|
||||
|
||||
type xx[K comparable, V any] struct {
|
||||
m map[K]V
|
||||
}
|
||||
|
||||
func (x xx[K, V]) Get(ctx context.Context, key K) (V, bool) {
|
||||
v, ok := x.m[key]
|
||||
return v, ok
|
||||
}
|
||||
|
||||
func (x xx[K, V]) Set(ctx context.Context, key K, val V) {
|
||||
x.m[key] = val
|
||||
}
|
||||
|
||||
func (x xx[K, V]) GetExpireTime(ctx context.Context) time.Duration {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (x xx[K, V]) Ttl(ctx context.Context, key K) time.Duration {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (x xx[K, V]) Flush(ctx context.Context) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (x xx[K, V]) Del(ctx context.Context, key ...K) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (x xx[K, V]) ClearExpired(ctx context.Context) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func TestSetVarCache(t *testing.T) {
|
||||
t.Run("t1", func(t *testing.T) {
|
||||
NewVarMemoryCache(func(ctx2 context.Context, a ...any) (string, error) {
|
||||
fmt.Println("memory cache")
|
||||
return "xxx", nil
|
||||
}, time.Hour, "test")
|
||||
fmt.Println(GetVarVal[string]("test", ctx, time.Second))
|
||||
o := NewVarCache[string](oo[string]{}, func(ctx2 context.Context, a ...any) (string, error) {
|
||||
fmt.Println("other cache drives. eg: redis,file.....")
|
||||
return "ooo", nil
|
||||
})
|
||||
if err := SetVarCache("test", o); err != nil {
|
||||
t.Errorf("SetVarCache() error = %v, wantErr %v", err, nil)
|
||||
}
|
||||
fmt.Println(GetVarVal[string]("test", ctx, time.Second))
|
||||
})
|
||||
}
|
||||
|
||||
type oo[T any] struct {
|
||||
val T
|
||||
}
|
||||
|
||||
func (o oo[T]) Get(ctx context.Context) (T, bool) {
|
||||
return o.val, false
|
||||
}
|
||||
|
||||
func (o oo[T]) Set(ctx context.Context, v T) {
|
||||
o.val = v
|
||||
}
|
||||
|
||||
func (o oo[T]) Flush(ctx context.Context) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (o oo[T]) GetLastSetTime(ctx context.Context) time.Time {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user