diff --git a/internal/ttlcache/cache.go b/internal/ttlcache/cache.go index c71dc6f..3b5b05b 100644 --- a/internal/ttlcache/cache.go +++ b/internal/ttlcache/cache.go @@ -5,7 +5,7 @@ import ( "github.com/TeaOSLab/EdgeNode/internal/utils/fasttime" ) -var SharedCache = NewCache() +var SharedCache = NewBigCache() // Cache TTL缓存 // 最大的缓存时间为30 * 86400 @@ -25,6 +25,14 @@ type Cache struct { 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 { var countPieces = 256 var maxItems = 1_000_000 @@ -34,7 +42,7 @@ func NewCache(opt ...OptionInterface) *Cache { // 我们限制内存过小的服务能够使用的数量 maxItems = 500_000 } else { - var delta = totalMemory / 8 + var delta = totalMemory / 4 if delta > 0 { maxItems *= delta } diff --git a/internal/utils/system.go b/internal/utils/system.go index 031d4fa..aec07e5 100644 --- a/internal/utils/system.go +++ b/internal/utils/system.go @@ -17,6 +17,8 @@ func init() { _ = SystemMemoryGB() } +// SystemMemoryGB 系统内存GB数量 +// 必须保证不小于1 func SystemMemoryGB() int { if systemTotalMemory > 0 { return systemTotalMemory @@ -24,10 +26,10 @@ func SystemMemoryGB() int { stat, err := mem.VirtualMemory() if err != nil { - return 0 + return 1 } - systemTotalMemory = int(stat.Total / 1024 / 1024 / 1024) + systemTotalMemory = int(stat.Total / (1<<30)) if systemTotalMemory <= 0 { systemTotalMemory = 1 }