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

143 lines
4.4 KiB
Go
Raw Normal View History

2023-01-12 12:42:16 +00:00
package cache
import (
"context"
"github.com/fthvgb1/wp-go/cache"
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/cmd/cachemanager"
"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"
"github.com/fthvgb1/wp-go/safety"
2023-01-12 12:42:16 +00:00
"time"
)
var postContextCache *cache.MapCache[uint64, dao.PostContext]
2023-04-24 13:51:43 +00:00
var categoryAndTagsCaches *cache.MapCache[string, []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-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
searchPostIdsCache = cachemanager.MapCacheBy[string](dao.SearchPostIds, c.CacheTime.SearchPostCacheTime)
2023-01-12 12:42:16 +00:00
postListIdsCache = cachemanager.MapCacheBy[string](dao.SearchPostIds, c.CacheTime.PostListCacheTime)
2023-01-12 12:42:16 +00:00
monthPostsCache = cachemanager.MapCacheBy[string](dao.MonthPost, c.CacheTime.MonthPostCacheTime)
2023-01-12 12:42:16 +00:00
postContextCache = cachemanager.MapCacheBy[uint64](dao.GetPostContext, c.CacheTime.ContextPostCacheTime)
2023-01-12 12:42:16 +00:00
postsCache = cachemanager.MapBatchCacheBy(dao.GetPostsByIds, c.CacheTime.PostDataCacheTime)
2023-01-12 12:42:16 +00:00
postMetaCache = cachemanager.MapBatchCacheBy(dao.GetPostMetaByPostIds, c.CacheTime.PostDataCacheTime)
2023-01-12 12:42:16 +00:00
2023-04-24 13:51:43 +00:00
categoryAndTagsCaches = cachemanager.MapCacheBy[string](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
postCommentCaches = cachemanager.MapCacheBy[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
usersCache = cachemanager.MapCacheBy[uint64](dao.GetUserById, c.CacheTime.UserInfoCacheTime)
2023-01-12 12:42:16 +00:00
usersNameCache = cachemanager.MapCacheBy[string](dao.GetUserByName, c.CacheTime.UserInfoCacheTime)
2023-01-12 12:42:16 +00:00
commentsCache = cachemanager.MapBatchCacheBy(dao.GetCommentByIds, c.CacheTime.CommentsCacheTime)
2023-02-05 13:06:04 +00:00
allUsernameCache = cache.NewVarCache(dao.AllUsername, c.CacheTime.UserInfoCacheTime)
2023-01-20 10:10:13 +00:00
feedCache = cache.NewVarCache(feed, time.Hour)
2023-01-19 13:02:39 +00:00
postFeedCache = cachemanager.MapCacheBy[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 = cachemanager.MapCacheBy[string, string](nil, 15*time.Minute)
2023-01-19 13:02:39 +00:00
InitFeed()
2023-01-12 12:42:16 +00:00
}
type Arch struct {
data []models.PostArchive
2023-02-06 07:59:44 +00:00
fn func(context.Context) ([]models.PostArchive, error)
month time.Month
2023-01-12 12:42:16 +00:00
}
var arch = safety.NewVar(Arch{
fn: dao.Archives,
})
func Archives(ctx context.Context) []models.PostArchive {
a := arch.Load()
data := a.data
l := len(data)
2023-01-12 12:42:16 +00:00
m := time.Now().Month()
2023-02-06 07:59:44 +00:00
if l > 0 && a.month != m || l < 1 {
r, err := a.fn(ctx)
2023-01-12 12:42:16 +00:00
if err != nil {
2023-04-07 14:59:07 +00:00
logs.Error(err, "set cache fail")
2023-01-12 12:42:16 +00:00
return nil
}
2023-02-06 07:59:44 +00:00
a.month = m
a.data = r
arch.Store(a)
data = r
2023-01-12 12:42:16 +00:00
}
return data
2023-01-12 12:42:16 +00:00
}
2023-03-14 10:35:48 +00:00
// CategoriesTags categories or tags
//
// t is constraints.Tag or constraints.Category
2023-04-24 13:51:43 +00:00
func CategoriesTags(ctx context.Context, t ...string) []models.TermsMy {
tt := ""
2023-02-05 12:33:33 +00:00
if len(t) > 0 {
2023-03-19 12:40:08 +00:00
tt = t[0]
2023-02-05 12:33:33 +00:00
}
2023-03-19 12:40:08 +00:00
r, err := categoryAndTagsCaches.GetCache(ctx, tt, time.Second, ctx, tt)
2023-04-07 14:59:07 +00:00
logs.IfError(err, "get category fail")
2023-01-12 12:42:16 +00:00
return r
}
2023-04-24 13:51:43 +00:00
func AllCategoryTagsNames(ctx context.Context, t ...string) map[string]struct{} {
tt := ""
2023-03-19 12:40:08 +00:00
if len(t) > 0 {
tt = t[0]
}
r, err := categoryAndTagsCaches.GetCache(ctx, tt, time.Second, ctx, tt)
2023-04-07 14:59:07 +00:00
if err != nil {
logs.Error(err, "get category fail")
return nil
}
2023-03-19 12:40:08 +00:00
return slice.ToMap(r, func(t models.TermsMy) (string, struct{}) {
return t.Name, struct{}{}
}, true)
2023-01-21 14:56:41 +00:00
}