将获取系统内存函数放入到utils中

This commit is contained in:
GoEdgeLab
2021-12-29 10:53:59 +08:00
parent 1a22d2959d
commit 42710c4e36
3 changed files with 12 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
package ttlcache package ttlcache
import ( import (
"github.com/TeaOSLab/EdgeNode/internal/utils"
"time" "time"
) )
@@ -24,7 +25,7 @@ func NewCache(opt ...OptionInterface) *Cache {
var countPieces = 128 var countPieces = 128
var maxItems = 2_000_000 var maxItems = 2_000_000
var totalMemory = systemMemoryGB() var totalMemory = utils.SystemMemoryGB()
if totalMemory < 2 { if totalMemory < 2 {
// 我们限制内存过小的服务能够使用的数量 // 我们限制内存过小的服务能够使用的数量
maxItems = 1_000_000 maxItems = 1_000_000

View File

@@ -1,6 +1,6 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package ttlcache package utils
import ( import (
"github.com/shirou/gopsutil/mem" "github.com/shirou/gopsutil/mem"
@@ -8,7 +8,11 @@ import (
var systemTotalMemory = -1 var systemTotalMemory = -1
func systemMemoryGB() int { func init() {
_ = SystemMemoryGB()
}
func SystemMemoryGB() int {
if systemTotalMemory > 0 { if systemTotalMemory > 0 {
return systemTotalMemory return systemTotalMemory
} }

View File

@@ -1,11 +1,11 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package ttlcache package utils
import "testing" import "testing"
func TestSystemMemoryGB(t *testing.T) { func TestSystemMemoryGB(t *testing.T) {
t.Log(systemMemoryGB()) t.Log(SystemMemoryGB())
t.Log(systemMemoryGB()) t.Log(SystemMemoryGB())
t.Log(systemMemoryGB()) t.Log(SystemMemoryGB())
} }