文件缓存自动加载热门数据时检查是否有足够的内存空间

This commit is contained in:
GoEdgeLab
2023-10-04 18:13:48 +08:00
parent c352de08bb
commit 3851a5c424
2 changed files with 11 additions and 1 deletions

View File

@@ -1156,11 +1156,16 @@ func (this *FileStorage) purgeLoop() {
// 热点数据任务 // 热点数据任务
func (this *FileStorage) hotLoop() { func (this *FileStorage) hotLoop() {
var memoryStorage = this.memoryStorage var memoryStorage = this.memoryStorage // copy
if memoryStorage == nil { if memoryStorage == nil {
return return
} }
// check memory space size
if !memoryStorage.HasFreeSpaceForHotItems() {
return
}
this.hotMapLocker.Lock() this.hotMapLocker.Lock()
if len(this.hotMap) == 0 { if len(this.hotMap) == 0 {
this.hotMapLocker.Unlock() this.hotMapLocker.Unlock()

View File

@@ -400,6 +400,11 @@ func (this *MemoryStorage) CanSendfile() bool {
return false return false
} }
// HasFreeSpaceForHotItems 是否有足够的空间提供给热门内容
func (this *MemoryStorage) HasFreeSpaceForHotItems() bool {
return atomic.LoadInt64(&this.usedSize) < this.memoryCapacityBytes()*3/4
}
// 计算Key Hash // 计算Key Hash
func (this *MemoryStorage) hash(key string) uint64 { func (this *MemoryStorage) hash(key string) uint64 {
return xxhash.Sum64String(key) return xxhash.Sum64String(key)