Skip to content

Commit

Permalink
fix: add compress mechanism + fix the trace length + support New Reli…
Browse files Browse the repository at this point in the history
…c APM (#17)

* support New Relic APM
* add compress mechanism for Marshal() and Unmarshal() function
* fix the trace length is ~10x times of the cache size

Co-authored-by: viney-shih <[email protected]>
  • Loading branch information
VineyAT and viney-shih authored Jan 20, 2023
1 parent 8d7a414 commit 49fc89b
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 9 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: go

go:
- "1.16"
- "1.17"
- "1.18"
- "1.19"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ func ExampleCache_GetByFunc() {
CacheAttributes: map[cache.Type]cache.Attribute{
cache.LocalCacheType: {TTL: 10 * time.Minute},
},
MarshalFunc: msgpack.Marshal, // msgpack is from "github.com/vmihailenco/msgpack/v5"
UnmarshalFunc: msgpack.Unmarshal,
},
})

Expand Down Expand Up @@ -215,6 +217,8 @@ func ExampleService_Create_mGetter() {
// HINT: remember to return as a slice, and the item order needs to consist with the keys in the parameters.
return []Object{{Str: "value3", Num: 3}}, nil
},
MarshalFunc: cache.Marshal,
UnmarshalFunc: cache.Unmarshal,
},
})

Expand Down
5 changes: 5 additions & 0 deletions example_advanced_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/go-redis/redis/v8"
"github.com/vmihailenco/msgpack/v5"

"github.com/viney-shih/go-cache"
)
Expand Down Expand Up @@ -35,6 +36,8 @@ func Example_cacheAsidePattern() {
cache.SharedCacheType: {TTL: time.Hour},
cache.LocalCacheType: {TTL: 10 * time.Minute},
},
MarshalFunc: msgpack.Marshal,
UnmarshalFunc: msgpack.Unmarshal,
},
{
Prefix: "student",
Expand All @@ -54,6 +57,8 @@ func Example_cacheAsidePattern() {

return nil, fmt.Errorf("XD")
},
MarshalFunc: cache.Marshal,
UnmarshalFunc: cache.Unmarshal,
},
})

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ go 1.18
require (
github.com/go-redis/redis/v8 v8.11.4
github.com/google/uuid v1.3.0
github.com/klauspost/compress v1.15.14
github.com/stretchr/testify v1.7.0
github.com/vmihailenco/go-tinylfu v0.2.2
github.com/vmihailenco/msgpack/v5 v5.3.5
golang.org/x/exp v0.0.0-20210526181343-b47a03e3048a
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
)
Expand All @@ -16,5 +18,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/klauspost/compress v1.15.14 h1:i7WCKDToww0wA+9qrUZ1xOjp218vfFo3nTU6UHp+gOc=
github.com/klauspost/compress v1.15.14/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
Expand All @@ -44,10 +46,15 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/vmihailenco/go-tinylfu v0.2.2 h1:H1eiG6HM36iniK6+21n9LLpzx1G9R3DJa2UjUjbynsI=
github.com/vmihailenco/go-tinylfu v0.2.2/go.mod h1:CutYi2Q9puTxfcolkliPq4npPuofg9N9t8JVrjzwa3Q=
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/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
92 changes: 92 additions & 0 deletions marshaler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package cache

import (
"fmt"

"github.com/klauspost/compress/s2"
"github.com/vmihailenco/msgpack/v5"
)

// ref: https://github.com/go-redis/cache/blob/v8/cache.go

const (
compressionThreshold = 64
timeLen = 4
)

const (
noCompression = 0x0
s2Compression = 0x1
)

// Marshal marshals value by msgpack + compress
func Marshal(value interface{}) ([]byte, error) {
switch value := value.(type) {
case nil:
return nil, nil
case []byte:
return value, nil
case string:
return []byte(value), nil
}

b, err := msgpack.Marshal(value)
if err != nil {
return nil, err
}

return compress(b), nil
}

func compress(data []byte) []byte {
if len(data) < compressionThreshold {
n := len(data) + 1
b := make([]byte, n, n+timeLen)
copy(b, data)
b[len(b)-1] = noCompression
return b
}

n := s2.MaxEncodedLen(len(data)) + 1
b := make([]byte, n, n+timeLen)
b = s2.Encode(b, data)
b = append(b, s2Compression)
return b
}

// Unmarshal unmarshals binary with the compress + msgpack
func Unmarshal(b []byte, value interface{}) error {
if len(b) == 0 {
return nil
}

switch value := value.(type) {
case nil:
return nil
case *[]byte:
clone := make([]byte, len(b))
copy(clone, b)
*value = clone
return nil
case *string:
*value = string(b)
return nil
}

switch c := b[len(b)-1]; c {
case noCompression:
b = b[:len(b)-1]
case s2Compression:
b = b[:len(b)-1]

var err error
b, err = s2.Decode(nil, b)
if err != nil {
return err
}
default:
return fmt.Errorf("unknown compression method: %x", c)
}

return msgpack.Unmarshal(b, value)
}
121 changes: 121 additions & 0 deletions marshaler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package cache

