feat: 增加定时清理缓存 (#5226)

This commit is contained in:
zhengkunwang 2024-05-30 18:09:15 +08:00 committed by GitHub
parent 90bcf464d3
commit d38d2c617d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 33 additions and 1 deletions

View File

@ -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

26
backend/cron/job/cache.go Normal file
View File

@ -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 ...")
}

View File

@ -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

View File

@ -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")
}