fix(cron): add missing tasks to collect expired items in mem KV (#2466)

pull/2224/merge
Aaron Liu 2025-06-12 09:55:05 +08:00
parent 3ab86e9b1d
commit e750cbfb77
3 changed files with 15 additions and 4 deletions

View File

@ -73,6 +73,9 @@ func (s *server) Start() error {
s.kv = s.dep.KV()
// delete all cached settings
_ = s.kv.Delete(setting.KvSettingPrefix)
if memKv, ok := s.kv.(*cache.MemoStore); ok {
memKv.GarbageCollect(s.logger)
}
// TODO: make sure redis is connected in dep before user traffic.
if s.config.System().Mode == conf.MasterMode {

9
pkg/cache/memo.go vendored
View File

@ -3,12 +3,13 @@ package cache
import (
"encoding/gob"
"fmt"
"github.com/cloudreve/Cloudreve/v4/pkg/logging"
"github.com/cloudreve/Cloudreve/v4/pkg/util"
"os"
"strings"
"sync"
"time"
"github.com/cloudreve/Cloudreve/v4/pkg/logging"
"github.com/cloudreve/Cloudreve/v4/pkg/util"
)
// MemoStore 内存存储驱动
@ -55,11 +56,11 @@ func getValue(item any, ok bool) (any, bool) {
}
// GarbageCollect 回收已过期的缓存
func (store *MemoStore) GarbageCollect() {
func (store *MemoStore) GarbageCollect(l logging.Logger) {
store.Store.Range(func(key, value any) bool {
if item, ok := value.(itemWithTTL); ok {
if item.Expires > 0 && item.Expires < time.Now().Unix() {
util.Log().Debug("Cache %q is garbage collected.", key.(string))
l.Debug("Cache %q is garbage collected.", key.(string))
store.Store.Delete(key)
}
}

View File

@ -13,6 +13,7 @@ import (
"github.com/cloudreve/Cloudreve/v4/ent/task"
"github.com/cloudreve/Cloudreve/v4/inventory"
"github.com/cloudreve/Cloudreve/v4/inventory/types"
"github.com/cloudreve/Cloudreve/v4/pkg/cache"
"github.com/cloudreve/Cloudreve/v4/pkg/crontab"
"github.com/cloudreve/Cloudreve/v4/pkg/filemanager/fs"
"github.com/cloudreve/Cloudreve/v4/pkg/filemanager/fs/dbfs"
@ -296,6 +297,12 @@ const (
func CronCollectTrashBin(ctx context.Context) {
dep := dependency.FromContext(ctx)
l := dep.Logger()
kv := dep.KV()
if memKv, ok := kv.(*cache.MemoStore); ok {
memKv.GarbageCollect(l)
}
fm := NewFileManager(dep, inventory.UserFromContext(ctx)).(*manager)
pageSize := dep.SettingProvider().DBFS(ctx).MaxPageSize
batch := 0