From d38d2c617dde8477e9752c3aa4436a4d5575d9c3 Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Thu, 30 May 2024 18:09:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E6=B8=85=E7=90=86=E7=BC=93=E5=AD=98=20(#5226)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/cron/cron.go | 3 +++ backend/cron/job/cache.go | 26 ++++++++++++++++++++++++++ backend/global/global.go | 2 ++ backend/init/cache/cache.go | 3 ++- 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 backend/cron/job/cache.go 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") }