diff --git a/application/application.go b/application/application.go index d0f0299..2078881 100644 --- a/application/application.go +++ b/application/application.go @@ -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 { diff --git a/pkg/cache/memo.go b/pkg/cache/memo.go index 5153fe2..3261113 100644 --- a/pkg/cache/memo.go +++ b/pkg/cache/memo.go @@ -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) } } diff --git a/pkg/filemanager/manager/recycle.go b/pkg/filemanager/manager/recycle.go index 1024a6c..2b1c84e 100644 --- a/pkg/filemanager/manager/recycle.go +++ b/pkg/filemanager/manager/recycle.go @@ -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