From 8561ff3e2daa87c7369d1a5e5cf6f35f59cf1336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Wed, 4 Oct 2023 18:13:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=BC=93=E5=AD=98=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=8A=A0=E8=BD=BD=E7=83=AD=E9=97=A8=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=97=B6=E6=A3=80=E6=9F=A5=E6=98=AF=E5=90=A6=E6=9C=89=E8=B6=B3?= =?UTF-8?q?=E5=A4=9F=E7=9A=84=E5=86=85=E5=AD=98=E7=A9=BA=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/caches/storage_file.go | 7 ++++++- internal/caches/storage_memory.go | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/caches/storage_file.go b/internal/caches/storage_file.go index 273a872..413ceb4 100644 --- a/internal/caches/storage_file.go +++ b/internal/caches/storage_file.go @@ -1156,11 +1156,16 @@ func (this *FileStorage) purgeLoop() { // 热点数据任务 func (this *FileStorage) hotLoop() { - var memoryStorage = this.memoryStorage + var memoryStorage = this.memoryStorage // copy if memoryStorage == nil { return } + // check memory space size + if !memoryStorage.HasFreeSpaceForHotItems() { + return + } + this.hotMapLocker.Lock() if len(this.hotMap) == 0 { this.hotMapLocker.Unlock() diff --git a/internal/caches/storage_memory.go b/internal/caches/storage_memory.go index ba4af46..65d9678 100644 --- a/internal/caches/storage_memory.go +++ b/internal/caches/storage_memory.go @@ -400,6 +400,11 @@ func (this *MemoryStorage) CanSendfile() bool { return false } +// HasFreeSpaceForHotItems 是否有足够的空间提供给热门内容 +func (this *MemoryStorage) HasFreeSpaceForHotItems() bool { + return atomic.LoadInt64(&this.usedSize) < this.memoryCapacityBytes()*3/4 +} + // 计算Key Hash func (this *MemoryStorage) hash(key string) uint64 { return xxhash.Sum64String(key)