Skip to content

Commit 602f72e

Browse files
committed
rename lru list
Signed-off-by: alanprot <[email protected]>
1 parent e493ae8 commit 602f72e

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

pkg/storage/tsdb/expanded_postings_cache.go

+12-8
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

pkg/storage/tsdb/expanded_postings_cache_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ func TestLru(t *testing.T) {
7777
key := RepeatStringIfNeeded(fmt.Sprintf("key%d", i), keySize)
7878
_, hit := cache.getPromiseForKey(key, func() (int, int64, error) { return 1, 1, nil })
7979
require.False(t, hit)
80-
require.Equal(t, key, cache.cached.Back().Value)
80+
require.Equal(t, key, cache.lruList.Back().Value)
8181
assertCacheItemsCount(t, cache, i+1)
8282
}
8383

8484
for i := 0; i < maxNumberOfCachedItems; i++ {
8585
key := RepeatStringIfNeeded(fmt.Sprintf("key%d", i), keySize)
8686
_, hit := cache.getPromiseForKey(key, func() (int, int64, error) { return 1, 1, nil })
8787
require.True(t, hit)
88-
require.Equal(t, key, cache.cached.Back().Value)
88+
require.Equal(t, key, cache.lruList.Back().Value)
8989
assertCacheItemsCount(t, cache, maxNumberOfCachedItems)
9090
}
9191

@@ -104,7 +104,7 @@ func TestLru(t *testing.T) {
104104
key := RepeatStringIfNeeded(fmt.Sprintf("key_new%d", i), keySize)
105105
_, hit := cache.getPromiseForKey(key, func() (int, int64, error) { return 1, 1, nil })
106106
require.False(t, hit)
107-
require.Equal(t, maxNumberOfCachedItems, cache.cached.Len())
107+
require.Equal(t, maxNumberOfCachedItems, cache.lruList.Len())
108108
}
109109

110110
for i := 0; i < maxNumberOfCachedItems; i++ {
@@ -244,7 +244,7 @@ func RepeatStringIfNeeded(seed string, length int) string {
244244
}
245245

246246
func assertCacheItemsCount[T any](t *testing.T, cache *lruCache[T], size int) {
247-
require.Equal(t, size, cache.cached.Len())
247+
require.Equal(t, size, cache.lruList.Len())
248248
count := 0
249249
cache.cachedValues.Range(func(k, v any) bool {
250250
count++

0 commit comments

Comments
 (0)