Skip to content

Commit 48849fc

Browse files
committed
rename lru list
Signed-off-by: alanprot <alanprot@gmail.com>
1 parent e493ae8 commit 48849fc

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

pkg/storage/tsdb/expanded_postings_cache.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,17 @@ type lruCache[V any] struct {
280280
metrics ExpandedPostingsCacheMetrics
281281

282282
// 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
285287
cachedBytes int64
286288
}
287289

288290
func newLruCache[V any](cfg PostingsCacheConfig, name string, metrics *ExpandedPostingsCacheMetrics, timeNow func() time.Time) *lruCache[V] {
289291
return &lruCache[V]{
290292
cachedValues: new(sync.Map),
291-
cached: list.New(),
293+
lruList: list.New(),
292294
cfg: cfg,
293295
timeNow: timeNow,
294296
name: name,
@@ -361,7 +363,7 @@ func (c *lruCache[V]) contains(k string) bool {
361363
}
362364

363365
func (c *lruCache[V]) shouldEvictHead() (string, bool) {
364-
h := c.cached.Front()
366+
h := c.lruList.Front()
365367
if h == nil {
366368
return "", false
367369
}
@@ -382,8 +384,8 @@ func (c *lruCache[V]) shouldEvictHead() (string, bool) {
382384
}
383385

384386
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)
387389
oldestKey := front.Value.(string)
388390
if oldest, loaded := c.cachedValues.LoadAndDelete(oldestKey); loaded {
389391
c.cachedBytes -= oldest.(*cacheEntryPromise[V]).sizeBytes
@@ -398,13 +400,13 @@ func (c *lruCache[V]) created(key string, sizeBytes int64) *list.Element {
398400
c.cachedMtx.Lock()
399401
defer c.cachedMtx.Unlock()
400402
c.cachedBytes += sizeBytes
401-
return c.cached.PushBack(key)
403+
return c.lruList.PushBack(key)
402404
}
403405

404406
func (c *lruCache[V]) moveBack(ele *list.Element) {
405407
c.cachedMtx.Lock()
406408
defer c.cachedMtx.Unlock()
407-
c.cached.MoveToBack(ele)
409+
c.lruList.MoveToBack(ele)
408410
}
409411

410412
func (c *lruCache[V]) updateSize(oldSize, newSizeBytes int64) {
@@ -425,6 +427,8 @@ type cacheEntryPromise[V any] struct {
425427
v V
426428
err error
427429

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
428432
lElement *list.Element
429433
}
430434

0 commit comments

Comments
 (0)