Skip to content

Commit fe855c5

Browse files
committed
avoid bigint converted into float64 when unmarshaling
1 parent 3f8b080 commit fe855c5

File tree

4 files changed

+35
-36
lines changed

4 files changed

+35
-36
lines changed

core/stores/cache/cachenode.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package cache
22

33
import (
4-
"encoding/json"
54
"errors"
65
"fmt"
76
"math/rand"
87
"sync"
98
"time"
109

10+
"github.com/tal-tech/go-zero/core/jsonx"
1111
"github.com/tal-tech/go-zero/core/logx"
1212
"github.com/tal-tech/go-zero/core/mathx"
1313
"github.com/tal-tech/go-zero/core/stat"
@@ -79,7 +79,7 @@ func (c cacheNode) SetCache(key string, v interface{}) error {
7979
}
8080

8181
func (c cacheNode) SetCacheWithExpire(key string, v interface{}, expire time.Duration) error {
82-
data, err := json.Marshal(v)
82+
data, err := jsonx.Marshal(v)
8383
if err != nil {
8484
return err
8585
}
@@ -168,7 +168,7 @@ func (c cacheNode) doTake(v interface{}, key string, query func(v interface{}) e
168168
}
169169
}
170170

171-
return json.Marshal(v)
171+
return jsonx.Marshal(v)
172172
})
173173
if err != nil {
174174
return err
@@ -181,11 +181,11 @@ func (c cacheNode) doTake(v interface{}, key string, query func(v interface{}) e
181181
c.stat.IncrementHit()
182182
}
183183

184-
return json.Unmarshal(val.([]byte), v)
184+
return jsonx.Unmarshal(val.([]byte), v)
185185
}
186186

187187
func (c cacheNode) processCache(key string, data string, v interface{}) error {
188-
err := json.Unmarshal([]byte(data), v)
188+
err := jsonx.Unmarshal([]byte(data), v)
189189
if err == nil {
190190
return nil
191191
}

core/stores/cache/cachenode_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package cache
22

33
import (
44
"errors"
5+
"fmt"
56
"math/rand"
7+
"strconv"
68
"sync"
79
"testing"
810
"time"
@@ -176,3 +178,31 @@ func TestCacheNode_String(t *testing.T) {
176178
}
177179
assert.Equal(t, s.Addr(), cn.String())
178180
}
181+
182+
func TestCacheValueWithBigInt(t *testing.T) {
183+
s, err := miniredis.Run()
184+
if err != nil {
185+
t.Error(err)
186+
}
187+
defer s.Close()
188+
189+
cn := cacheNode{
190+
rds: redis.NewRedis(s.Addr(), redis.NodeType),
191+
r: rand.New(rand.NewSource(time.Now().UnixNano())),
192+
barrier: syncx.NewSharedCalls(),
193+
lock: new(sync.Mutex),
194+
unstableExpiry: mathx.NewUnstable(expiryDeviation),
195+
stat: NewCacheStat("any"),
196+
errNotFound: errors.New("any"),
197+
}
198+
199+
const (
200+
key = "key"
201+
value int64 = 323427211229009810
202+
)
203+
204+
assert.Nil(t, cn.SetCache(key, value))
205+
var val interface{}
206+
assert.Nil(t, cn.GetCache(key, &val))
207+
assert.Equal(t, strconv.FormatInt(value, 10), fmt.Sprintf("%v", val))
208+
}

core/stores/sqlc/cachedsql.go

-17
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ func (cc CachedConn) QueryRowIndex(v interface{}, key string, keyer func(primary
8383
var primaryKey interface{}
8484
var found bool
8585

86-
// if don't use convert numeric primary key into int64,
87-
// then it will be represented as scientific notion, like 2e6
88-
// which will make the cache doesn't match with the previous insert one
89-
keyer = floatKeyer(keyer)
9086
if err := cc.cache.TakeWithExpire(&primaryKey, key, func(val interface{}, expire time.Duration) (err error) {
9187
primaryKey, err = indexQuery(cc.db, v)
9288
if err != nil {
@@ -124,16 +120,3 @@ func (cc CachedConn) SetCache(key string, v interface{}) error {
124120
func (cc CachedConn) Transact(fn func(sqlx.Session) error) error {
125121
return cc.db.Transact(fn)
126122
}
127-
128-
func floatKeyer(fn func(interface{}) string) func(interface{}) string {
129-
return func(primary interface{}) string {
130-
switch v := primary.(type) {
131-
case float32:
132-
return fn(int64(v))
133-
case float64:
134-
return fn(int64(v))
135-
default:
136-
return fn(primary)
137-
}
138-
}
139-
}

core/stores/sqlc/cachedsql_test.go

-14
Original file line numberDiff line numberDiff line change
@@ -594,20 +594,6 @@ func TestQueryRowNoCache(t *testing.T) {
594594
assert.True(t, ran)
595595
}
596596

597-
func TestFloatKeyer(t *testing.T) {
598-
primaries := []interface{}{
599-
float32(1),
600-
float64(1),
601-
}
602-
603-
for _, primary := range primaries {
604-
val := floatKeyer(func(i interface{}) string {
605-
return fmt.Sprint(i)
606-
})(primary)
607-
assert.Equal(t, "1", val)
608-
}
609-
}
610-
611597
func resetStats() {
612598
atomic.StoreUint64(&stats.Total, 0)
613599
atomic.StoreUint64(&stats.Hit, 0)

0 commit comments

Comments
 (0)