定时清理缓存回收内存
This commit is contained in:
parent
477fd126f4
commit
e1636bbb9c
|
@ -31,15 +31,15 @@ func InitActionsCommonCache() {
|
||||||
setCacheFunc: archives,
|
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)
|
postsCache.SetCacheFunc(getPostById)
|
||||||
|
|
||||||
categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime)
|
categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime)
|
||||||
|
@ -48,7 +48,16 @@ func InitActionsCommonCache() {
|
||||||
|
|
||||||
recentCommentsCaches = cache.NewSliceCache[models.WpComments](recentComments, vars.Conf.RecentCommentsCacheTime)
|
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) {
|
func GetMonthPostIds(ctx context.Context, year, month string, page, limit int, order string) (r []models.WpPosts, total int, err error) {
|
||||||
|
|
15
cache/map.go
vendored
15
cache/map.go
vendored
|
@ -16,6 +16,10 @@ type MapCache[K comparable, V any] struct {
|
||||||
expireTime time.Duration
|
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 {
|
type mapCacheStruct[T any] struct {
|
||||||
setTime time.Time
|
setTime time.Time
|
||||||
incr int
|
incr int
|
||||||
|
@ -30,7 +34,7 @@ func (m *MapCache[K, V]) SetCacheBatchFunc(fn func(...any) (map[K]V, error)) {
|
||||||
m.setBatchCacheFn = fn
|
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]{
|
return &MapCache[K, V]{
|
||||||
mutex: &sync.Mutex{},
|
mutex: &sync.Mutex{},
|
||||||
setCacheFunc: fun,
|
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]),
|
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]{
|
return &MapCache[K, V]{
|
||||||
mutex: &sync.Mutex{},
|
mutex: &sync.Mutex{},
|
||||||
setBatchCacheFn: fn,
|
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()
|
m.mutex.Lock()
|
||||||
defer m.mutex.Unlock()
|
defer m.mutex.Unlock()
|
||||||
key := k.(K)
|
m.data = make(map[K]mapCacheStruct[V])
|
||||||
delete(m.data, key)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MapCache[K, V]) Get(k K) 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
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MapCache[K, V]) ClearExpiredCache() {
|
func (m *MapCache[K, V]) ClearExpired() {
|
||||||
now := time.Duration(time.Now().UnixNano())
|
now := time.Duration(time.Now().UnixNano())
|
||||||
m.mutex.Lock()
|
m.mutex.Lock()
|
||||||
defer m.mutex.Unlock()
|
defer m.mutex.Unlock()
|
||||||
|
|
|
@ -31,7 +31,11 @@ digestWordCount: 300
|
||||||
postListCacheTime: 1h
|
postListCacheTime: 1h
|
||||||
# 搜索文档id缓存时间
|
# 搜索文档id缓存时间
|
||||||
searchPostCacheTime: 5m
|
searchPostCacheTime: 5m
|
||||||
# 月归档文章id缓存
|
# 月归档文章id缓存时间
|
||||||
monthPostCacheTime: 1h
|
monthPostCacheTime: 1h
|
||||||
# 文档数据缓存时间
|
# 文档数据缓存时间
|
||||||
postDataCacheTime: 1h
|
postDataCacheTime: 1h
|
||||||
|
# 文章评论缓存时间
|
||||||
|
commentsCacheTime: 5m
|
||||||
|
# 定时清理缓存周期时间
|
||||||
|
crontabClearCacheTime: 5m
|
13
main.go
13
main.go
|
@ -7,6 +7,7 @@ import (
|
||||||
"github/fthvgb1/wp-go/plugins"
|
"github/fthvgb1/wp-go/plugins"
|
||||||
"github/fthvgb1/wp-go/route"
|
"github/fthvgb1/wp-go/route"
|
||||||
"github/fthvgb1/wp-go/vars"
|
"github/fthvgb1/wp-go/vars"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -32,6 +33,18 @@ func init() {
|
||||||
|
|
||||||
common.InitActionsCommonCache()
|
common.InitActionsCommonCache()
|
||||||
plugins.InitDigestCache()
|
plugins.InitDigestCache()
|
||||||
|
go cronClearCache()
|
||||||
|
}
|
||||||
|
|
||||||
|
func cronClearCache() {
|
||||||
|
t := time.NewTicker(vars.Conf.CrontabClearCacheTime)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-t.C:
|
||||||
|
common.ClearCache()
|
||||||
|
plugins.ClearDigestCache()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -19,7 +19,11 @@ var digestCache *cache.MapCache[uint64, string]
|
||||||
var quto = regexp.MustCompile(`" *|& *|< *|> ?| *`)
|
var quto = regexp.MustCompile(`" *|& *|< *|> ?| *`)
|
||||||
|
|
||||||
func InitDigestCache() {
|
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) {
|
func digestRaw(arg ...any) (string, error) {
|
||||||
|
|
|
@ -22,6 +22,8 @@ type Config struct {
|
||||||
SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime"`
|
SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime"`
|
||||||
MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime"`
|
MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime"`
|
||||||
PostDataCacheTime time.Duration `yaml:"postDataCacheTime"`
|
PostDataCacheTime time.Duration `yaml:"postDataCacheTime"`
|
||||||
|
CommentsCacheTime time.Duration `yaml:"commentsCacheTime"`
|
||||||
|
CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Mysql struct {
|
type Mysql struct {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user