From 3888565c0fbc1b124621b5b7dbd6d1c2a797b49f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Mon, 4 Oct 2021 09:12:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E7=B3=BB=E7=BB=9F=E5=86=85?= =?UTF-8?q?=E5=AD=98=E8=87=AA=E5=8A=A8=E8=B0=83=E8=8A=82ttlcache=E7=9A=84?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E6=9D=A1=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/node_status_executor_unix.go | 1 + internal/ttlcache/cache.go | 12 ++++++++--- internal/ttlcache/cache_test.go | 9 ++++++++ internal/ttlcache/piece.go | 3 ++- internal/ttlcache/system.go | 23 +++++++++++++++++++++ internal/ttlcache/system_test.go | 11 ++++++++++ 6 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 internal/ttlcache/system.go create mode 100644 internal/ttlcache/system_test.go diff --git a/internal/nodes/node_status_executor_unix.go b/internal/nodes/node_status_executor_unix.go index 6a56ba8..bebe9a9 100644 --- a/internal/nodes/node_status_executor_unix.go +++ b/internal/nodes/node_status_executor_unix.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows package nodes diff --git a/internal/ttlcache/cache.go b/internal/ttlcache/cache.go index 3ac5249..d1e1f77 100644 --- a/internal/ttlcache/cache.go +++ b/internal/ttlcache/cache.go @@ -24,7 +24,13 @@ type Cache struct { func NewCache(opt ...OptionInterface) *Cache { countPieces := 128 - maxItems := 10_000_000 + maxItems := 2_000_000 + + var delta = systemMemoryGB() / 4 + if delta > 0 { + maxItems *= delta + } + for _, option := range opt { if option == nil { continue @@ -61,7 +67,7 @@ func NewCache(opt ...OptionInterface) *Cache { return cache } -func (this *Cache) Write(key string, value interface{}, expiredAt int64) { +func (this *Cache) Write(key string, value interface{}, expiredAt int64) (ok bool) { if this.isDestroyed { return } @@ -77,7 +83,7 @@ func (this *Cache) Write(key string, value interface{}, expiredAt int64) { } uint64Key := HashKey([]byte(key)) pieceIndex := uint64Key % this.countPieces - this.pieces[pieceIndex].Add(uint64Key, &Item{ + return this.pieces[pieceIndex].Add(uint64Key, &Item{ Value: value, expiredAt: expiredAt, }) diff --git a/internal/ttlcache/cache_test.go b/internal/ttlcache/cache_test.go index 11c74c8..ffd11c2 100644 --- a/internal/ttlcache/cache_test.go +++ b/internal/ttlcache/cache_test.go @@ -28,6 +28,15 @@ func TestNewCache(t *testing.T) { t.Log(cache.Count(), "items") } +func TestCache_Memory(t *testing.T) { + cache := NewCache() + for i := 0; i < 20_000_000; i++ { + cache.Write("a"+strconv.Itoa(i), 1, time.Now().Unix()+3600) + } + t.Log("waiting ...") + time.Sleep(10 * time.Second) +} + func BenchmarkCache_Add(b *testing.B) { runtime.GOMAXPROCS(1) diff --git a/internal/ttlcache/piece.go b/internal/ttlcache/piece.go index 6677971..a6904c0 100644 --- a/internal/ttlcache/piece.go +++ b/internal/ttlcache/piece.go @@ -17,7 +17,7 @@ func NewPiece(maxItems int) *Piece { return &Piece{m: map[uint64]*Item{}, maxItems: maxItems} } -func (this *Piece) Add(key uint64, item *Item) { +func (this *Piece) Add(key uint64, item *Item) (ok bool) { this.locker.Lock() if len(this.m) >= this.maxItems { this.locker.Unlock() @@ -25,6 +25,7 @@ func (this *Piece) Add(key uint64, item *Item) { } this.m[key] = item this.locker.Unlock() + return true } func (this *Piece) IncreaseInt64(key uint64, delta int64, expiredAt int64) (result int64) { diff --git a/internal/ttlcache/system.go b/internal/ttlcache/system.go new file mode 100644 index 0000000..8a943dc --- /dev/null +++ b/internal/ttlcache/system.go @@ -0,0 +1,23 @@ +// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. + +package ttlcache + +import ( + "github.com/shirou/gopsutil/mem" +) + +var systemTotalMemory = -1 + +func systemMemoryGB() int { + if systemTotalMemory > 0 { + return systemTotalMemory + } + + stat, err := mem.VirtualMemory() + if err != nil { + return 0 + } + + systemTotalMemory = int(stat.Total / 1024 / 1024 / 1024) + return systemTotalMemory +} diff --git a/internal/ttlcache/system_test.go b/internal/ttlcache/system_test.go new file mode 100644 index 0000000..fd3fb8f --- /dev/null +++ b/internal/ttlcache/system_test.go @@ -0,0 +1,11 @@ +// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. + +package ttlcache + +import "testing" + +func TestSystemMemoryGB(t *testing.T) { + t.Log(systemMemoryGB()) + t.Log(systemMemoryGB()) + t.Log(systemMemoryGB()) +}