Skip to content

Commit

Permalink
fix golint errors and requested changes in PR
Browse files Browse the repository at this point in the history
  • Loading branch information
benprew committed Aug 29, 2020
1 parent 9231805 commit cd4ebd6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
8 changes: 4 additions & 4 deletions 2-race-in-cache/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ func TestLRU(t *testing.T) {
cache := New(&loader)

for i := 0; i < 100; i++ {
cache.Get("Test " + strconv.Itoa(i))
cache.Get("Test" + strconv.Itoa(i))
}

if len(cache.cache) != 100 {
t.Errorf("cache not 100: %d", len(cache.cache))
}
cache.Get("Test 0")
cache.Get("Test 101")
if _, ok := cache.cache["Test 0"]; !ok {
cache.Get("Test0")
cache.Get("Test101")
if _, ok := cache.cache["Test0"]; !ok {
t.Errorf("0 evicted incorrectly: %v", cache.cache)
}

Expand Down
31 changes: 15 additions & 16 deletions 2-race-in-cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type KeyStoreCacheLoader interface {
Load(string) string
}

type Page struct {
type page struct {
Key string
Value string
}
Expand All @@ -43,22 +43,21 @@ func New(load KeyStoreCacheLoader) *KeyStoreCache {
func (k *KeyStoreCache) Get(key string) string {
if e, ok := k.cache[key]; ok {
k.pages.MoveToFront(e)
return e.Value.(Page).Value
} else {
// Miss - load from database and save it in cache
page := Page{key, k.load(key)}
// if cache is full remove the least used item
if len(k.cache) >= CacheSize {
end := k.pages.Back()
// remove from map
delete(k.cache, end.Value.(Page).Key)
// remove from list
k.pages.Remove(end)
}
k.pages.PushFront(page)
k.cache[key] = k.pages.Front()
return page.Value
return e.Value.(page).Value
}
// Miss - load from database and save it in cache
p := page{key, k.load(key)}
// if cache is full remove the least used item
if len(k.cache) >= CacheSize {
end := k.pages.Back()
// remove from map
delete(k.cache, end.Value.(page).Key)
// remove from list
k.pages.Remove(end)
}
k.pages.PushFront(p)
k.cache[key] = k.pages.Front()
return p.Value
}

// Loader implements KeyStoreLoader
Expand Down

0 comments on commit cd4ebd6

Please sign in to comment.