From 3e480ca15cb22638596c565a4ffd3a166022a90d Mon Sep 17 00:00:00 2001 From: censhin Date: Fri, 30 Apr 2021 11:23:41 -0400 Subject: [PATCH] refactor(all): changing the value passed and stored as a byte slice --- groupcache.go | 14 ++++---------- groupcachepb/groupcache.pb.go | 2 +- http_test.go | 5 ++--- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/groupcache.go b/groupcache.go index 162ccfa..88c4b02 100644 --- a/groupcache.go +++ b/groupcache.go @@ -26,7 +26,6 @@ package groupcache import ( "context" - "encoding/json" "errors" "strconv" "sync" @@ -259,7 +258,7 @@ func (g *Group) Get(ctx context.Context, key string, dest Sink) error { return setSinkView(dest, value) } -func (g *Group) Set(ctx context.Context, key string, value interface{}) error { +func (g *Group) Set(ctx context.Context, key string, value []byte) error { g.peersOnce.Do(g.initPeers) _, err := g.setGroup.Do(key, func() (interface{}, error) { @@ -478,7 +477,7 @@ func (g *Group) getFromPeer(ctx context.Context, peer ProtoGetter, key string) ( return value, nil } -func (g *Group) setFromPeer(ctx context.Context, peer ProtoGetter, key string, value interface{}) error { +func (g *Group) setFromPeer(ctx context.Context, peer ProtoGetter, key string, value []byte) error { req := &pb.GetRequest{ Group: &g.name, Key: &key, @@ -507,18 +506,13 @@ func (g *Group) lookupCache(key string) (value ByteView, ok bool) { return } -func (g *Group) localSet(key string, value interface{}) { +func (g *Group) localSet(key string, value []byte) { if g.cacheBytes <= 0 { return } - buf, err := json.Marshal(value) - if err != nil { - return - } - bv := ByteView{ - b: buf, + b: value, e: time.Time{}, } diff --git a/groupcachepb/groupcache.pb.go b/groupcachepb/groupcache.pb.go index f6957e0..f6c1313 100644 --- a/groupcachepb/groupcache.pb.go +++ b/groupcachepb/groupcache.pb.go @@ -31,7 +31,7 @@ const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package type GetRequest struct { Group *string `protobuf:"bytes,1,req,name=group" json:"group,omitempty"` Key *string `protobuf:"bytes,2,req,name=key" json:"key,omitempty"` - Value interface{} + Value []byte XXX_unrecognized []byte `json:"-"` } diff --git a/http_test.go b/http_test.go index 785b38f..55e6aef 100644 --- a/http_test.go +++ b/http_test.go @@ -156,7 +156,7 @@ func TestHTTPPool(t *testing.T) { setValue := "test set" var getValue string // Add the key to the cache - if err := g.Set(ctx, key, setValue); err != nil { + if err := g.Set(ctx, key, []byte(setValue)); err != nil { t.Fatal(err) } @@ -165,8 +165,7 @@ func TestHTTPPool(t *testing.T) { t.Fatal(err) } - // TODO: why is this like this? - if fmt.Sprintf("\"%s\"", setValue) != getValue { + if setValue != getValue { t.Fatal(errors.New(fmt.Sprintf("incorrect value retrieved after set: %s", getValue))) } }