deleteExpired deletes expired records from the oldest bucket, waiting for the newest entry in it to expire first.
()
| 322 | // deleteExpired deletes expired records from the oldest bucket, waiting for the newest entry |
| 323 | // in it to expire first. |
| 324 | func (c *LRU[K, V]) deleteExpired() { |
| 325 | c.mu.Lock() |
| 326 | bucketIdx := c.nextCleanupBucket |
| 327 | timeToExpire := time.Until(c.buckets[bucketIdx].newestEntry) |
| 328 | // wait for newest entry to expire before cleanup without holding lock |
| 329 | if timeToExpire > 0 { |
| 330 | c.mu.Unlock() |
| 331 | time.Sleep(timeToExpire) |
| 332 | c.mu.Lock() |
| 333 | } |
| 334 | for _, ent := range c.buckets[bucketIdx].entries { |
| 335 | c.removeElement(ent) |
| 336 | } |
| 337 | c.nextCleanupBucket = (c.nextCleanupBucket + 1) % numBuckets |
| 338 | c.mu.Unlock() |
| 339 | } |
| 340 | |
| 341 | // addToBucket adds entry to expire bucket so that it will be cleaned up when the time comes. Has to be called with lock! |
| 342 | func (c *LRU[K, V]) addToBucket(e *Entry[K, V]) { |