2022-08-16 15:30:23 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2024-09-04 09:21:43 +00:00
|
|
|
"os"
|
2023-01-09 14:55:10 +00:00
|
|
|
"time"
|
|
|
|
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/init/cache/badger_db"
|
2023-05-09 07:23:43 +00:00
|
|
|
"github.com/dgraph-io/badger/v4"
|
2022-08-16 15:30:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Init() {
|
2023-01-09 14:55:10 +00:00
|
|
|
c := global.CONF.System.Cache
|
2024-09-04 09:21:43 +00:00
|
|
|
_ = os.RemoveAll(c)
|
2024-09-05 09:23:47 +00:00
|
|
|
_ = os.Mkdir(c, 0755)
|
2022-08-25 09:54:52 +00:00
|
|
|
options := badger.Options{
|
2023-01-09 14:55:10 +00:00
|
|
|
Dir: c,
|
|
|
|
ValueDir: c,
|
2023-06-01 06:45:18 +00:00
|
|
|
ValueLogFileSize: 64 << 20,
|
|
|
|
ValueLogMaxEntries: 10 << 20,
|
2022-08-25 09:54:52 +00:00
|
|
|
VLogPercentile: 0.1,
|
|
|
|
|
2023-06-01 06:45:18 +00:00
|
|
|
MemTableSize: 32 << 20,
|
2022-08-25 09:54:52 +00:00
|
|
|
BaseTableSize: 2 << 20,
|
|
|
|
BaseLevelSize: 10 << 20,
|
|
|
|
TableSizeMultiplier: 2,
|
|
|
|
LevelSizeMultiplier: 10,
|
|
|
|
MaxLevels: 7,
|
2023-06-01 06:45:18 +00:00
|
|
|
NumGoroutines: 4,
|
2022-08-25 09:54:52 +00:00
|
|
|
MetricsEnabled: true,
|
2023-06-01 06:45:18 +00:00
|
|
|
NumCompactors: 2,
|
2022-08-25 09:54:52 +00:00
|
|
|
NumLevelZeroTables: 5,
|
|
|
|
NumLevelZeroTablesStall: 15,
|
2023-06-01 06:45:18 +00:00
|
|
|
NumMemtables: 1,
|
2022-08-25 09:54:52 +00:00
|
|
|
BloomFalsePositive: 0.01,
|
2023-06-01 06:45:18 +00:00
|
|
|
BlockSize: 2 * 1024,
|
2022-08-25 09:54:52 +00:00
|
|
|
SyncWrites: false,
|
|
|
|
NumVersionsToKeep: 1,
|
|
|
|
CompactL0OnClose: false,
|
|
|
|
VerifyValueChecksum: false,
|
2023-06-01 06:45:18 +00:00
|
|
|
BlockCacheSize: 32 << 20,
|
2022-08-25 09:54:52 +00:00
|
|
|
IndexCacheSize: 0,
|
|
|
|
ZSTDCompressionLevel: 1,
|
|
|
|
EncryptionKey: []byte{},
|
|
|
|
EncryptionKeyRotationDuration: 10 * 24 * time.Hour, // Default 10 days.
|
|
|
|
DetectConflicts: true,
|
|
|
|
NamespaceOffset: -1,
|
|
|
|
}
|
|
|
|
|
|
|
|
cache, err := badger.Open(options)
|
2022-08-16 15:30:23 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-05-30 10:09:15 +00:00
|
|
|
_ = cache.DropAll()
|
|
|
|
global.CacheDb = cache
|
2022-08-16 15:30:23 +00:00
|
|
|
global.CACHE = badger_db.NewCacheDB(cache)
|
2023-03-01 10:17:31 +00:00
|
|
|
global.LOG.Info("init cache successfully")
|
2022-08-16 15:30:23 +00:00
|
|
|
}
|