From e75010692cd3a463d22ed124eea9941ea0aa915c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Tue, 25 Apr 2023 19:10:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=BF=ABttlcache=20GC=E9=80=9F?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/ttlcache/cache.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/internal/ttlcache/cache.go b/internal/ttlcache/cache.go index 3b5b05b..73a3453 100644 --- a/internal/ttlcache/cache.go +++ b/internal/ttlcache/cache.go @@ -146,12 +146,20 @@ func (this *Cache) Count() (count int) { } func (this *Cache) GC() { - this.pieces[this.gcPieceIndex].GC() - var newIndex = this.gcPieceIndex + 1 - if newIndex >= int(this.countPieces) { - newIndex = 0 + var index = this.gcPieceIndex + var maxPiecesPerGC = 4 + for i := index; i < index+maxPiecesPerGC; i++ { + if i >= int(this.countPieces) { + break + } + this.pieces[i].GC() } - this.gcPieceIndex = newIndex + + index += maxPiecesPerGC + if index >= int(this.countPieces) { + index = 0 + } + this.gcPieceIndex = index } func (this *Cache) Clean() {