Compare commits

..

3 Commits

Author SHA1 Message Date
刘祥超
822e967874 优化文件句柄缓存容量判断 2023-10-17 09:59:04 +08:00
刘祥超
2acf890b8e 限制内存缓存最大容量为系统内存的三分之一 2023-10-16 14:28:07 +08:00
刘祥超
3909695b44 优化代码 2023-10-16 11:48:38 +08:00
5 changed files with 32 additions and 15 deletions

View File

@@ -9,11 +9,13 @@ import (
"sync"
)
const HashMapSharding = 11
const HashMapSharding = 31
var bigIntPool = sync.Pool{New: func() any {
return big.NewInt(0)
}}
var bigIntPool = sync.Pool{
New: func() any {
return big.NewInt(0)
},
}
// FileListHashMap 文件Hash列表
type FileListHashMap struct {
@@ -63,7 +65,7 @@ func (this *FileListHashMap) Load(db *FileListDB) error {
this.AddHashes(hashList)
lastId = maxId
maxLoops --
maxLoops--
if maxLoops <= 0 {
break
}

View File

@@ -93,7 +93,7 @@ func (this *OpenFileCache) Put(filename string, file *OpenFile) {
defer this.locker.Unlock()
// 如果超过当前容量,则关闭最早的
if this.count >= this.maxCount || this.usedSize >= this.capacitySize {
if this.count >= this.maxCount || this.usedSize+file.size >= this.capacitySize {
this.consumeHead()
return
}

View File

@@ -584,13 +584,19 @@ func (this *MemoryStorage) flushItem(key string) {
}
func (this *MemoryStorage) memoryCapacityBytes() int64 {
var maxSystemBytes = int64(utils.SystemMemoryBytes()) / 3 // 1/3 of the system memory
if this.policy == nil {
return 0
return maxSystemBytes
}
if SharedManager.MaxMemoryCapacity != nil {
var capacityBytes = SharedManager.MaxMemoryCapacity.Bytes()
if capacityBytes > 0 {
if capacityBytes > maxSystemBytes {
return maxSystemBytes
}
return capacityBytes
}
}
@@ -599,17 +605,15 @@ func (this *MemoryStorage) memoryCapacityBytes() int64 {
if capacity != nil {
var capacityBytes = capacity.Bytes()
if capacityBytes > 0 {
if capacityBytes > maxSystemBytes {
return maxSystemBytes
}
return capacityBytes
}
}
// half of the system memory
var memoryGB = utils.SystemMemoryGB()
if memoryGB < 1 {
memoryGB = 1
}
return int64(memoryGB) << 30 / 2
// 1/4 of the system memory
return maxSystemBytes
}
func (this *MemoryStorage) deleteWithoutLocker(key string) error {

View File

@@ -8,6 +8,7 @@ import (
)
var systemTotalMemory = -1
var systemMemoryBytes uint64
func init() {
if !teaconst.IsMain {
@@ -29,7 +30,9 @@ func SystemMemoryGB() int {
return 1
}
systemTotalMemory = int(stat.Total / (1<<30))
systemMemoryBytes = stat.Total
systemTotalMemory = int(stat.Total / (1 << 30))
if systemTotalMemory <= 0 {
systemTotalMemory = 1
}
@@ -38,3 +41,8 @@ func SystemMemoryGB() int {
return systemTotalMemory
}
// SystemMemoryBytes 系统内存总字节数
func SystemMemoryBytes() uint64 {
return systemMemoryBytes
}

View File

@@ -8,4 +8,7 @@ func TestSystemMemoryGB(t *testing.T) {
t.Log(SystemMemoryGB())
t.Log(SystemMemoryGB())
t.Log(SystemMemoryGB())
t.Log(SystemMemoryBytes())
t.Log(SystemMemoryBytes())
t.Log(SystemMemoryBytes()>>30, "GB")
}