From e1636bbb9c0a77dfe573d1f6101a7becad0660b5 Mon Sep 17 00:00:00 2001 From: xing Date: Wed, 28 Sep 2022 21:16:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E6=97=B6=E6=B8=85=E7=90=86=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=9B=9E=E6=94=B6=E5=86=85=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/common/common.go | 21 +++++++++++++++------ cache/map.go | 15 +++++++++------ config.example.yaml | 6 +++++- main.go | 13 +++++++++++++ plugins/digest.go | 6 +++++- vars/config.go | 2 ++ 6 files changed, 49 insertions(+), 14 deletions(-) diff --git a/actions/common/common.go b/actions/common/common.go index b4f1599..856751c 100644 --- a/actions/common/common.go +++ b/actions/common/common.go @@ -31,15 +31,15 @@ func InitActionsCommonCache() { setCacheFunc: archives, } - searchPostIdsCache = cache.NewMapCache[string, PostIds](searchPostIds, vars.Conf.SearchPostCacheTime) + searchPostIdsCache = cache.NewMapCacheByFn[string, PostIds](searchPostIds, vars.Conf.SearchPostCacheTime) - postListIdsCache = cache.NewMapCache[string, PostIds](searchPostIds, vars.Conf.PostListCacheTime) + postListIdsCache = cache.NewMapCacheByFn[string, PostIds](searchPostIds, vars.Conf.PostListCacheTime) - monthPostsCache = cache.NewMapCache[string, []uint64](monthPost, vars.Conf.MonthPostCacheTime) + monthPostsCache = cache.NewMapCacheByFn[string, []uint64](monthPost, vars.Conf.MonthPostCacheTime) - postContextCache = cache.NewMapCache[uint64, PostContext](getPostContext, vars.Conf.ContextPostCacheTime) + postContextCache = cache.NewMapCacheByFn[uint64, PostContext](getPostContext, vars.Conf.ContextPostCacheTime) - postsCache = cache.NewMapBatchCache[uint64, models.WpPosts](getPostsByIds, vars.Conf.PostDataCacheTime) + postsCache = cache.NewMapCacheByBatchFn[uint64, models.WpPosts](getPostsByIds, vars.Conf.PostDataCacheTime) postsCache.SetCacheFunc(getPostById) categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime) @@ -48,7 +48,16 @@ func InitActionsCommonCache() { recentCommentsCaches = cache.NewSliceCache[models.WpComments](recentComments, vars.Conf.RecentCommentsCacheTime) - postCommentCaches = cache.NewMapCache[uint64, []models.WpComments](postComments, time.Minute*5) + postCommentCaches = cache.NewMapCacheByFn[uint64, []models.WpComments](postComments, vars.Conf.CommentsCacheTime) +} + +func ClearCache() { + searchPostIdsCache.ClearExpired() + postsCache.ClearExpired() + postsCache.ClearExpired() + postListIdsCache.ClearExpired() + monthPostsCache.ClearExpired() + postContextCache.ClearExpired() } func GetMonthPostIds(ctx context.Context, year, month string, page, limit int, order string) (r []models.WpPosts, total int, err error) { diff --git a/cache/map.go b/cache/map.go index 8f22eb7..e570dd3 100644 --- a/cache/map.go +++ b/cache/map.go @@ -16,6 +16,10 @@ type MapCache[K comparable, V any] struct { expireTime time.Duration } +func NewMapCache[K comparable, V any](expireTime time.Duration) *MapCache[K, V] { + return &MapCache[K, V]{expireTime: expireTime} +} + type mapCacheStruct[T any] struct { setTime time.Time incr int @@ -30,7 +34,7 @@ func (m *MapCache[K, V]) SetCacheBatchFunc(fn func(...any) (map[K]V, error)) { m.setBatchCacheFn = fn } -func NewMapCache[K comparable, V any](fun func(...any) (V, error), expireTime time.Duration) *MapCache[K, V] { +func NewMapCacheByFn[K comparable, V any](fun func(...any) (V, error), expireTime time.Duration) *MapCache[K, V] { return &MapCache[K, V]{ mutex: &sync.Mutex{}, setCacheFunc: fun, @@ -38,7 +42,7 @@ func NewMapCache[K comparable, V any](fun func(...any) (V, error), expireTime ti data: make(map[K]mapCacheStruct[V]), } } -func NewMapBatchCache[K comparable, V any](fn func(...any) (map[K]V, error), expireTime time.Duration) *MapCache[K, V] { +func NewMapCacheByBatchFn[K comparable, V any](fn func(...any) (map[K]V, error), expireTime time.Duration) *MapCache[K, V] { return &MapCache[K, V]{ mutex: &sync.Mutex{}, setBatchCacheFn: fn, @@ -47,11 +51,10 @@ func NewMapBatchCache[K comparable, V any](fn func(...any) (map[K]V, error), exp } } -func (m *MapCache[K, V]) FlushCache(k any) { +func (m *MapCache[K, V]) Flush() { m.mutex.Lock() defer m.mutex.Unlock() - key := k.(K) - delete(m.data, key) + m.data = make(map[K]mapCacheStruct[V]) } func (m *MapCache[K, V]) Get(k K) V { @@ -207,7 +210,7 @@ func (m *MapCache[K, V]) GetCacheBatch(c context.Context, key []K, timeout time. return res, err } -func (m *MapCache[K, V]) ClearExpiredCache() { +func (m *MapCache[K, V]) ClearExpired() { now := time.Duration(time.Now().UnixNano()) m.mutex.Lock() defer m.mutex.Unlock() diff --git a/config.example.yaml b/config.example.yaml index 90712ee..621c2a0 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -31,7 +31,11 @@ digestWordCount: 300 postListCacheTime: 1h # 搜索文档id缓存时间 searchPostCacheTime: 5m -# 月归档文章id缓存 +# 月归档文章id缓存时间 monthPostCacheTime: 1h # 文档数据缓存时间 postDataCacheTime: 1h +# 文章评论缓存时间 +commentsCacheTime: 5m +# 定时清理缓存周期时间 +crontabClearCacheTime: 5m \ No newline at end of file diff --git a/main.go b/main.go index 43dab4b..4d1ea81 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "github/fthvgb1/wp-go/plugins" "github/fthvgb1/wp-go/route" "github/fthvgb1/wp-go/vars" + "time" ) func init() { @@ -32,6 +33,18 @@ func init() { common.InitActionsCommonCache() plugins.InitDigestCache() + go cronClearCache() +} + +func cronClearCache() { + t := time.NewTicker(vars.Conf.CrontabClearCacheTime) + for { + select { + case <-t.C: + common.ClearCache() + plugins.ClearDigestCache() + } + } } func main() { diff --git a/plugins/digest.go b/plugins/digest.go index b05fb9e..d46a57e 100644 --- a/plugins/digest.go +++ b/plugins/digest.go @@ -19,7 +19,11 @@ var digestCache *cache.MapCache[uint64, string] var quto = regexp.MustCompile(`" *|& *|< *|> ?|  *`) func InitDigestCache() { - digestCache = cache.NewMapCache[uint64](digestRaw, vars.Conf.DigestCacheTime) + digestCache = cache.NewMapCacheByFn[uint64](digestRaw, vars.Conf.DigestCacheTime) +} + +func ClearDigestCache() { + digestCache.ClearExpired() } func digestRaw(arg ...any) (string, error) { diff --git a/vars/config.go b/vars/config.go index 106c1c6..92ed231 100644 --- a/vars/config.go +++ b/vars/config.go @@ -22,6 +22,8 @@ type Config struct { SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime"` MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime"` PostDataCacheTime time.Duration `yaml:"postDataCacheTime"` + CommentsCacheTime time.Duration `yaml:"commentsCacheTime"` + CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime"` } type Mysql struct {