增加ttlcache默认最大容量

This commit is contained in:
刘祥超
2023-04-25 17:24:05 +08:00
parent 6fce430469
commit 7a4b68de97
2 changed files with 14 additions and 4 deletions

View File

@@ -5,7 +5,7 @@ import (
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime" "github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
) )
var SharedCache = NewCache() var SharedCache = NewBigCache()
// Cache TTL缓存 // Cache TTL缓存
// 最大的缓存时间为30 * 86400 // 最大的缓存时间为30 * 86400
@@ -25,6 +25,14 @@ type Cache struct {
gcPieceIndex int gcPieceIndex int
} }
func NewBigCache() *Cache {
var delta = utils.SystemMemoryGB() / 2
if delta <= 0 {
delta = 1
}
return NewCache(NewMaxItemsOption(delta * 1_000_000))
}
func NewCache(opt ...OptionInterface) *Cache { func NewCache(opt ...OptionInterface) *Cache {
var countPieces = 256 var countPieces = 256
var maxItems = 1_000_000 var maxItems = 1_000_000
@@ -34,7 +42,7 @@ func NewCache(opt ...OptionInterface) *Cache {
// 我们限制内存过小的服务能够使用的数量 // 我们限制内存过小的服务能够使用的数量
maxItems = 500_000 maxItems = 500_000
} else { } else {
var delta = totalMemory / 8 var delta = totalMemory / 4
if delta > 0 { if delta > 0 {
maxItems *= delta maxItems *= delta
} }

View File

@@ -17,6 +17,8 @@ func init() {
_ = SystemMemoryGB() _ = SystemMemoryGB()
} }
// SystemMemoryGB 系统内存GB数量
// 必须保证不小于1
func SystemMemoryGB() int { func SystemMemoryGB() int {
if systemTotalMemory > 0 { if systemTotalMemory > 0 {
return systemTotalMemory return systemTotalMemory
@@ -24,10 +26,10 @@ func SystemMemoryGB() int {
stat, err := mem.VirtualMemory() stat, err := mem.VirtualMemory()
if err != nil { if err != nil {
return 0 return 1
} }
systemTotalMemory = int(stat.Total / 1024 / 1024 / 1024) systemTotalMemory = int(stat.Total / (1<<30))
if systemTotalMemory <= 0 { if systemTotalMemory <= 0 {
systemTotalMemory = 1 systemTotalMemory = 1
} }