wp-go/internal/pkg/cache/cache.go

180 lines
5.7 KiB
Go
Raw Normal View History

2023-01-12 12:42:16 +00:00
package cache
import (
"context"
"github.com/fthvgb1/wp-go/cache"
2023-02-05 12:33:33 +00:00
"github.com/fthvgb1/wp-go/helper"
"github.com/fthvgb1/wp-go/helper/slice"
"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-02-05 12:33:33 +00:00
"github.com/fthvgb1/wp-go/internal/plugins"
2023-01-12 12:42:16 +00:00
"sync"
"time"
)
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]
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]
var allUsernameCache *cache.VarCache[map[string]struct{}]
2023-01-31 16:58:42 +00:00
var headerImagesCache *cache.MapCache[string, []models.PostThumbnail]
2023-02-02 11:16:18 +00:00
var ctx context.Context
2023-01-12 12:42:16 +00:00
func InitActionsCommonCache() {
2023-02-05 13:06:04 +00:00
c := config.GetConfig()
2023-01-12 12:42:16 +00:00
archivesCaches = &Arch{
mutex: &sync.Mutex{},
setCacheFunc: dao.Archives,
2023-01-12 12:42:16 +00:00
}
2023-02-05 13:06:04 +00:00
searchPostIdsCache = cache.NewMemoryMapCacheByFn[string](dao.SearchPostIds, c.CacheTime.SearchPostCacheTime)
2023-01-12 12:42:16 +00:00
2023-02-05 13:06:04 +00:00
postListIdsCache = cache.NewMemoryMapCacheByFn[string](dao.SearchPostIds, c.CacheTime.PostListCacheTime)
2023-01-12 12:42:16 +00:00
2023-02-05 13:06:04 +00:00
monthPostsCache = cache.NewMemoryMapCacheByFn[string](dao.MonthPost, c.CacheTime.MonthPostCacheTime)
2023-01-12 12:42:16 +00:00
2023-02-05 13:06:04 +00:00
postContextCache = cache.NewMemoryMapCacheByFn[uint64](dao.GetPostContext, c.CacheTime.ContextPostCacheTime)
2023-01-12 12:42:16 +00:00
2023-02-05 13:06:04 +00:00
postsCache = cache.NewMemoryMapCacheByBatchFn(dao.GetPostsByIds, c.CacheTime.PostDataCacheTime)
2023-01-12 12:42:16 +00:00
2023-02-05 13:06:04 +00:00
postMetaCache = cache.NewMemoryMapCacheByBatchFn(dao.GetPostMetaByPostIds, c.CacheTime.PostDataCacheTime)
2023-01-12 12:42:16 +00:00
2023-02-05 13:06:04 +00:00
categoryAndTagsCaches = cache.NewVarCache(dao.CategoriesAndTags, c.CacheTime.CategoryCacheTime)
2023-01-12 12:42:16 +00:00
2023-02-05 13:06:04 +00:00
recentPostsCaches = cache.NewVarCache(dao.RecentPosts, c.CacheTime.RecentPostCacheTime)
2023-01-12 12:42:16 +00:00
2023-02-05 13:06:04 +00:00
recentCommentsCaches = cache.NewVarCache(dao.RecentComments, c.CacheTime.RecentCommentsCacheTime)
2023-01-12 12:42:16 +00:00
2023-02-05 13:06:04 +00:00
postCommentCaches = cache.NewMemoryMapCacheByFn[uint64](dao.PostComments, c.CacheTime.PostCommentsCacheTime)
2023-01-12 12:42:16 +00:00
2023-02-05 13:06:04 +00:00
maxPostIdCache = cache.NewVarCache(dao.GetMaxPostId, c.CacheTime.MaxPostIdCacheTime)
2023-01-12 12:42:16 +00:00
2023-02-05 13:06:04 +00:00
usersCache = cache.NewMemoryMapCacheByFn[uint64](dao.GetUserById, c.CacheTime.UserInfoCacheTime)
2023-01-12 12:42:16 +00:00
2023-02-05 13:06:04 +00:00
usersNameCache = cache.NewMemoryMapCacheByFn[string](dao.GetUserByName, c.CacheTime.UserInfoCacheTime)
2023-01-12 12:42:16 +00:00
2023-02-05 13:06:04 +00:00
commentsCache = cache.NewMemoryMapCacheByBatchFn(dao.GetCommentByIds, c.CacheTime.CommentsCacheTime)
allUsernameCache = cache.NewVarCache(dao.AllUsername, c.CacheTime.UserInfoCacheTime)
headerImagesCache = cache.NewMemoryMapCacheByFn[string](getHeaderImages, c.CacheTime.ThemeHeaderImagCacheTime)
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-02-02 11:16:18 +00:00
postFeedCache = cache.NewMemoryMapCacheByFn[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
2023-02-02 11:16:18 +00:00
newCommentCache = cache.NewMemoryMapCacheByFn[string, string](nil, 15*time.Minute)
2023-01-19 13:02:39 +00:00
2023-02-02 11:16:18 +00:00
ctx = context.Background()
2023-01-31 16:58:42 +00:00
2023-01-19 13:02:39 +00:00
InitFeed()
2023-01-12 12:42:16 +00:00
}
func ClearCache() {
2023-02-02 11:16:18 +00:00
searchPostIdsCache.ClearExpired(ctx)
searchPostIdsCache.ClearExpired(ctx)
postsCache.ClearExpired(ctx)
postMetaCache.ClearExpired(ctx)
postListIdsCache.ClearExpired(ctx)
monthPostsCache.ClearExpired(ctx)
postContextCache.ClearExpired(ctx)
usersCache.ClearExpired(ctx)
commentsCache.ClearExpired(ctx)
usersNameCache.ClearExpired(ctx)
postFeedCache.ClearExpired(ctx)
newCommentCache.ClearExpired(ctx)
headerImagesCache.ClearExpired(ctx)
2023-01-12 12:42:16 +00:00
}
func FlushCache() {
2023-02-02 11:16:18 +00:00
searchPostIdsCache.Flush(ctx)
postsCache.Flush(ctx)
postMetaCache.Flush(ctx)
postListIdsCache.Flush(ctx)
monthPostsCache.Flush(ctx)
postContextCache.Flush(ctx)
usersCache.Flush(ctx)
commentsCache.Flush(ctx)
usersCache.Flush(ctx)
postFeedCache.Flush(ctx)
newCommentCache.Flush(ctx)
headerImagesCache.Flush(ctx)
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-02-05 12:33:33 +00:00
func CategoriesTags(ctx context.Context, t ...int) []models.TermsMy {
2023-01-21 14:56:41 +00:00
r, err := categoryAndTagsCaches.GetCache(ctx, time.Second, ctx)
logs.ErrPrintln(err, "get category err")
2023-02-05 12:33:33 +00:00
if len(t) > 0 {
return slice.Filter(r, func(my models.TermsMy) bool {
return helper.Or(t[0] == plugins.Tag, "post_tag", "category") == my.Taxonomy
})
}
2023-01-12 12:42:16 +00:00
return r
}
2023-02-05 12:33:33 +00:00
func AllCategoryTagsNames(ctx context.Context, c int) map[string]struct{} {
2023-01-21 14:56:41 +00:00
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) {
2023-02-05 12:33:33 +00:00
if helper.Or(c == plugins.Tag, "post_tag", "category") == t.Taxonomy {
2023-01-21 14:56:41 +00:00
return t.Name, struct{}{}, true
}
return "", struct{}{}, false
})
}