加快ttlcache GC速度

This commit is contained in:
刘祥超
2023-04-25 19:10:37 +08:00
parent f2df4a1560
commit e75010692c

View File

@@ -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.gcPieceIndex = newIndex
this.pieces[i].GC()
}
index += maxPiecesPerGC
if index >= int(this.countPieces) {
index = 0
}
this.gcPieceIndex = index
}
func (this *Cache) Clean() {