diff --git a/gee-cache/day1-lru/geecache/lru/lru.go b/gee-cache/day1-lru/geecache/lru/lru.go index 81eee43..6fa617a 100644 --- a/gee-cache/day1-lru/geecache/lru/lru.go +++ b/gee-cache/day1-lru/geecache/lru/lru.go @@ -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 } diff --git a/gee-cache/day1-lru/geecache/lru/lru_test.go b/gee-cache/day1-lru/geecache/lru/lru_test.go index 7308322..f2d3470 100644 --- a/gee-cache/day1-lru/geecache/lru/lru_test.go +++ b/gee-cache/day1-lru/geecache/lru/lru_test.go @@ -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) + } +} diff --git a/gee-cache/day2-single-node/geecache/lru/lru.go b/gee-cache/day2-single-node/geecache/lru/lru.go index 81eee43..6fa617a 100644 --- a/gee-cache/day2-single-node/geecache/lru/lru.go +++ b/gee-cache/day2-single-node/geecache/lru/lru.go @@ -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 } diff --git a/gee-cache/day2-single-node/geecache/lru/lru_test.go b/gee-cache/day2-single-node/geecache/lru/lru_test.go index 7308322..f2d3470 100644 --- a/gee-cache/day2-single-node/geecache/lru/lru_test.go +++ b/gee-cache/day2-single-node/geecache/lru/lru_test.go @@ -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) + } +} diff --git a/gee-cache/day3-http-server/geecache/lru/lru.go b/gee-cache/day3-http-server/geecache/lru/lru.go index 81eee43..6fa617a 100644 --- a/gee-cache/day3-http-server/geecache/lru/lru.go +++ b/gee-cache/day3-http-server/geecache/lru/lru.go @@ -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 } diff --git a/gee-cache/day3-http-server/geecache/lru/lru_test.go b/gee-cache/day3-http-server/geecache/lru/lru_test.go index 7308322..f2d3470 100644 --- a/gee-cache/day3-http-server/geecache/lru/lru_test.go +++ b/gee-cache/day3-http-server/geecache/lru/lru_test.go @@ -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) + } +} diff --git a/gee-cache/day4-consistent-hash/geecache/lru/lru.go b/gee-cache/day4-consistent-hash/geecache/lru/lru.go index 81eee43..6fa617a 100644 --- a/gee-cache/day4-consistent-hash/geecache/lru/lru.go +++ b/gee-cache/day4-consistent-hash/geecache/lru/lru.go @@ -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 } diff --git a/gee-cache/day4-consistent-hash/geecache/lru/lru_test.go b/gee-cache/day4-consistent-hash/geecache/lru/lru_test.go index 7308322..f2d3470 100644 --- a/gee-cache/day4-consistent-hash/geecache/lru/lru_test.go +++ b/gee-cache/day4-consistent-hash/geecache/lru/lru_test.go @@ -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) + } +} diff --git a/gee-cache/day5-multi-nodes/geecache/lru/lru.go b/gee-cache/day5-multi-nodes/geecache/lru/lru.go index 81eee43..6fa617a 100644 --- a/gee-cache/day5-multi-nodes/geecache/lru/lru.go +++ b/gee-cache/day5-multi-nodes/geecache/lru/lru.go @@ -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 } diff --git a/gee-cache/day5-multi-nodes/geecache/lru/lru_test.go b/gee-cache/day5-multi-nodes/geecache/lru/lru_test.go index 7308322..f2d3470 100644 --- a/gee-cache/day5-multi-nodes/geecache/lru/lru_test.go +++ b/gee-cache/day5-multi-nodes/geecache/lru/lru_test.go @@ -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) + } +} diff --git a/gee-cache/day6-single-flight/geecache/lru/lru.go b/gee-cache/day6-single-flight/geecache/lru/lru.go index 81eee43..6fa617a 100644 --- a/gee-cache/day6-single-flight/geecache/lru/lru.go +++ b/gee-cache/day6-single-flight/geecache/lru/lru.go @@ -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 } diff --git a/gee-cache/day6-single-flight/geecache/lru/lru_test.go b/gee-cache/day6-single-flight/geecache/lru/lru_test.go index 7308322..f2d3470 100644 --- a/gee-cache/day6-single-flight/geecache/lru/lru_test.go +++ b/gee-cache/day6-single-flight/geecache/lru/lru_test.go @@ -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) + } +} diff --git a/gee-cache/day7-proto-buf/geecache/lru/lru.go b/gee-cache/day7-proto-buf/geecache/lru/lru.go index 81eee43..6fa617a 100644 --- a/gee-cache/day7-proto-buf/geecache/lru/lru.go +++ b/gee-cache/day7-proto-buf/geecache/lru/lru.go @@ -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 } diff --git a/gee-cache/day7-proto-buf/geecache/lru/lru_test.go b/gee-cache/day7-proto-buf/geecache/lru/lru_test.go index 7308322..f2d3470 100644 --- a/gee-cache/day7-proto-buf/geecache/lru/lru_test.go +++ b/gee-cache/day7-proto-buf/geecache/lru/lru_test.go @@ -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) + } +} diff --git a/gee-cache/doc/geecache-day1.md b/gee-cache/doc/geecache-day1.md index 6848421..064d90a 100644 --- a/gee-cache/doc/geecache-day1.md +++ b/gee-cache/doc/geecache-day1.md @@ -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 }