From cd4ebd67a5c1bbcfe904d92b8b41a42a5f5ea202 Mon Sep 17 00:00:00 2001 From: Ben Prew Date: Sat, 29 Aug 2020 14:30:25 -0700 Subject: [PATCH] fix golint errors and requested changes in PR --- 2-race-in-cache/check_test.go | 8 ++++---- 2-race-in-cache/main.go | 31 +++++++++++++++---------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/2-race-in-cache/check_test.go b/2-race-in-cache/check_test.go index 415ade8..cba3aee 100644 --- a/2-race-in-cache/check_test.go +++ b/2-race-in-cache/check_test.go @@ -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) } diff --git a/2-race-in-cache/main.go b/2-race-in-cache/main.go index 57d3b2e..b28abd7 100644 --- a/2-race-in-cache/main.go +++ b/2-race-in-cache/main.go @@ -19,7 +19,7 @@ type KeyStoreCacheLoader interface { Load(string) string } -type Page struct { +type page struct { Key string Value string } @@ -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