2023-01-12 12:42:16 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-18 15:02:59 +00:00
|
|
|
"github.com/fthvgb1/wp-go/cache"
|
2023-01-21 13:13:33 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
2023-01-18 15:02:59 +00:00
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/dao"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
2023-01-12 12:42:16 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
var postContextCache *cache.MapCache[uint64, dao.PostContext]
|
2023-01-12 12:42:16 +00:00
|
|
|
var archivesCaches *Arch
|
2023-01-21 14:56:41 +00:00
|
|
|
var categoryAndTagsCaches *cache.VarCache[[]models.TermsMy]
|
2023-01-20 10:10:13 +00:00
|
|
|
var recentPostsCaches *cache.VarCache[[]models.Posts]
|
|
|
|
var recentCommentsCaches *cache.VarCache[[]models.Comments]
|
2023-01-12 12:42:16 +00:00
|
|
|
var postCommentCaches *cache.MapCache[uint64, []uint64]
|
2023-01-17 15:18:31 +00:00
|
|
|
var postsCache *cache.MapCache[uint64, models.Posts]
|
2023-01-12 12:42:16 +00:00
|
|
|
|
|
|
|
var postMetaCache *cache.MapCache[uint64, map[string]any]
|
|
|
|
|
|
|
|
var monthPostsCache *cache.MapCache[string, []uint64]
|
2023-01-21 13:13:33 +00:00
|
|
|
var postListIdsCache *cache.MapCache[string, dao.PostIds]
|
|
|
|
var searchPostIdsCache *cache.MapCache[string, dao.PostIds]
|
2023-01-20 10:10:13 +00:00
|
|
|
var maxPostIdCache *cache.VarCache[uint64]
|
2023-01-12 12:42:16 +00:00
|
|
|
|
2023-01-17 15:18:31 +00:00
|
|
|
var usersCache *cache.MapCache[uint64, models.Users]
|
|
|
|
var usersNameCache *cache.MapCache[string, models.Users]
|
|
|
|
var commentsCache *cache.MapCache[uint64, models.Comments]
|
2023-01-12 12:42:16 +00:00
|
|
|
|
2023-01-20 10:10:13 +00:00
|
|
|
var feedCache *cache.VarCache[[]string]
|
2023-01-19 13:02:39 +00:00
|
|
|
|
|
|
|
var postFeedCache *cache.MapCache[string, string]
|
|
|
|
|
2023-01-20 10:10:13 +00:00
|
|
|
var commentsFeedCache *cache.VarCache[[]string]
|
2023-01-19 13:02:39 +00:00
|
|
|
|
|
|
|
var newCommentCache *cache.MapCache[string, string]
|
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
var allUsernameCache *cache.VarCache[map[string]struct{}]
|
|
|
|
|
2023-01-31 16:58:42 +00:00
|
|
|
var headerImagesCache *cache.MapCache[string, []models.PostThumbnail]
|
2023-01-21 13:13:33 +00:00
|
|
|
|
2023-01-12 12:42:16 +00:00
|
|
|
func InitActionsCommonCache() {
|
|
|
|
c := config.Conf.Load()
|
|
|
|
archivesCaches = &Arch{
|
|
|
|
mutex: &sync.Mutex{},
|
2023-01-21 13:13:33 +00:00
|
|
|
setCacheFunc: dao.Archives,
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
searchPostIdsCache = cache.NewMapCacheByFn[string](dao.SearchPostIds, c.SearchPostCacheTime)
|
2023-01-12 12:42:16 +00:00
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
postListIdsCache = cache.NewMapCacheByFn[string](dao.SearchPostIds, c.PostListCacheTime)
|
2023-01-12 12:42:16 +00:00
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
monthPostsCache = cache.NewMapCacheByFn[string](dao.MonthPost, c.MonthPostCacheTime)
|
2023-01-12 12:42:16 +00:00
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
postContextCache = cache.NewMapCacheByFn[uint64](dao.GetPostContext, c.ContextPostCacheTime)
|
2023-01-12 12:42:16 +00:00
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
postsCache = cache.NewMapCacheByBatchFn(dao.GetPostsByIds, c.PostDataCacheTime)
|
2023-01-12 12:42:16 +00:00
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
postMetaCache = cache.NewMapCacheByBatchFn(dao.GetPostMetaByPostIds, c.PostDataCacheTime)
|
2023-01-12 12:42:16 +00:00
|
|
|
|
2023-01-21 14:56:41 +00:00
|
|
|
categoryAndTagsCaches = cache.NewVarCache(dao.CategoriesAndTags, c.CategoryCacheTime)
|
2023-01-12 12:42:16 +00:00
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
recentPostsCaches = cache.NewVarCache(dao.RecentPosts, c.RecentPostCacheTime)
|
2023-01-12 12:42:16 +00:00
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
recentCommentsCaches = cache.NewVarCache(dao.RecentComments, c.RecentCommentsCacheTime)
|
2023-01-12 12:42:16 +00:00
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
postCommentCaches = cache.NewMapCacheByFn[uint64](dao.PostComments, c.PostCommentsCacheTime)
|
2023-01-12 12:42:16 +00:00
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
maxPostIdCache = cache.NewVarCache(dao.GetMaxPostId, c.MaxPostIdCacheTime)
|
2023-01-12 12:42:16 +00:00
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
usersCache = cache.NewMapCacheByFn[uint64](dao.GetUserById, c.UserInfoCacheTime)
|
2023-01-12 12:42:16 +00:00
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
usersNameCache = cache.NewMapCacheByFn[string](dao.GetUserByName, c.UserInfoCacheTime)
|
2023-01-12 12:42:16 +00:00
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
commentsCache = cache.NewMapCacheByBatchFn(dao.GetCommentByIds, c.CommentsCacheTime)
|
2023-01-19 13:02:39 +00:00
|
|
|
|
2023-01-20 10:10:13 +00:00
|
|
|
feedCache = cache.NewVarCache(feed, time.Hour)
|
2023-01-19 13:02:39 +00:00
|
|
|
|
2023-01-20 10:10:13 +00:00
|
|
|
postFeedCache = cache.NewMapCacheByFn[string](postFeed, time.Hour)
|
2023-01-19 13:02:39 +00:00
|
|
|
|
2023-01-20 10:10:13 +00:00
|
|
|
commentsFeedCache = cache.NewVarCache(commentsFeed, time.Hour)
|
2023-01-19 13:02:39 +00:00
|
|
|
|
|
|
|
newCommentCache = cache.NewMapCacheByFn[string, string](nil, 15*time.Minute)
|
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
allUsernameCache = cache.NewVarCache(dao.AllUsername, c.UserInfoCacheTime)
|
|
|
|
|
2023-01-31 16:58:42 +00:00
|
|
|
headerImagesCache = cache.NewMapCacheByFn[string](getHeaderImages, c.ThemeHeaderImagCacheTime)
|
|
|
|
|
2023-01-19 13:02:39 +00:00
|
|
|
InitFeed()
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ClearCache() {
|
|
|
|
searchPostIdsCache.ClearExpired()
|
|
|
|
postsCache.ClearExpired()
|
|
|
|
postMetaCache.ClearExpired()
|
|
|
|
postListIdsCache.ClearExpired()
|
|
|
|
monthPostsCache.ClearExpired()
|
|
|
|
postContextCache.ClearExpired()
|
|
|
|
usersCache.ClearExpired()
|
|
|
|
commentsCache.ClearExpired()
|
|
|
|
usersNameCache.ClearExpired()
|
2023-01-19 13:02:39 +00:00
|
|
|
postFeedCache.ClearExpired()
|
|
|
|
newCommentCache.ClearExpired()
|
2023-01-31 16:58:42 +00:00
|
|
|
headerImagesCache.ClearExpired()
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|
|
|
|
func FlushCache() {
|
|
|
|
searchPostIdsCache.Flush()
|
|
|
|
postsCache.Flush()
|
|
|
|
postMetaCache.Flush()
|
|
|
|
postListIdsCache.Flush()
|
|
|
|
monthPostsCache.Flush()
|
|
|
|
postContextCache.Flush()
|
|
|
|
usersCache.Flush()
|
|
|
|
commentsCache.Flush()
|
|
|
|
usersCache.Flush()
|
2023-01-19 13:02:39 +00:00
|
|
|
postFeedCache.Flush()
|
|
|
|
newCommentCache.Flush()
|
2023-01-31 16:58:42 +00:00
|
|
|
headerImagesCache.Flush()
|
2023-01-12 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 15:18:31 +00:00
|
|
|
func Archives(ctx context.Context) (r []models.PostArchive) {
|
2023-01-12 12:42:16 +00:00
|
|
|
return archivesCaches.getArchiveCache(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Arch struct {
|
2023-01-17 15:18:31 +00:00
|
|
|
data []models.PostArchive
|
2023-01-12 12:42:16 +00:00
|
|
|
mutex *sync.Mutex
|
2023-01-17 15:18:31 +00:00
|
|
|
setCacheFunc func(context.Context) ([]models.PostArchive, error)
|
2023-01-12 12:42:16 +00:00
|
|
|
month time.Month
|
|
|
|
}
|
|
|
|
|
2023-01-17 15:18:31 +00:00
|
|
|
func (c *Arch) getArchiveCache(ctx context.Context) []models.PostArchive {
|
2023-01-12 12:42:16 +00:00
|
|
|
l := len(c.data)
|
|
|
|
m := time.Now().Month()
|
|
|
|
if l > 0 && c.month != m || l < 1 {
|
|
|
|
r, err := c.setCacheFunc(ctx)
|
|
|
|
if err != nil {
|
|
|
|
logs.ErrPrintln(err, "set cache err[%s]")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
c.month = m
|
|
|
|
c.data = r
|
|
|
|
}
|
|
|
|
return c.data
|
|
|
|
}
|
|
|
|
|
2023-01-17 15:18:31 +00:00
|
|
|
func Categories(ctx context.Context) []models.TermsMy {
|
2023-01-21 14:56:41 +00:00
|
|
|
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
2023-01-21 13:13:33 +00:00
|
|
|
logs.ErrPrintln(err, "get category err")
|
2023-01-21 14:56:41 +00:00
|
|
|
r = slice.Filter(r, func(my models.TermsMy) bool {
|
|
|
|
return my.Taxonomy == "category"
|
|
|
|
})
|
2023-01-21 13:13:33 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2023-01-21 14:56:41 +00:00
|
|
|
func Tags(ctx context.Context) []models.TermsMy {
|
|
|
|
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
|
|
|
logs.ErrPrintln(err, "get category err")
|
|
|
|
r = slice.Filter(r, func(my models.TermsMy) bool {
|
|
|
|
return my.Taxonomy == "post_tag"
|
|
|
|
})
|
2023-01-12 12:42:16 +00:00
|
|
|
return r
|
|
|
|
}
|
2023-01-21 14:56:41 +00:00
|
|
|
func AllTagsNames(ctx context.Context) map[string]struct{} {
|
|
|
|
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
|
|
|
logs.ErrPrintln(err, "get category err")
|
|
|
|
return slice.FilterAndToMap(r, func(t models.TermsMy) (string, struct{}, bool) {
|
|
|
|
if t.Taxonomy == "post_tag" {
|
|
|
|
return t.Name, struct{}{}, true
|
|
|
|
}
|
|
|
|
return "", struct{}{}, false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func AllCategoryNames(ctx context.Context) map[string]struct{} {
|
|
|
|
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
|
|
|
|
logs.ErrPrintln(err, "get category err")
|
|
|
|
return slice.FilterAndToMap(r, func(t models.TermsMy) (string, struct{}, bool) {
|
|
|
|
if t.Taxonomy == "category" {
|
|
|
|
return t.Name, struct{}{}, true
|
|
|
|
}
|
|
|
|
return "", struct{}{}, false
|
|
|
|
})
|
|
|
|
}
|