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

127 lines
4.4 KiB
Go
Raw Permalink 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-07 07:18:34 +00:00
"github.com/fthvgb1/wp-go/cache/cachemanager"
"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
cachemanager.NewVarMemoryCache(RecentComment, 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
cachemanager.NewMemoryMapCache(nil, dao.CommentNum, 30*time.Second, "commentNumber", func() time.Duration {
return config.GetConfig().CacheTime.CommentsIncreaseUpdateTime
})
cachemanager.NewMemoryMapCache(nil, PostTopComments, 30*time.Second, "PostCommentsIds", func() time.Duration {
return config.GetConfig().CacheTime.CommentsIncreaseUpdateTime
})
cachemanager.NewMemoryMapCache(nil, dao.PostTopCommentNum, 30*time.Second, "postTopCommentsNum", func() time.Duration {
return config.GetConfig().CacheTime.CommentsIncreaseUpdateTime
})
cachemanager.NewMemoryMapCache(dao.GetCommentByIds, nil, time.Hour, "postCommentData", func() time.Duration {
return config.GetConfig().CacheTime.CommentsCacheTime
})
cachemanager.NewMemoryMapCache(dao.CommentChildren, nil, time.Minute, "commentChildren", 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
cachemanager.NewMemoryMapCache(nil, dao.GetUserByName, c.CacheTime.UserInfoCacheTime, "usernameToUserData", 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.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
2024-01-14 14:07:16 +00:00
cachemanager.NewVarMemoryCache(SiteFeed, time.Hour, "siteFeed")
2023-01-19 13:02:39 +00:00
cachemanager.NewMemoryMapCache(nil, PostFeed, time.Hour, "postFeed")
2023-01-19 13:02:39 +00:00
2024-01-14 14:07:16 +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()
if l < 1 || a.month != m {
2023-02-06 07:59:44 +00:00
r, err := a.fn(ctx)
2023-01-12 12:42:16 +00:00
if err != nil {
logs.Error(err, "set cache Archives 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
}