diff --git a/backend/cron/cron.go b/backend/cron/cron.go index 93cb9c2c8..5014571be 100644 --- a/backend/cron/cron.go +++ b/backend/cron/cron.go @@ -46,6 +46,9 @@ func Run() { if _, err := global.Cron.AddJob(fmt.Sprintf("%v %v * * *", mathRand.Intn(60), mathRand.Intn(3)), job.NewAppStoreJob()); err != nil { global.LOG.Errorf("can not add appstore corn job: %s", err.Error()) } + if _, err := global.Cron.AddJob("@daily", job.NewCacheJob()); err != nil { + global.LOG.Errorf("can not add cache corn job: %s", err.Error()) + } var backup model.BackupAccount _ = global.DB.Where("type = ?", "OneDrive").Find(&backup).Error diff --git a/backend/cron/job/cache.go b/backend/cron/job/cache.go new file mode 100644 index 000000000..2fb3cd643 --- /dev/null +++ b/backend/cron/job/cache.go @@ -0,0 +1,26 @@ +package job + +import ( + "github.com/1Panel-dev/1Panel/backend/global" + "time" +) + +type Cache struct{} + +func NewCacheJob() *Cache { + return &Cache{} +} + +func (c *Cache) Run() { + global.LOG.Info("run cache gc start ...") + ticker := time.NewTicker(5 * time.Minute) + defer ticker.Stop() + for range ticker.C { + again: + err := global.CacheDb.RunValueLogGC(0.7) + if err == nil { + goto again + } + } + global.LOG.Info("run cache gc end ...") +} diff --git a/backend/global/global.go b/backend/global/global.go index 964f40f27..24b660cad 100644 --- a/backend/global/global.go +++ b/backend/global/global.go @@ -4,6 +4,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/configs" "github.com/1Panel-dev/1Panel/backend/init/cache/badger_db" "github.com/1Panel-dev/1Panel/backend/init/session/psession" + "github.com/dgraph-io/badger/v4" "github.com/go-playground/validator/v10" "github.com/nicksnyder/go-i18n/v2/i18n" "github.com/robfig/cron/v3" @@ -20,6 +21,7 @@ var ( VALID *validator.Validate SESSION *psession.PSession CACHE *badger_db.Cache + CacheDb *badger.DB Viper *viper.Viper Cron *cron.Cron diff --git a/backend/init/cache/cache.go b/backend/init/cache/cache.go index 4d8dad440..1b64e1f72 100644 --- a/backend/init/cache/cache.go +++ b/backend/init/cache/cache.go @@ -49,7 +49,8 @@ func Init() { if err != nil { panic(err) } - + _ = cache.DropAll() + global.CacheDb = cache global.CACHE = badger_db.NewCacheDB(cache) global.LOG.Info("init cache successfully") }