Skip to content

Commit

Permalink
rename lru list
Browse files Browse the repository at this point in the history
Signed-off-by: alanprot <[email protected]>
  • Loading branch information
alanprot committed Nov 21, 2024
1 parent e493ae8 commit 48849fc
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions pkg/storage/tsdb/expanded_postings_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,17 @@ type lruCache[V any] struct {
metrics ExpandedPostingsCacheMetrics

// Fields from here should be locked
cachedMtx sync.RWMutex
cached *list.List
cachedMtx sync.RWMutex
// Keeps tracks of the last recent used keys.
// The most recent key used is placed in the back of the list while items should be evicted from the front of the list
lruList *list.List
cachedBytes int64
}

func newLruCache[V any](cfg PostingsCacheConfig, name string, metrics *ExpandedPostingsCacheMetrics, timeNow func() time.Time) *lruCache[V] {
return &lruCache[V]{
cachedValues: new(sync.Map),
cached: list.New(),
lruList: list.New(),
cfg: cfg,
timeNow: timeNow,
name: name,
Expand Down Expand Up @@ -361,7 +363,7 @@ func (c *lruCache[V]) contains(k string) bool {
}

func (c *lruCache[V]) shouldEvictHead() (string, bool) {
h := c.cached.Front()
h := c.lruList.Front()
if h == nil {
return "", false
}
Expand All @@ -382,8 +384,8 @@ func (c *lruCache[V]) shouldEvictHead() (string, bool) {
}

func (c *lruCache[V]) evictHead() {
front := c.cached.Front()
c.cached.Remove(front)
front := c.lruList.Front()
c.lruList.Remove(front)
oldestKey := front.Value.(string)
if oldest, loaded := c.cachedValues.LoadAndDelete(oldestKey); loaded {
c.cachedBytes -= oldest.(*cacheEntryPromise[V]).sizeBytes
Expand All @@ -398,13 +400,13 @@ func (c *lruCache[V]) created(key string, sizeBytes int64) *list.Element {
c.cachedMtx.Lock()
defer c.cachedMtx.Unlock()
c.cachedBytes += sizeBytes
return c.cached.PushBack(key)
return c.lruList.PushBack(key)
}

func (c *lruCache[V]) moveBack(ele *list.Element) {
c.cachedMtx.Lock()
defer c.cachedMtx.Unlock()
c.cached.MoveToBack(ele)
c.lruList.MoveToBack(ele)
}

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

// reference for the element in the LRU list
// This is used to push this cache entry to the back of the list as result as a cache hit
lElement *list.Element
}

Expand Down

0 comments on commit 48849fc

Please sign in to comment.