Skip to content

Commit

Permalink
fix lru dumplicate key, nbytes update issue
Browse files Browse the repository at this point in the history
  • Loading branch information
geektutu committed Feb 15, 2020
1 parent 3833699 commit 3986794
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions gee-cache/day1-lru/geecache/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (c *Cache) Add(key string, value Value) {
if ele, ok := c.cache[key]; ok {
c.ll.MoveToFront(ele)
kv := ele.Value.(*entry)
c.nbytes += int64(value.Len()) - int64(kv.value.Len())
kv.value = value
return
}
Expand Down
10 changes: 10 additions & 0 deletions gee-cache/day1-lru/geecache/lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ func TestOnEvicted(t *testing.T) {
t.Fatalf("Call OnEvicted failed, expect keys equals to %s", expect)
}
}

func TestAdd(t *testing.T) {
lru := New(int64(0), nil)
lru.Add("key", String("1"))
lru.Add("key", String("111"))

if lru.nbytes != int64(len("key")+len("111")) {
t.Fatal("expected 6 but got", lru.nbytes)
}
}
1 change: 1 addition & 0 deletions gee-cache/day2-single-node/geecache/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (c *Cache) Add(key string, value Value) {
if ele, ok := c.cache[key]; ok {
c.ll.MoveToFront(ele)
kv := ele.Value.(*entry)
c.nbytes += int64(value.Len()) - int64(kv.value.Len())
kv.value = value
return
}
Expand Down
10 changes: 10 additions & 0 deletions gee-cache/day2-single-node/geecache/lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ func TestOnEvicted(t *testing.T) {
t.Fatalf("Call OnEvicted failed, expect keys equals to %s", expect)
}
}

func TestAdd(t *testing.T) {
lru := New(int64(0), nil)
lru.Add("key", String("1"))
lru.Add("key", String("111"))

if lru.nbytes != int64(len("key")+len("111")) {
t.Fatal("expected 6 but got", lru.nbytes)
}
}
1 change: 1 addition & 0 deletions gee-cache/day3-http-server/geecache/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (c *Cache) Add(key string, value Value) {
if ele, ok := c.cache[key]; ok {
c.ll.MoveToFront(ele)
kv := ele.Value.(*entry)
c.nbytes += int64(value.Len()) - int64(kv.value.Len())
kv.value = value
return
}
Expand Down
10 changes: 10 additions & 0 deletions gee-cache/day3-http-server/geecache/lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ func TestOnEvicted(t *testing.T) {
t.Fatalf("Call OnEvicted failed, expect keys equals to %s", expect)
}
}

func TestAdd(t *testing.T) {
lru := New(int64(0), nil)
lru.Add("key", String("1"))
lru.Add("key", String("111"))

if lru.nbytes != int64(len("key")+len("111")) {
t.Fatal("expected 6 but got", lru.nbytes)
}
}
1 change: 1 addition & 0 deletions gee-cache/day4-consistent-hash/geecache/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (c *Cache) Add(key string, value Value) {
if ele, ok := c.cache[key]; ok {
c.ll.MoveToFront(ele)
kv := ele.Value.(*entry)
c.nbytes += int64(value.Len()) - int64(kv.value.Len())
kv.value = value
return
}
Expand Down
10 changes: 10 additions & 0 deletions gee-cache/day4-consistent-hash/geecache/lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ func TestOnEvicted(t *testing.T) {
t.Fatalf("Call OnEvicted failed, expect keys equals to %s", expect)
}
}

func TestAdd(t *testing.T) {
lru := New(int64(0), nil)
lru.Add("key", String("1"))
lru.Add("key", String("111"))

if lru.nbytes != int64(len("key")+len("111")) {
t.Fatal("expected 6 but got", lru.nbytes)
}
}
1 change: 1 addition & 0 deletions gee-cache/day5-multi-nodes/geecache/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (c *Cache) Add(key string, value Value) {
if ele, ok := c.cache[key]; ok {
c.ll.MoveToFront(ele)
kv := ele.Value.(*entry)
c.nbytes += int64(value.Len()) - int64(kv.value.Len())
kv.value = value
return
}
Expand Down
10 changes: 10 additions & 0 deletions gee-cache/day5-multi-nodes/geecache/lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ func TestOnEvicted(t *testing.T) {
t.Fatalf("Call OnEvicted failed, expect keys equals to %s", expect)
}
}

func TestAdd(t *testing.T) {
lru := New(int64(0), nil)
lru.Add("key", String("1"))
lru.Add("key", String("111"))

if lru.nbytes != int64(len("key")+len("111")) {
t.Fatal("expected 6 but got", lru.nbytes)
}
}
1 change: 1 addition & 0 deletions gee-cache/day6-single-flight/geecache/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (c *Cache) Add(key string, value Value) {
if ele, ok := c.cache[key]; ok {
c.ll.MoveToFront(ele)
kv := ele.Value.(*entry)
c.nbytes += int64(value.Len()) - int64(kv.value.Len())
kv.value = value
return
}
Expand Down
10 changes: 10 additions & 0 deletions gee-cache/day6-single-flight/geecache/lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ func TestOnEvicted(t *testing.T) {
t.Fatalf("Call OnEvicted failed, expect keys equals to %s", expect)
}
}

func TestAdd(t *testing.T) {
lru := New(int64(0), nil)
lru.Add("key", String("1"))
lru.Add("key", String("111"))

if lru.nbytes != int64(len("key")+len("111")) {
t.Fatal("expected 6 but got", lru.nbytes)
}
}
1 change: 1 addition & 0 deletions gee-cache/day7-proto-buf/geecache/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (c *Cache) Add(key string, value Value) {
if ele, ok := c.cache[key]; ok {
c.ll.MoveToFront(ele)
kv := ele.Value.(*entry)
c.nbytes += int64(value.Len()) - int64(kv.value.Len())
kv.value = value
return
}
Expand Down
10 changes: 10 additions & 0 deletions gee-cache/day7-proto-buf/geecache/lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ func TestOnEvicted(t *testing.T) {
t.Fatalf("Call OnEvicted failed, expect keys equals to %s", expect)
}
}

func TestAdd(t *testing.T) {
lru := New(int64(0), nil)
lru.Add("key", String("1"))
lru.Add("key", String("111"))

if lru.nbytes != int64(len("key")+len("111")) {
t.Fatal("expected 6 but got", lru.nbytes)
}
}
1 change: 1 addition & 0 deletions gee-cache/doc/geecache-day1.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func (c *Cache) Add(key string, value Value) {
if ele, ok := c.cache[key]; ok {
c.ll.MoveToFront(ele)
kv := ele.Value.(*entry)
c.nbytes += int64(value.Len()) - int64(kv.value.Len())
kv.value = value
return
}
Expand Down

0 comments on commit 3986794

Please sign in to comment.