From ca06d065c5ef4588a7405579c4c6210aa50af013 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Duffeck?= Date: Wed, 8 Mar 2023 09:42:10 +0100 Subject: [PATCH] Use msgpack to serialize cache data instead of json --- go.mod | 2 ++ go.sum | 4 ++++ pkg/storage/cache/cache.go | 12 +++++++++--- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 8d2d483baf..87f7aa7edc 100644 --- a/go.mod +++ b/go.mod @@ -190,6 +190,8 @@ require ( github.com/stretchr/objx v0.5.0 // indirect github.com/tidwall/pretty v1.2.1 // indirect github.com/urfave/cli/v2 v2.20.3 // indirect + github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect + github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect github.com/xanzy/ssh-agent v0.3.2 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect go.etcd.io/etcd/api/v3 v3.5.5 // indirect diff --git a/go.sum b/go.sum index 28c4ff19be..17a76c4773 100644 --- a/go.sum +++ b/go.sum @@ -904,6 +904,10 @@ github.com/urfave/cli/v2 v2.20.3 h1:lOgGidH/N5loaigd9HjFsOIhXSTrzl7tBpHswZ428w4= github.com/urfave/cli/v2 v2.20.3/go.mod h1:1CNUng3PtjQMtRzJO4FMXBQvkGtuYRxxiR9xMa7jMwI= github.com/vimeo/go-util v1.2.0/go.mod h1:s13SMDTSO7AjH1nbgp707mfN5JFIWUFDU5MDDuRRtKs= github.com/vimeo/go-util v1.4.1/go.mod h1:r+yspV//C48HeMXV8nEvtUeNiIiGfVv3bbEHzOgudwE= +github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU= +github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc= +github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= +github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= github.com/wk8/go-ordered-map v1.0.0 h1:BV7z+2PaK8LTSd/mWgY12HyMAo5CEgkHqbkVq2thqr8= github.com/wk8/go-ordered-map v1.0.0/go.mod h1:9ZIbRunKbuvfPKyBP1SIKLcXNlv74YCOZ3t3VTS6gRk= github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0= diff --git a/pkg/storage/cache/cache.go b/pkg/storage/cache/cache.go index e4fab668f4..ae96770138 100644 --- a/pkg/storage/cache/cache.go +++ b/pkg/storage/cache/cache.go @@ -19,7 +19,7 @@ package cache import ( - "encoding/json" + "bytes" "fmt" "strings" "sync" @@ -32,6 +32,7 @@ import ( redisopts "github.com/go-redis/redis/v8" "github.com/nats-io/nats.go" microetcd "github.com/owncloud/ocis/v2/ocis-pkg/store/etcd" + msgpack "github.com/vmihailenco/msgpack/v5" microstore "go-micro.dev/v4/store" ) @@ -238,12 +239,17 @@ func (cache cacheStore) PullFromCache(key string, dest interface{}) error { if len(r) == 0 { return fmt.Errorf("not found") } - return json.Unmarshal(r[0].Value, dest) + // we seem to need the TypedMap decoder: https://github.com/vmihailenco/msgpack/issues/327 + decoder := msgpack.NewDecoder(bytes.NewReader(r[0].Value)) + decoder.SetMapDecoder(func(dec *msgpack.Decoder) (interface{}, error) { + return dec.DecodeTypedMap() + }) + return decoder.Decode(&dest) } // PushToCache pushes a key and value to the configured database and table of the underlying store func (cache cacheStore) PushToCache(key string, src interface{}) error { - b, err := json.Marshal(src) + b, err := msgpack.Marshal(src) if err != nil { return err }