Skip to content

Commit

Permalink
fix cache int64 problem
Browse files Browse the repository at this point in the history
  • Loading branch information
wubin1989 committed Aug 22, 2024
1 parent 7524ff8 commit 2259bd5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
20 changes: 15 additions & 5 deletions framework/database/marshaler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package database

import (
"bytes"
"context"
"fmt"
"github.com/bytedance/sonic/decoder"
"github.com/pkg/errors"
"reflect"

"github.com/bytedance/sonic"
Expand All @@ -29,23 +32,30 @@ func NewMarshaler(cache cache.CacheInterface[any]) *Marshaler {
}
}

type idecoder interface {
UseInt64()
Decode(val interface{}) (err error)
}

// Get obtains a value from cache and unmarshal value with given object
func (c *Marshaler) Get(ctx context.Context, key any, returnObj any) (any, error) {
key = c.shortenKey(key)
result, err := c.cache.Get(ctx, key)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

var dec idecoder
switch v := result.(type) {
case []byte:
err = json.Unmarshal(v, returnObj)
dec = decoder.NewStreamDecoder(bytes.NewBuffer(v))
case string:
err = json.Unmarshal([]byte(v), returnObj)
dec = decoder.NewDecoder(v)
}

if err != nil {
return nil, err
dec.UseInt64()
if err = dec.Decode(returnObj); err != nil {
return nil, errors.WithStack(err)
}

return returnObj, nil
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ require (
github.com/coocood/freecache v1.2.4
github.com/deckarep/golang-set/v2 v2.6.0
github.com/dgraph-io/ristretto v0.1.1
github.com/duke-git/lancet/v2 v2.3.2
github.com/elliotchance/orderedmap/v2 v2.2.0
github.com/go-playground/assert/v2 v2.2.0
github.com/go-redis/redis/v8 v8.11.5
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,6 @@ github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 h1:iFaUwBSo5Svw6L7HYpRu/0lE3e0BaElwnNO1qkNQxBY=
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5/go.mod h1:qssHWj60/X5sZFNxpG4HBPDHVqxNm4DfnCKgrbZOT+s=
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
github.com/duke-git/lancet/v2 v2.3.2 h1:Cv+uNkx5yGqDSvGc5Vu9eiiZobsPIf0Ng7NGy5hEdow=
github.com/duke-git/lancet/v2 v2.3.2/go.mod h1:zGa2R4xswg6EG9I6WnyubDbFO/+A/RROxIbXcwryTsc=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
Expand Down
14 changes: 10 additions & 4 deletions toolkit/copier/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package copier

import (
"github.com/bytedance/sonic"
"github.com/bytedance/sonic/decoder"
"github.com/pkg/errors"
"reflect"
)
Expand All @@ -13,12 +14,17 @@ func DeepCopy(src, target interface{}) error {
if src == nil || target == nil {
return nil
}
b, err := json.Marshal(src)
if reflect.ValueOf(target).Kind() != reflect.Ptr {
return errors.New("Target should be a pointer")
}
b, err := json.MarshalToString(src)
if err != nil {
return errors.WithStack(err)
}
if reflect.ValueOf(target).Kind() != reflect.Ptr {
return errors.New("Target should be a pointer")
dec := decoder.NewDecoder(b)
dec.UseInt64()
if err = dec.Decode(target); err != nil {
return errors.WithStack(err)
}
return json.Unmarshal(b, target)
return nil
}

0 comments on commit 2259bd5

Please sign in to comment.