diff --git a/internal/ttlcache/cache.go b/internal/ttlcache/cache.go index d36d83f..84a0d14 100644 --- a/internal/ttlcache/cache.go +++ b/internal/ttlcache/cache.go @@ -1,6 +1,7 @@ package ttlcache import ( + "github.com/TeaOSLab/EdgeNode/internal/utils" "time" ) @@ -24,7 +25,7 @@ func NewCache(opt ...OptionInterface) *Cache { var countPieces = 128 var maxItems = 2_000_000 - var totalMemory = systemMemoryGB() + var totalMemory = utils.SystemMemoryGB() if totalMemory < 2 { // 我们限制内存过小的服务能够使用的数量 maxItems = 1_000_000 diff --git a/internal/ttlcache/system.go b/internal/utils/system.go similarity index 81% rename from internal/ttlcache/system.go rename to internal/utils/system.go index 8a943dc..f70c553 100644 --- a/internal/ttlcache/system.go +++ b/internal/utils/system.go @@ -1,6 +1,6 @@ // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. -package ttlcache +package utils import ( "github.com/shirou/gopsutil/mem" @@ -8,7 +8,11 @@ import ( var systemTotalMemory = -1 -func systemMemoryGB() int { +func init() { + _ = SystemMemoryGB() +} + +func SystemMemoryGB() int { if systemTotalMemory > 0 { return systemTotalMemory } diff --git a/internal/ttlcache/system_test.go b/internal/utils/system_test.go similarity index 59% rename from internal/ttlcache/system_test.go rename to internal/utils/system_test.go index fd3fb8f..df15ca1 100644 --- a/internal/ttlcache/system_test.go +++ b/internal/utils/system_test.go @@ -1,11 +1,11 @@ // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. -package ttlcache +package utils import "testing" func TestSystemMemoryGB(t *testing.T) { - t.Log(systemMemoryGB()) - t.Log(systemMemoryGB()) - t.Log(systemMemoryGB()) + t.Log(SystemMemoryGB()) + t.Log(SystemMemoryGB()) + t.Log(SystemMemoryGB()) }