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 ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"strconv" "strconv"
"sync" "sync"
@ -259,7 +258,7 @@ func (g *Group) Get(ctx context.Context, key string, dest Sink) error {
return setSinkView(dest, value) 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) g.peersOnce.Do(g.initPeers)
_, err := g.setGroup.Do(key, func() (interface{}, error) { _, 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 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{ req := &pb.GetRequest{
Group: &g.name, Group: &g.name,
Key: &key, Key: &key,
@ -507,18 +506,13 @@ func (g *Group) lookupCache(key string) (value ByteView, ok bool) {
return return
} }
func (g *Group) localSet(key string, value interface{}) { func (g *Group) localSet(key string, value []byte) {
if g.cacheBytes <= 0 { if g.cacheBytes <= 0 {
return return
} }
buf, err := json.Marshal(value)
if err != nil {
return
}
bv := ByteView{ bv := ByteView{
b: buf, b: value,
e: time.Time{}, e: time.Time{},
} }

View File

@ -31,7 +31,7 @@ const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type GetRequest struct { type GetRequest struct {
Group *string `protobuf:"bytes,1,req,name=group" json:"group,omitempty"` Group *string `protobuf:"bytes,1,req,name=group" json:"group,omitempty"`
Key *string `protobuf:"bytes,2,req,name=key" json:"key,omitempty"` Key *string `protobuf:"bytes,2,req,name=key" json:"key,omitempty"`
Value interface{} Value []byte
XXX_unrecognized []byte `json:"-"` XXX_unrecognized []byte `json:"-"`
} }

View File

@ -156,7 +156,7 @@ func TestHTTPPool(t *testing.T) {
setValue := "test set" setValue := "test set"
var getValue string var getValue string
// Add the key to the cache // 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) t.Fatal(err)
} }
@ -165,8 +165,7 @@ func TestHTTPPool(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
// TODO: why is this like this? if setValue != getValue {
if fmt.Sprintf("\"%s\"", setValue) != getValue {
t.Fatal(errors.New(fmt.Sprintf("incorrect value retrieved after set: %s", getValue))) t.Fatal(errors.New(fmt.Sprintf("incorrect value retrieved after set: %s", getValue)))
} }
} }