import (
"testing"
"time"

"github.com/stretchr/testify/suite"
)

var (
mockTimeNow = time.Date(2022, 11, 23, 0, 0, 0, 0, time.Local)
)

type marshalerSuite struct {
suite.Suite
}

func (s *marshalerSuite) SetupSuite() {}

func (s *marshalerSuite) TearDownSuite() {}

func (s *marshalerSuite) SetupTest() {}

func (s *marshalerSuite) TearDownTest() {}

func TestMarshalerSuite(t *testing.T) {
suite.Run(t, new(marshalerSuite))
}

type mockStruct struct {
ID int64
Key string
CreatedAt time.Time
child *mockStruct
}

func (s *marshalerSuite) TestMarshaler() {
var bs []byte
var err error
marshal := Marshal
unmarshal := Unmarshal

// nil
var null error
bs, err = marshal(null)
s.Require().NoError(err)

var retNull error
s.Require().NoError(unmarshal(bs, &retNull))
s.Require().Equal(null, retNull)

// bytes
bytes := []byte("strings to bytes")
bs, err = marshal(bytes)
s.Require().NoError(err)

var retBytes []byte
s.Require().NoError(unmarshal(bs, &retBytes))
s.Require().Equal(bytes, retBytes)

// string
str := "this is a string"
bs, err = marshal(str)
s.Require().NoError(err)

var retStr string
s.Require().NoError(unmarshal(bs, &retStr))
s.Require().Equal(str, retStr)

// pointer
num := 100
intPtr := &num
bs, err = marshal(intPtr)
s.Require().NoError(err)

var retIntPtr *int
s.Require().NoError(unmarshal(bs, &retIntPtr))
s.Require().Equal(intPtr, retIntPtr)

// struct
st := mockStruct{
ID: 28825252,
Key: "I am rich",
CreatedAt: mockTimeNow,
}
bs, err = marshal(st)
s.Require().NoError(err)

retSt := mockStruct{}
s.Require().NoError(unmarshal(bs, &retSt))
s.Require().Equal(st, retSt)

// struct without nil pointer
st2 := mockStruct{
ID: 28825252,
Key: "I am rich",
CreatedAt: mockTimeNow,
child: &mockStruct{
ID: 2266,
},
}
bs, err = marshal(st2)
s.Require().NoError(err)

var retSt2 mockStruct
s.Require().NoError(unmarshal(bs, &retSt2))
s.Require().Equal(st, retSt2)

// compress
st3 := mockStruct{
ID: 1234567890,
Key: `1234567890123456789012345678901234567890123456789012345678901234567890`, // 70 chars
CreatedAt: mockTimeNow,
}
bs, err = marshal(st3)
s.Require().NoError(err)

var retSt3 mockStruct
s.Require().NoError(unmarshal(bs, &retSt3))
s.Require().Equal(st3, retSt3)
}
6 changes: 3 additions & 3 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (r *rds) MSet(
return nil
}

_, err := r.ring.Pipelined(ctx, func(pipe redis.Pipeliner) error {
_, err := r.ring.WithContext(ctx).Pipelined(ctx, func(pipe redis.Pipeliner) error {
// set multiple pairs
pairSlice := make([]interface{}, len(keyVals)*2)
i := 0
Expand All @@ -63,7 +63,7 @@ func (r *rds) MSet(
}

func (r *rds) MGet(ctx context.Context, keys []string) ([]Value, error) {
vals, err := r.ring.MGet(ctx, keys...).Result()
vals, err := r.ring.WithContext(ctx).MGet(ctx, keys...).Result()
if err != nil {
return nil, err
}
Expand All @@ -88,7 +88,7 @@ func (r *rds) MGet(ctx context.Context, keys []string) ([]Value, error) {
}

func (r *rds) Del(ctx context.Context, keys ...string) error {
_, err := r.ring.Del(ctx, keys...).Result()
_, err := r.ring.WithContext(ctx).Del(ctx, keys...).Result()

return err
}
Expand Down
11 changes: 6 additions & 5 deletions tinylfu.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import (
)

const (
maxOffset = 10 * time.Second
defaultSamples = 100000
defaultOffset = -1
maxOffset = 10 * time.Second
defaultOffset = -1
)

type tinyLFU struct {
Expand All @@ -26,10 +25,12 @@ type tinyLFU struct {

// NewTinyLFU generates Adapter with tinylfu
func NewTinyLFU(size int, options ...TinyLFUOptions) Adapter {
// number of keys to track frequency
// samples are the number of keys to track frequency
// TinyLFU works best for small number of keys (~ 100k)
// Ref: https://github.com/vmihailenco/go-cache-benchmark
samples := defaultSamples
// consider the discussing in (https://github.com/ben-manes/caffeine/issues/106),
// choose ~10x the cache size as the default value.
samples := size * 10

o := loadtinyLFUOptions(options...)
if o.offset != defaultOffset && o.offset < 0 {
Expand Down

0 comments on commit 49fc89b

Please sign in to comment.