Skip to content
This repository has been archived by the owner on Sep 22, 2020. It is now read-only.

cache: update LRU cache when putting same key but different value #424

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions distributor/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package distributor

import (
"container/list"
"reflect"
"sync"
)

Expand Down Expand Up @@ -32,8 +33,19 @@ func (lru *cache) Put(key string, value interface{}) {
}
lru.mut.Lock()
defer lru.mut.Unlock()
if _, ok := lru.get(key); ok {
return
if v, ok := lru.get(key); ok {
if reflect.DeepEqual(v, value) {
return
} else {
// Actually find() is not necessary, but it makes sure to remove
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if we want to be extra safe here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Umm... @lpabon how do you think?

// correct element and the find loop would be finished soon, since
// the element is in the front now.
if old := lru.find(lru.priority.Front(), v); old != nil {
lru.priority.Remove(old)
} else {
clog.Errorf("read cache is corrupted. Please restart the process.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clog.Fatalf

}
}
}
if len(lru.cache) == lru.maxSize {
lru.removeOldest()
Expand Down Expand Up @@ -63,3 +75,13 @@ func (lru *cache) removeOldest() {
last := lru.priority.Remove(lru.priority.Back())
delete(lru.cache, last.(kv).key)
}

func (lru *cache) find(e *list.Element, v interface{}) *list.Element {
if e == nil {
return nil
} else if e.Value.(kv).value == v {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change else if to if. we already return at line 81.

return e
} else {
return lru.find(e.Next(), v)
}
}