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

151 lines
4.9 KiB
Go
Raw Normal View History

2023-01-12 12:42:16 +00:00
package cache
import (
"context"
2023-05-04 12:36:17 +00:00
"github.com/fthvgb1/wp-go/app/pkg/config"
"github.com/fthvgb1/wp-go/app/pkg/dao"
"github.com/fthvgb1/wp-go/app/pkg/logs"
"github.com/fthvgb1/wp-go/app/pkg/models"
2023-11-28 14:46:22 +00:00
"github.com/fthvgb1/wp-go/cache"
2023-11-07 07:18:34 +00:00
"github.com/fthvgb1/wp-go/cache/cachemanager"
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/safety"
2023-01-12 12:42:16 +00:00
"time"
)
func InitActionsCommonCache() {
2023-02-05 13:06:04 +00:00
c := config.GetConfig()
2023-01-12 12:42:16 +00:00
2023-11-25 09:49:20 +00:00
cachemanager.NewMemoryMapCache(nil, dao.SearchPostIds, c.CacheTime.SearchPostCacheTime, "searchPostIds", func() time.Duration {
return config.GetConfig().CacheTime.SearchPostCacheTime
})
2023-01-12 12:42:16 +00:00
2023-11-25 09:49:20 +00:00
cachemanager.NewMemoryMapCache(nil, dao.SearchPostIds, c.CacheTime.PostListCacheTime, "listPostIds", func() time.Duration {
return config.GetConfig().CacheTime.PostListCacheTime
})
2023-01-12 12:42:16 +00:00
2023-11-25 09:49:20 +00:00
cachemanager.NewMemoryMapCache(nil, dao.MonthPost, c.CacheTime.MonthPostCacheTime, "monthPostIds", func() time.Duration {
return config.GetConfig().CacheTime.MonthPostCacheTime
})
2023-01-12 12:42:16 +00:00
2023-11-25 09:49:20 +00:00
cachemanager.NewMemoryMapCache(nil, dao.GetPostContext, c.CacheTime.ContextPostCacheTime, "postContext", func() time.Duration {
return config.GetConfig().CacheTime.ContextPostCacheTime
})
2023-01-12 12:42:16 +00:00
2023-11-25 09:49:20 +00:00
cachemanager.NewMemoryMapCache(dao.GetPostsByIds, nil, c.CacheTime.PostDataCacheTime, "postData", func() time.Duration {
return config.GetConfig().CacheTime.PostDataCacheTime
})
2023-01-12 12:42:16 +00:00
2023-11-25 09:49:20 +00:00
cachemanager.NewMemoryMapCache(dao.GetPostMetaByPostIds, nil, c.CacheTime.PostDataCacheTime, "postMetaData", func() time.Duration {
return config.GetConfig().CacheTime.PostDataCacheTime
})
2023-01-12 12:42:16 +00:00
2023-11-25 09:49:20 +00:00
cachemanager.NewMemoryMapCache(nil, dao.CategoriesAndTags, c.CacheTime.CategoryCacheTime, "categoryAndTagsData", func() time.Duration {
return config.GetConfig().CacheTime.CategoryCacheTime
})
2023-01-12 12:42:16 +00:00
2023-11-25 09:49:20 +00:00
cachemanager.NewVarMemoryCache(dao.RecentPosts, c.CacheTime.RecentPostCacheTime, "recentPosts", func() time.Duration {
2023-11-02 14:40:13 +00:00
return config.GetConfig().CacheTime.RecentPostCacheTime
})
2023-01-12 12:42:16 +00:00
2023-11-25 09:49:20 +00:00
cachemanager.NewVarMemoryCache(dao.RecentComments, c.CacheTime.RecentCommentsCacheTime, "recentComments", func() time.Duration {
2023-11-02 14:40:13 +00:00
return config.GetConfig().CacheTime.RecentCommentsCacheTime
})
2023-01-12 12:42:16 +00:00
2023-11-28 14:46:22 +00:00
cachemanager.NewMemoryMapCache(nil, dao.PostComments, c.CacheTime.PostCommentsCacheTime,
"postCommentIds",
func() time.Duration {
return config.GetConfig().CacheTime.PostCommentsCacheTime
},
cache.NewIncreaseUpdate("comment-increaseUpdate", dao.GetIncreaseComment, 30*time.Second,
func() time.Duration {
return config.GetConfig().CacheTime.CommentsIncreaseUpdateTime
}))
2023-01-12 12:42:16 +00:00
2023-11-25 09:49:20 +00:00
cachemanager.NewVarMemoryCache(dao.GetMaxPostId, c.CacheTime.MaxPostIdCacheTime, "maxPostId", func() time.Duration {
2023-11-02 14:40:13 +00:00
return config.GetConfig().CacheTime.MaxPostIdCacheTime
})
2023-01-12 12:42:16 +00:00
cachemanager.NewMemoryMapCache(nil, dao.GetUserById, c.CacheTime.UserInfoCacheTime, "userData", func() time.Duration {
return config.GetConfig().CacheTime.UserInfoCacheTime
})
2023-01-12 12:42:16 +00:00
2023-11-25 09:49:20 +00:00
cachemanager.NewMemoryMapCache(nil, dao.GetUserByName, c.CacheTime.UserInfoCacheTime, "usernameMapToUserData", func() time.Duration {
return config.GetConfig().CacheTime.UserInfoCacheTime
})
2023-01-12 12:42:16 +00:00
cachemanager.NewMemoryMapCache(dao.GetCommentByIds, nil, c.CacheTime.CommentsCacheTime, "commentData", func() time.Duration {
return config.GetConfig().CacheTime.CommentsCacheTime
})
2023-02-05 13:06:04 +00:00
2023-11-25 09:49:20 +00:00
cachemanager.NewVarMemoryCache(dao.AllUsername, c.CacheTime.UserInfoCacheTime, "allUsername", func() time.Duration {
2023-11-02 14:40:13 +00:00
return config.GetConfig().CacheTime.UserInfoCacheTime
})
2023-02-05 13:06:04 +00:00
2023-11-25 09:49:20 +00:00
cachemanager.NewVarMemoryCache(feed, time.Hour, "feed")
2023-01-19 13:02:39 +00:00
2023-11-25 09:49:20 +00:00
cachemanager.NewMemoryMapCache(nil, postFeed, time.Hour, "postFeed")
2023-01-19 13:02:39 +00:00
2023-11-25 09:49:20 +00:00
cachemanager.NewVarMemoryCache(commentsFeed, time.Hour, "commentsFeed")
2023-01-19 13:02:39 +00:00
2023-11-25 09:49:20 +00:00
cachemanager.NewMemoryMapCache[string, string](nil, nil, 15*time.Minute, "NewComment")
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-11-25 09:49:20 +00:00
r, err := cachemanager.Get[[]models.TermsMy]("categoryAndTagsData", ctx, tt, time.Second)
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]
}
2023-11-25 09:49:20 +00:00
r, err := cachemanager.Get[[]models.TermsMy]("categoryAndTagsData", ctx, tt, time.Second)
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
}