@@ -280,15 +280,17 @@ type lruCache[V any] struct {
280
280
metrics ExpandedPostingsCacheMetrics
281
281
282
282
// Fields from here should be locked
283
- cachedMtx sync.RWMutex
284
- cached * list.List
283
+ cachedMtx sync.RWMutex
284
+ // Keeps tracks of the last recent used keys.
285
+ // The most recent key used is placed in the back of the list while items should be evicted from the front of the list
286
+ lruList * list.List
285
287
cachedBytes int64
286
288
}
287
289
288
290
func newLruCache [V any ](cfg PostingsCacheConfig , name string , metrics * ExpandedPostingsCacheMetrics , timeNow func () time.Time ) * lruCache [V ] {
289
291
return & lruCache [V ]{
290
292
cachedValues : new (sync.Map ),
291
- cached : list .New (),
293
+ lruList : list .New (),
292
294
cfg : cfg ,
293
295
timeNow : timeNow ,
294
296
name : name ,
@@ -361,7 +363,7 @@ func (c *lruCache[V]) contains(k string) bool {
361
363
}
362
364
363
365
func (c * lruCache [V ]) shouldEvictHead () (string , bool ) {
364
- h := c .cached .Front ()
366
+ h := c .lruList .Front ()
365
367
if h == nil {
366
368
return "" , false
367
369
}
@@ -382,8 +384,8 @@ func (c *lruCache[V]) shouldEvictHead() (string, bool) {
382
384
}
383
385
384
386
func (c * lruCache [V ]) evictHead () {
385
- front := c .cached .Front ()
386
- c .cached .Remove (front )
387
+ front := c .lruList .Front ()
388
+ c .lruList .Remove (front )
387
389
oldestKey := front .Value .(string )
388
390
if oldest , loaded := c .cachedValues .LoadAndDelete (oldestKey ); loaded {
389
391
c .cachedBytes -= oldest .(* cacheEntryPromise [V ]).sizeBytes
@@ -398,13 +400,13 @@ func (c *lruCache[V]) created(key string, sizeBytes int64) *list.Element {
398
400
c .cachedMtx .Lock ()
399
401
defer c .cachedMtx .Unlock ()
400
402
c .cachedBytes += sizeBytes
401
- return c .cached .PushBack (key )
403
+ return c .lruList .PushBack (key )
402
404
}
403
405
404
406
func (c * lruCache [V ]) moveBack (ele * list.Element ) {
405
407
c .cachedMtx .Lock ()
406
408
defer c .cachedMtx .Unlock ()
407
- c .cached .MoveToBack (ele )
409
+ c .lruList .MoveToBack (ele )
408
410
}
409
411
410
412
func (c * lruCache [V ]) updateSize (oldSize , newSizeBytes int64 ) {
@@ -425,6 +427,8 @@ type cacheEntryPromise[V any] struct {
425
427
v V
426
428
err error
427
429
430
+ // reference for the element in the LRU list
431
+ // This is used to push this cache entry to the back of the list as result as a cache hit
428
432
lElement * list.Element
429
433
}
430
434
0 commit comments