From be43aae929fbdbba6461b5ff55659900ca56c6b7 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Tue, 10 Oct 2023 14:45:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E7=BC=93=E5=AD=98=E5=86=99=E5=85=A5?= =?UTF-8?q?=E5=86=85=E5=AD=98=E4=B9=8B=E5=89=8D=E6=A3=80=E6=9F=A5=E7=A3=81?= =?UTF-8?q?=E7=9B=98=E6=98=AF=E5=90=A6=E8=B6=85=E5=87=BA=E5=AE=B9=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/caches/storage_file.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/internal/caches/storage_file.go b/internal/caches/storage_file.go index 0a2046c..2ce3890 100644 --- a/internal/caches/storage_file.go +++ b/internal/caches/storage_file.go @@ -422,6 +422,13 @@ func (this *FileStorage) openWriter(key string, expiredAt int64, status int, hea return nil, ErrEntityTooLarge } + // 检查磁盘是否超出容量 + // 需要在内存缓存之前执行,避免成功写进到了内存缓存,但无法刷到磁盘 + var capacityBytes = this.diskCapacityBytes() + if capacityBytes > 0 && capacityBytes <= this.TotalDiskSize()+(32<<20 /** 余量 **/) { + return nil, NewCapacityError("write file cache failed: over disk size, current: " + types.String(this.TotalDiskSize()) + ", capacity: " + types.String(capacityBytes)) + } + // 先尝试内存缓存 // 我们限定仅小文件优先存在内存中 var maxMemorySize = FileToMemoryMaxSize @@ -465,12 +472,6 @@ func (this *FileStorage) openWriter(key string, expiredAt int64, status int, hea } }() - // 检查是否超出容量 - var capacityBytes = this.diskCapacityBytes() - if capacityBytes > 0 && capacityBytes <= this.TotalDiskSize() { - return nil, NewCapacityError("write file cache failed: over disk size, current total size: " + types.String(this.TotalDiskSize()) + " bytes, capacity: " + types.String(capacityBytes)) - } - var hash = stringutil.Md5(key) dir, diskIsFull := this.subDir(hash) @@ -1146,7 +1147,7 @@ func (this *FileStorage) purgeLoop() { if requireFullLFU { prefix = "fully " } - remotelogs.Println("CACHE", prefix+"LFU purge policy '"+this.policy.Name+"' id: "+types.String(this.policy.Id)+", count: "+types.String(count)+", cost:"+fmt.Sprintf("%.2fms", time.Since(before).Seconds()*1000)) + remotelogs.Println("CACHE", prefix+"LFU purge policy '"+this.policy.Name+"' id: "+types.String(this.policy.Id)+", count: "+types.String(count)+", cost: "+fmt.Sprintf("%.2fms", time.Since(before).Seconds()*1000)) if err != nil { remotelogs.Warn("CACHE", "purge file storage in LFU failed: "+err.Error())