Skip to content

Commit

Permalink
1. move cloneBytes from geecache.go to byteview.go 2. add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
geektutu committed Feb 12, 2020
1 parent a6535ef commit 0d40f4f
Show file tree
Hide file tree
Showing 34 changed files with 860 additions and 62 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
[GeeCache](https://geektutu.com/post/geecache.html) 是一个模仿 [groupcache](https://github.com/golang/groupcache) 实现的分布式缓存系统

- [第一天:LRU 缓存淘汰策略](https://geektutu.com/post/geecache-day1.html) | [Code](gee-cache/day1-lru)
- 第二天:单机并发缓存 | [Code](gee-cache/day2-single-node)
- 第三天:HTTP 服务端 | [Code](gee-cache/day3-http-server)
- [第二天:单机并发缓存](https://geektutu.com/post/geecache-day2.html) | [Code](gee-cache/day2-single-node)
- [第三天:HTTP 服务端](https://geektutu.com/post/geecache-day3.html) | [Code](gee-cache/day3-http-server)
- 第四天:一致性哈希(Hash) | [Code](gee-cache/day4-consistent-hash)
- 第五天:分布式节点 | [Code](gee-cache/day5-multi-nodes)
- 第六天:防止缓存击穿 | [Code](gee-cache/day6-single-flight)
Expand Down
6 changes: 6 additions & 0 deletions gee-cache/day2-single-node/geecache/byteview.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ func (v ByteView) ByteSlice() []byte {
func (v ByteView) String() string {
return string(v.b)
}

func cloneBytes(b []byte) []byte {
c := make([]byte, len(b))
copy(c, b)
return c
}
6 changes: 0 additions & 6 deletions gee-cache/day2-single-node/geecache/geecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ func (g *Group) Get(key string) (ByteView, error) {
return g.load(key)
}

func cloneBytes(b []byte) []byte {
c := make([]byte, len(b))
copy(c, b)
return c
}

func (g *Group) load(key string) (value ByteView, err error) {
return g.getLocally(key)
}
Expand Down
23 changes: 21 additions & 2 deletions gee-cache/day2-single-node/geecache/geecache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package geecache
import (
"fmt"
"log"
"reflect"
"testing"
)

Expand All @@ -12,21 +13,39 @@ var db = map[string]string{
"Sam": "567",
}

func TestGetter(t *testing.T) {
var f Getter = GetterFunc(func(key string) ([]byte, error) {
return []byte(key), nil
})

expect := []byte("key")
if v, _ := f.Get("key"); !reflect.DeepEqual(v, expect) {
t.Fatal("callback failed")
}
}

func TestGet(t *testing.T) {
loadCounts := make(map[string]int, len(db))
gee := NewGroup("scores", 2<<10, GetterFunc(
func(key string) ([]byte, error) {
log.Println("[SlowDB] search key", key)
if v, ok := db[key]; ok {
if _, ok := loadCounts[key]; !ok {
loadCounts[key] = 0
}
loadCounts[key]++
return []byte(v), nil
}
return nil, fmt.Errorf("%s not exist", key)
}))

for k, v := range db {
view, err := gee.Get(k)
if err != nil || view.String() != v {
if view, err := gee.Get(k); err != nil || view.String() != v {
t.Fatal("failed to get value of Tom")
}
if _, err := gee.Get(k); err != nil || loadCounts[k] > 1 {
t.Fatalf("cache %s miss", k)
}
}

if view, err := gee.Get("unknown"); err == nil {
Expand Down
6 changes: 6 additions & 0 deletions gee-cache/day3-http-server/geecache/byteview.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ func (v ByteView) ByteSlice() []byte {
func (v ByteView) String() string {
return string(v.b)
}

func cloneBytes(b []byte) []byte {
c := make([]byte, len(b))
copy(c, b)
return c
}
6 changes: 0 additions & 6 deletions gee-cache/day3-http-server/geecache/geecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ func (g *Group) Get(key string) (ByteView, error) {
return g.load(key)
}

func cloneBytes(b []byte) []byte {
c := make([]byte, len(b))
copy(c, b)
return c
}

func (g *Group) load(key string) (value ByteView, err error) {
return g.getLocally(key)
}
Expand Down
23 changes: 21 additions & 2 deletions gee-cache/day3-http-server/geecache/geecache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package geecache
import (
"fmt"
"log"
"reflect"
"testing"
)

Expand All @@ -12,21 +13,39 @@ var db = map[string]string{
"Sam": "567",
}

func TestGetter(t *testing.T) {
var f Getter = GetterFunc(func(key string) ([]byte, error) {
return []byte(key), nil
})

expect := []byte("key")
if v, _ := f.Get("key"); !reflect.DeepEqual(v, expect) {
t.Fatal("callback failed")
}
}

func TestGet(t *testing.T) {
loadCounts := make(map[string]int, len(db))
gee := NewGroup("scores", 2<<10, GetterFunc(
func(key string) ([]byte, error) {
log.Println("[SlowDB] search key", key)
if v, ok := db[key]; ok {
if _, ok := loadCounts[key]; !ok {
loadCounts[key] = 0
}
loadCounts[key]++
return []byte(v), nil
}
return nil, fmt.Errorf("%s not exist", key)
}))

for k, v := range db {
view, err := gee.Get(k)
if err != nil || view.String() != v {
if view, err := gee.Get(k); err != nil || view.String() != v {
t.Fatal("failed to get value of Tom")
}
if _, err := gee.Get(k); err != nil || loadCounts[k] > 1 {
t.Fatalf("cache %s miss", k)
}
}

if view, err := gee.Get("unknown"); err == nil {
Expand Down
2 changes: 1 addition & 1 deletion gee-cache/day3-http-server/geecache/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type HTTPPool struct {
basePath string
}

// NewHTTPPool initializes an HTTP pool of peers, and registers itself as a PeerPicker.
// NewHTTPPool initializes an HTTP pool of peers.
func NewHTTPPool(self string) *HTTPPool {
return &HTTPPool{
self: self,
Expand Down
6 changes: 6 additions & 0 deletions gee-cache/day4-consistent-hash/geecache/byteview.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ func (v ByteView) ByteSlice() []byte {
func (v ByteView) String() string {
return string(v.b)
}

func cloneBytes(b []byte) []byte {
c := make([]byte, len(b))
copy(c, b)
return c
}
6 changes: 0 additions & 6 deletions gee-cache/day4-consistent-hash/geecache/geecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ func (g *Group) Get(key string) (ByteView, error) {
return g.load(key)
}

func cloneBytes(b []byte) []byte {
c := make([]byte, len(b))
copy(c, b)
return c
}

func (g *Group) load(key string) (value ByteView, err error) {
return g.getLocally(key)
}
Expand Down
23 changes: 21 additions & 2 deletions gee-cache/day4-consistent-hash/geecache/geecache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package geecache
import (
"fmt"
"log"
"reflect"
"testing"
)

Expand All @@ -12,21 +13,39 @@ var db = map[string]string{
"Sam": "567",
}

func TestGetter(t *testing.T) {
var f Getter = GetterFunc(func(key string) ([]byte, error) {
return []byte(key), nil
})

expect := []byte("key")
if v, _ := f.Get("key"); !reflect.DeepEqual(v, expect) {
t.Fatal("callback failed")
}
}

func TestGet(t *testing.T) {
loadCounts := make(map[string]int, len(db))
gee := NewGroup("scores", 2<<10, GetterFunc(
func(key string) ([]byte, error) {
log.Println("[SlowDB] search key", key)
if v, ok := db[key]; ok {
if _, ok := loadCounts[key]; !ok {
loadCounts[key] = 0
}
loadCounts[key]++
return []byte(v), nil
}
return nil, fmt.Errorf("%s not exist", key)
}))

for k, v := range db {
view, err := gee.Get(k)
if err != nil || view.String() != v {
if view, err := gee.Get(k); err != nil || view.String() != v {
t.Fatal("failed to get value of Tom")
}
if _, err := gee.Get(k); err != nil || loadCounts[k] > 1 {
t.Fatalf("cache %s miss", k)
}
}

if view, err := gee.Get("unknown"); err == nil {
Expand Down
2 changes: 1 addition & 1 deletion gee-cache/day4-consistent-hash/geecache/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type HTTPPool struct {
basePath string
}

// NewHTTPPool initializes an HTTP pool of peers, and registers itself as a PeerPicker.
// NewHTTPPool initializes an HTTP pool of peers.
func NewHTTPPool(self string) *HTTPPool {
return &HTTPPool{
self: self,
Expand Down
6 changes: 6 additions & 0 deletions gee-cache/day5-multi-nodes/geecache/byteview.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ func (v ByteView) ByteSlice() []byte {
func (v ByteView) String() string {
return string(v.b)
}

func cloneBytes(b []byte) []byte {
c := make([]byte, len(b))
copy(c, b)
return c
}
6 changes: 0 additions & 6 deletions gee-cache/day5-multi-nodes/geecache/geecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@ func (g *Group) RegisterPeers(peers PeerPicker) {
g.peers = peers
}

func cloneBytes(b []byte) []byte {
c := make([]byte, len(b))
copy(c, b)
return c
}

func (g *Group) load(key string) (value ByteView, err error) {
if g.peers != nil {
if peer, ok := g.peers.PickPeer(key); ok {
Expand Down
23 changes: 21 additions & 2 deletions gee-cache/day5-multi-nodes/geecache/geecache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package geecache
import (
"fmt"
"log"
"reflect"
"testing"
)

Expand All @@ -12,21 +13,39 @@ var db = map[string]string{
"Sam": "567",
}

func TestGetter(t *testing.T) {
var f Getter = GetterFunc(func(key string) ([]byte, error) {
return []byte(key), nil
})

expect := []byte("key")
if v, _ := f.Get("key"); !reflect.DeepEqual(v, expect) {
t.Fatal("callback failed")
}
}

func TestGet(t *testing.T) {
loadCounts := make(map[string]int, len(db))
gee := NewGroup("scores", 2<<10, GetterFunc(
func(key string) ([]byte, error) {
log.Println("[SlowDB] search key", key)
if v, ok := db[key]; ok {
if _, ok := loadCounts[key]; !ok {
loadCounts[key] = 0
}
loadCounts[key]++
return []byte(v), nil
}
return nil, fmt.Errorf("%s not exist", key)
}))

for k, v := range db {
view, err := gee.Get(k)
if err != nil || view.String() != v {
if view, err := gee.Get(k); err != nil || view.String() != v {
t.Fatal("failed to get value of Tom")
}
if _, err := gee.Get(k); err != nil || loadCounts[k] > 1 {
t.Fatalf("cache %s miss", k)
}
}

if view, err := gee.Get("unknown"); err == nil {
Expand Down
2 changes: 1 addition & 1 deletion gee-cache/day5-multi-nodes/geecache/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type HTTPPool struct {
httpGetters map[string]*httpGetter // keyed by e.g. "http://10.0.0.2:8008"
}

// NewHTTPPool initializes an HTTP pool of peers, and registers itself as a PeerPicker.
// NewHTTPPool initializes an HTTP pool of peers.
func NewHTTPPool(self string) *HTTPPool {
return &HTTPPool{
self: self,
Expand Down
6 changes: 6 additions & 0 deletions gee-cache/day6-single-flight/geecache/byteview.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ func (v ByteView) ByteSlice() []byte {
func (v ByteView) String() string {
return string(v.b)
}

func cloneBytes(b []byte) []byte {
c := make([]byte, len(b))
copy(c, b)
return c
}
6 changes: 0 additions & 6 deletions gee-cache/day6-single-flight/geecache/geecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ func (g *Group) RegisterPeers(peers PeerPicker) {
g.peers = peers
}

func cloneBytes(b []byte) []byte {
c := make([]byte, len(b))
copy(c, b)
return c
}

func (g *Group) load(key string) (value ByteView, err error) {
// each key is only fetched once (either locally or remotely)
// regardless of the number of concurrent callers.
Expand Down
Loading

0 comments on commit 0d40f4f

Please sign in to comment.