Skip to content

Commit

Permalink
feat(freecache):
Browse files Browse the repository at this point in the history
1、格式化import的路径
2、预分配len(ids)的内存
3、封装unmarshal和marshal方法
  • Loading branch information
彭业昌 committed Feb 14, 2023
1 parent 598fb9e commit 1311c5c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 46 deletions.
3 changes: 1 addition & 2 deletions pkg/cache/xfreecache/cache_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package xfreecache

import (
"fmt"

"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down
25 changes: 5 additions & 20 deletions pkg/cache/xfreecache/v2/cache.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package xfreecache

import (
"encoding/json"
"fmt"
"github.com/douyu/jupiter/pkg/xlog"
"github.com/samber/lo"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
"reflect"
)

type storage interface {
Expand Down Expand Up @@ -39,22 +36,13 @@ func (c *cache[K, V]) GetAndSetCacheMap(key string, ids []K, fn func([]K) (map[K

// id去重
ids = lo.Uniq(ids)
idsNone := make([]K, 0)
idsNone := make([]K, 0, len(ids))
for _, id := range ids {
cacheKey := c.getKey(key, id)
if resT, innerErr := c.GetCacheData(cacheKey); innerErr == nil && resT != nil {
var value V
if msg, ok := any(value).(proto.Message); ok { // Constrained to proto.Message
// Peek the type inside T (as T= *SomeProtoMsgType)
msgType := reflect.TypeOf(msg).Elem()
// Make a new one, and throw it back into T
msg = reflect.New(msgType).Interface().(proto.Message)

err = proto.Unmarshal(resT, msg)
value = msg.(V)
} else {
err = json.Unmarshal(resT, &value)
}
// 反序列化
value, err = unmarshal[V](resT)
if err != nil {
return
}
Expand Down Expand Up @@ -90,11 +78,8 @@ func (c *cache[K, V]) GetAndSetCacheMap(key string, ids []K, fn func([]K) (map[K
if val, ok := v[id]; ok {
cacheData = val
}
if msg, ok := any(cacheData).(proto.Message); ok {
data, err = proto.Marshal(msg)
} else {
data, err = json.Marshal(cacheData)
}
// 序列化
data, err = marshal(cacheData)

if err != nil {
xlog.Jupiter().Error("GetAndSetCacheMap Marshal", append(args, zap.Error(err))...)
Expand Down
5 changes: 3 additions & 2 deletions pkg/cache/xfreecache/v2/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package xfreecache
import (
"bytes"
"fmt"
"testing"
"time"

"github.com/BurntSushi/toml"
"github.com/douyu/jupiter/pkg/conf"
helloworldv1 "github.com/douyu/jupiter/proto/helloworld/v1"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

type Student struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/xfreecache/v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package xfreecache

import (
"fmt"
cfg "github.com/douyu/jupiter/pkg/conf"
"sync"
"time"

"github.com/coocood/freecache"
cfg "github.com/douyu/jupiter/pkg/conf"
"github.com/douyu/jupiter/pkg/xlog"
"go.uber.org/zap"
)
Expand Down
35 changes: 35 additions & 0 deletions pkg/cache/xfreecache/v2/encode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package xfreecache

import (
"encoding/json"
"reflect"

"google.golang.org/protobuf/proto"
)

// 反序列化,如果是pb格式,则使用proto序列化
func unmarshal[T any](body []byte) (value T, err error) {
if msg, ok := any(value).(proto.Message); ok { // Constrained to proto.Message
// Peek the type inside T (as T= *SomeProtoMsgType)
msgType := reflect.TypeOf(msg).Elem()

// Make a new one, and throw it back into T
msg = reflect.New(msgType).Interface().(proto.Message)

err = proto.Unmarshal(body, msg)
value = msg.(T)
} else {
err = json.Unmarshal(body, &value)
}
return
}

// 序列化,如果是pb格式,则使用proto序列化
func marshal[T any](cacheData T) (data []byte, err error) {
if msg, ok := any(cacheData).(proto.Message); ok {
data, err = proto.Marshal(msg)
} else {
data, err = json.Marshal(cacheData)
}
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package xfreecache

import (
"encoding/json"
helloworldv1 "github.com/douyu/jupiter/proto/helloworld/v1"
"reflect"
"testing"

helloworldv1 "github.com/douyu/jupiter/proto/helloworld/v1"
jsoniter "github.com/json-iterator/go"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -66,31 +65,14 @@ func BenchmarkDecodeProto(b *testing.B) {
func BenchmarkEncodeProto(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = proto.Marshal(helloReply)
_, _ = marshal[*helloworldv1.SayHiResponse](helloReply)
}
}

func BenchmarkDecodeProtoWithReflect(b *testing.B) {
res, _ := proto.Marshal(helloReply)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = protoUnmarshal[*helloworldv1.SayHiResponse](res)
_, _ = unmarshal[*helloworldv1.SayHiResponse](res)
}
}

func protoUnmarshal[T any](body []byte) T {
var value T

if msg, ok := any(value).(proto.Message); ok { // Constrained to proto.Message
// Peek the type inside T (as T= *SomeProtoMsgType)
msgType := reflect.TypeOf(msg).Elem()

// Make a new one, and throw it back into T
msg = reflect.New(msgType).Interface().(proto.Message)

_ = proto.Unmarshal(body, msg)
value = msg.(T)
}

return value
}

0 comments on commit 1311c5c

Please sign in to comment.