From 42710c4e36801c4bd189e1d0794b05e8c0e0ef3c Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Wed, 29 Dec 2021 10:53:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E8=8E=B7=E5=8F=96=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E5=86=85=E5=AD=98=E5=87=BD=E6=95=B0=E6=94=BE=E5=85=A5=E5=88=B0?= =?UTF-8?q?utils=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/ttlcache/cache.go | 3 ++- internal/{ttlcache => utils}/system.go | 8 ++++++-- internal/{ttlcache => utils}/system_test.go | 8 ++++---- 3 files changed, 12 insertions(+), 7 deletions(-) rename internal/{ttlcache => utils}/system.go (81%) rename internal/{ttlcache => utils}/system_test.go (59%) 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()) }