Skip to content
This repository was archived by the owner on Sep 22, 2020. It is now read-only.
Open
Changes from all 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.Fatalf("read cache is corrupted. Please restart the process.")
}
}
}
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
}
if reflect.DeepEqual(e.Value.(kv).value, v) {
return e
}
return lru.find(e.Next(), v)
}