From 477fd126f48b6847095f941499d2b089c8a00e10 Mon Sep 17 00:00:00 2001 From: xing Date: Wed, 28 Sep 2022 20:02:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=90=9C=E7=B4=A2=E5=8D=95=E7=8B=AC=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/common/common.go | 10 +++++++--- actions/common/posts.go | 16 +++++++++------- actions/index.go | 4 +++- cache/map.go | 17 +++++++++++++++-- cache/slice.go | 2 +- config.example.yaml | 8 ++++++++ vars/config.go | 4 ++++ 7 files changed, 47 insertions(+), 14 deletions(-) diff --git a/actions/common/common.go b/actions/common/common.go index 9068b45..b4f1599 100644 --- a/actions/common/common.go +++ b/actions/common/common.go @@ -22,6 +22,7 @@ var recentCommentsCaches *cache.SliceCache[models.WpComments] var postCommentCaches *cache.MapCache[uint64, []models.WpComments] var postsCache *cache.MapCache[uint64, models.WpPosts] var monthPostsCache *cache.MapCache[string, []uint64] +var postListIdsCache *cache.MapCache[string, PostIds] var searchPostIdsCache *cache.MapCache[string, PostIds] func InitActionsCommonCache() { @@ -30,13 +31,15 @@ func InitActionsCommonCache() { setCacheFunc: archives, } - searchPostIdsCache = cache.NewMapCache[string, PostIds](searchPostIds, time.Hour) + searchPostIdsCache = cache.NewMapCache[string, PostIds](searchPostIds, vars.Conf.SearchPostCacheTime) - monthPostsCache = cache.NewMapCache[string, []uint64](monthPost, time.Hour) + postListIdsCache = cache.NewMapCache[string, PostIds](searchPostIds, vars.Conf.PostListCacheTime) + + monthPostsCache = cache.NewMapCache[string, []uint64](monthPost, vars.Conf.MonthPostCacheTime) postContextCache = cache.NewMapCache[uint64, PostContext](getPostContext, vars.Conf.ContextPostCacheTime) - postsCache = cache.NewMapBatchCache[uint64, models.WpPosts](getPostsByIds, time.Hour) + postsCache = cache.NewMapBatchCache[uint64, models.WpPosts](getPostsByIds, vars.Conf.PostDataCacheTime) postsCache.SetCacheFunc(getPostById) categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime) @@ -44,6 +47,7 @@ func InitActionsCommonCache() { recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, vars.Conf.RecentPostCacheTime) recentCommentsCaches = cache.NewSliceCache[models.WpComments](recentComments, vars.Conf.RecentCommentsCacheTime) + postCommentCaches = cache.NewMapCache[uint64, []models.WpComments](postComments, time.Minute*5) } diff --git a/actions/common/posts.go b/actions/common/posts.go index 8fc2617..0606314 100644 --- a/actions/common/posts.go +++ b/actions/common/posts.go @@ -22,12 +22,14 @@ func GetPostsByIds(ctx context.Context, ids []uint64) ([]models.WpPosts, error) return postsCache.GetCacheBatch(ctx, ids, time.Second, ids) } -func SetPostCache(ids []models.WpPosts) error { - var arg []uint64 - for _, posts := range ids { - arg = append(arg, posts.Id) +func SearchPost(ctx context.Context, key string, args ...any) (r []models.WpPosts, total int, err error) { + ids, err := searchPostIdsCache.GetCache(ctx, key, time.Second, args...) + if err != nil { + return } - return postsCache.SetByBatchFn(arg) + total = ids.Length + r, err = GetPostsByIds(ctx, ids.Ids) + return } func getPostById(id ...any) (post models.WpPosts, err error) { @@ -89,8 +91,8 @@ func getPostsByIds(ids ...any) (m map[uint64]models.WpPosts, err error) { return } -func SearchPostIds(ctx context.Context, key string, args ...any) (r []models.WpPosts, total int, err error) { - ids, err := searchPostIdsCache.GetCache(ctx, key, time.Second, args...) +func PostLists(ctx context.Context, key string, args ...any) (r []models.WpPosts, total int, err error) { + ids, err := postListIdsCache.GetCache(ctx, key, time.Second, args...) if err != nil { return } diff --git a/actions/index.go b/actions/index.go index a7a8f97..a120953 100644 --- a/actions/index.go +++ b/actions/index.go @@ -180,8 +180,10 @@ func Index(c *gin.Context) { if err != nil { return } + } else if h.search != "" { + postIds, totalRaw, err = common.SearchPost(c, h.getSearchKey(), h.where, h.page, h.pageSize, models.SqlBuilder{{h.orderBy, h.order}}, h.join, h.postType, h.status) } else { - postIds, totalRaw, err = common.SearchPostIds(c, h.getSearchKey(), h.where, h.page, h.pageSize, models.SqlBuilder{{h.orderBy, h.order}}, h.join, h.postType, h.status) + postIds, totalRaw, err = common.PostLists(c, h.getSearchKey(), h.where, h.page, h.pageSize, models.SqlBuilder{{h.orderBy, h.order}}, h.join, h.postType, h.status) } defer func() { diff --git a/cache/map.go b/cache/map.go index b7fe63e..8f22eb7 100644 --- a/cache/map.go +++ b/cache/map.go @@ -99,8 +99,9 @@ func (m *MapCache[K, V]) GetCache(c context.Context, key K, timeout time.Duratio if !ok { data = mapCacheStruct[V]{} } + now := time.Duration(time.Now().UnixNano()) var err error - expired := time.Duration(data.setTime.Unix())+m.expireTime/time.Second < time.Duration(time.Now().Unix()) + expired := time.Duration(data.setTime.UnixNano())+m.expireTime < now //todo 这里应该判断下取出的值是否为零值,不过怎么操作呢? if !ok || (ok && m.expireTime >= 0 && expired) { t := data.incr @@ -145,13 +146,14 @@ func (m *MapCache[K, V]) GetCacheBatch(c context.Context, key []K, timeout time. var needFlush []K var res []V t := 0 + now := time.Duration(time.Now().UnixNano()) for _, k := range key { d, ok := m.data[k] if !ok { needFlush = append(needFlush, k) continue } - expired := time.Duration(d.setTime.Unix())+m.expireTime/time.Second < time.Duration(time.Now().Unix()) + expired := time.Duration(d.setTime.UnixNano())+m.expireTime < now if expired { needFlush = append(needFlush, k) } @@ -204,3 +206,14 @@ func (m *MapCache[K, V]) GetCacheBatch(c context.Context, key []K, timeout time. } return res, err } + +func (m *MapCache[K, V]) ClearExpiredCache() { + now := time.Duration(time.Now().UnixNano()) + m.mutex.Lock() + defer m.mutex.Unlock() + for k, v := range m.data { + if now > time.Duration(v.setTime.UnixNano())+m.expireTime { + delete(m.data, k) + } + } +} diff --git a/cache/slice.go b/cache/slice.go index 6be73f5..d53a607 100644 --- a/cache/slice.go +++ b/cache/slice.go @@ -35,7 +35,7 @@ func (c *SliceCache[T]) GetCache(ctx context.Context, timeout time.Duration, par l := len(c.data) data := c.data var err error - expired := time.Duration(c.setTime.Unix())+c.expireTime/time.Second < time.Duration(time.Now().Unix()) + expired := time.Duration(c.setTime.UnixNano())+c.expireTime < time.Duration(time.Now().UnixNano()) if l < 1 || (l > 0 && c.expireTime >= 0 && expired) { t := c.incr call := func() { diff --git a/config.example.yaml b/config.example.yaml index 5efb338..90712ee 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -27,3 +27,11 @@ recentCommentsCacheTime: 5m digestCacheTime: 5m # 摘要字数 digestWordCount: 300 +# 文档列表id页缓存 包括默认列表、分类 +postListCacheTime: 1h +# 搜索文档id缓存时间 +searchPostCacheTime: 5m +# 月归档文章id缓存 +monthPostCacheTime: 1h +# 文档数据缓存时间 +postDataCacheTime: 1h diff --git a/vars/config.go b/vars/config.go index 12b6521..106c1c6 100644 --- a/vars/config.go +++ b/vars/config.go @@ -18,6 +18,10 @@ type Config struct { RecentCommentsCacheTime time.Duration `yaml:"recentCommentsCacheTime"` DigestCacheTime time.Duration `yaml:"digestCacheTime"` DigestWordCount int `yaml:"digestWordCount"` + PostListCacheTime time.Duration `yaml:"postListCacheTime"` + SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime"` + MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime"` + PostDataCacheTime time.Duration `yaml:"postDataCacheTime"` } type Mysql struct {