refactor(all): changing the value passed and stored as a byte slice

This commit is contained in:
censhin 2021-04-30 11:23:41 -04:00
parent a88b9ce0a6
commit 3e480ca15c
3 changed files with 7 additions and 14 deletions

View File

@ -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{},
}

View File

@ -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:"-"`
}

View File

@ -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)))
}
}