mirror of https://github.com/cloudreve/Cloudreve
fix(cron): add missing tasks to collect expired items in mem KV (#2466)
parent
3ab86e9b1d
commit
e750cbfb77
|
@ -73,6 +73,9 @@ func (s *server) Start() error {
|
||||||
s.kv = s.dep.KV()
|
s.kv = s.dep.KV()
|
||||||
// delete all cached settings
|
// delete all cached settings
|
||||||
_ = s.kv.Delete(setting.KvSettingPrefix)
|
_ = 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.
|
// TODO: make sure redis is connected in dep before user traffic.
|
||||||
if s.config.System().Mode == conf.MasterMode {
|
if s.config.System().Mode == conf.MasterMode {
|
||||||
|
|
|
@ -3,12 +3,13 @@ package cache
|
||||||
import (
|
import (
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/cloudreve/Cloudreve/v4/pkg/logging"
|
|
||||||
"github.com/cloudreve/Cloudreve/v4/pkg/util"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/cloudreve/Cloudreve/v4/pkg/logging"
|
||||||
|
"github.com/cloudreve/Cloudreve/v4/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MemoStore 内存存储驱动
|
// MemoStore 内存存储驱动
|
||||||
|
@ -55,11 +56,11 @@ func getValue(item any, ok bool) (any, bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GarbageCollect 回收已过期的缓存
|
// GarbageCollect 回收已过期的缓存
|
||||||
func (store *MemoStore) GarbageCollect() {
|
func (store *MemoStore) GarbageCollect(l logging.Logger) {
|
||||||
store.Store.Range(func(key, value any) bool {
|
store.Store.Range(func(key, value any) bool {
|
||||||
if item, ok := value.(itemWithTTL); ok {
|
if item, ok := value.(itemWithTTL); ok {
|
||||||
if item.Expires > 0 && item.Expires < time.Now().Unix() {
|
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)
|
store.Store.Delete(key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ import (
|
||||||
"github.com/cloudreve/Cloudreve/v4/ent/task"
|
"github.com/cloudreve/Cloudreve/v4/ent/task"
|
||||||
"github.com/cloudreve/Cloudreve/v4/inventory"
|
"github.com/cloudreve/Cloudreve/v4/inventory"
|
||||||
"github.com/cloudreve/Cloudreve/v4/inventory/types"
|
"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/crontab"
|
||||||
"github.com/cloudreve/Cloudreve/v4/pkg/filemanager/fs"
|
"github.com/cloudreve/Cloudreve/v4/pkg/filemanager/fs"
|
||||||
"github.com/cloudreve/Cloudreve/v4/pkg/filemanager/fs/dbfs"
|
"github.com/cloudreve/Cloudreve/v4/pkg/filemanager/fs/dbfs"
|
||||||
|
@ -296,6 +297,12 @@ const (
|
||||||
func CronCollectTrashBin(ctx context.Context) {
|
func CronCollectTrashBin(ctx context.Context) {
|
||||||
dep := dependency.FromContext(ctx)
|
dep := dependency.FromContext(ctx)
|
||||||
l := dep.Logger()
|
l := dep.Logger()
|
||||||
|
|
||||||
|
kv := dep.KV()
|
||||||
|
if memKv, ok := kv.(*cache.MemoStore); ok {
|
||||||
|
memKv.GarbageCollect(l)
|
||||||
|
}
|
||||||
|
|
||||||
fm := NewFileManager(dep, inventory.UserFromContext(ctx)).(*manager)
|
fm := NewFileManager(dep, inventory.UserFromContext(ctx)).(*manager)
|
||||||
pageSize := dep.SettingProvider().DBFS(ctx).MaxPageSize
|
pageSize := dep.SettingProvider().DBFS(ctx).MaxPageSize
|
||||||
batch := 0
|
batch := 0
|
||||||
|
|
Loading…
Reference in New Issue