mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-14 07:30:25 +08:00
根据系统内存自动调节ttlcache的最大条目
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package nodes
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
23
internal/ttlcache/system.go
Normal file
23
internal/ttlcache/system.go
Normal file
@@ -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
|
||||
}
|
||||
11
internal/ttlcache/system_test.go
Normal file
11
internal/ttlcache/system_test.go
Normal file
@@ -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())
|
||||
}
|
||||
Reference in New Issue
Block a user