加快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() { func (this *Cache) GC() {
this.pieces[this.gcPieceIndex].GC() var index = this.gcPieceIndex
var newIndex = this.gcPieceIndex + 1 var maxPiecesPerGC = 4
if newIndex >= int(this.countPieces) { for i := index; i < index+maxPiecesPerGC; i++ {
newIndex = 0 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() { func (this *Cache) Clean() {