Skip to content

Commit

Permalink
Merge pull request #7 from daoshenzzg/refactor_mget
Browse files Browse the repository at this point in the history
Refactor mget
  • Loading branch information
daoshenzzg authored Mar 7, 2024
2 parents fae95ae + 47cba5b commit 60d634e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
55 changes: 55 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,24 @@ var _ = Describe("Cache", func() {
Expect(ret).To(Equal(map[int]*object{}))
}
})

It("with will skip elements that remote MSet error", func() {
if cache.CacheType() == TypeRemote {
codecErrCache := New(WithName("codecErr"),
WithRemote(&mockGoRedisMGetMSetErrAdapter{}))
cacheT := NewT[int, *object](codecErrCache)
ids := []int{1, 2, 3}
// 1st marshal error, but return origin load func data
ret := cacheT.MGet(context.Background(), "key", ids,
func(ctx context.Context, ids []int) (map[int]*object, error) {
return map[int]*object{1: {Str: "str1", Num: 1}, 2: {Str: "str2", Num: 2}}, nil
})
Expect(ret).To(Equal(map[int]*object{1: {Str: "str1", Num: 1}, 2: {Str: "str2", Num: 2}}))
// 2nd cache hit placeholder "*", then return miss
ret = cacheT.MGet(context.Background(), "key", ids, nil)
Expect(ret).To(Equal(map[int]*object{}))
}
})
})

Describe("Once func", func() {
Expand Down Expand Up @@ -738,3 +756,40 @@ func (mockEncode) Unmarshal(data []byte, v interface{}) error {
func (mockEncode) Name() string {
return mockMarshalErr
}

var _ remote.Remote = (*mockGoRedisMGetMSetErrAdapter)(nil)

type mockGoRedisMGetMSetErrAdapter struct {
}

func (m mockGoRedisMGetMSetErrAdapter) SetEX(ctx context.Context, key string, value any, expire time.Duration) error {
panic("implement me")
}

func (m mockGoRedisMGetMSetErrAdapter) SetNX(ctx context.Context, key string, value any, expire time.Duration) (val bool, err error) {
panic("implement me")
}

func (m mockGoRedisMGetMSetErrAdapter) SetXX(ctx context.Context, key string, value any, expire time.Duration) (val bool, err error) {
panic("implement me")
}

func (m mockGoRedisMGetMSetErrAdapter) Get(ctx context.Context, key string) (val string, err error) {
panic("implement me")
}

func (m mockGoRedisMGetMSetErrAdapter) Del(ctx context.Context, key string) (val int64, err error) {
panic("implement me")
}

func (m mockGoRedisMGetMSetErrAdapter) MGet(ctx context.Context, keys ...string) (map[string]any, error) {
return nil, errors.New("any")
}

func (m mockGoRedisMGetMSetErrAdapter) MSet(ctx context.Context, value map[string]any, expire time.Duration) error {
return errors.New("any")
}

func (m mockGoRedisMGetMSetErrAdapter) Nil() error {
panic("implement me")
}
11 changes: 5 additions & 6 deletions cachegeneric.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func (w *T[K, V]) MGet(ctx context.Context, key string, ids []K, fn func(context
fnValues, err := fn(ctx, missIds)
if err != nil {
c.statsHandler.IncrQueryFail(err)
logger.Error("MGet#fn(%s) error(%v)", util.JoinAny(",", ids), err)
} else {
placeholderValues := make(map[string]any, len(ids))
cacheValues := make(map[string]any, len(ids))
Expand All @@ -40,7 +39,7 @@ func (w *T[K, V]) MGet(ctx context.Context, key string, ids []K, fn func(context
cacheKey := util.JoinAny(":", key, rk)
if b, err := c.Marshal(rv); err != nil {
placeholderValues[cacheKey] = notFoundPlaceholder
logger.Error("MGet#w.Marshal(%v) error(%v)", rv, err)
logger.Warn("MGet#w.Marshal(%v) error(%v)", rv, err)
} else {
cacheValues[cacheKey] = b
}
Expand Down Expand Up @@ -69,12 +68,12 @@ func (w *T[K, V]) MGet(ctx context.Context, key string, ids []K, fn func(context
if c.remote != nil {
if len(placeholderValues) > 0 {
if err = c.remote.MSet(ctx, placeholderValues, c.notFoundExpiry); err != nil {
logger.Error("MGet#MSet error(%v)", err)
logger.Warn("MGet#remote.MSet error(%v)", err)
}
}
if len(cacheValues) > 0 {
if err = c.remote.MSet(ctx, cacheValues, c.remoteExpiry); err != nil {
logger.Error("MGet#MSet error(%v)", err)
logger.Warn("MGet#remote.MSet error(%v)", err)
}
}
}
Expand All @@ -100,7 +99,7 @@ func (w *T[K, V]) mGetCache(ctx context.Context, key string, ids []K) (v map[K]V
}
var varT V
if err := c.Unmarshal(b, &varT); err != nil {
logger.Error("mGetCache#c.Unmarshal(%s) error(%v)", cacheKey, err)
logger.Warn("mGetCache#c.Unmarshal(%s) error(%v)", cacheKey, err)
} else {
v[id] = varT
}
Expand Down Expand Up @@ -130,7 +129,7 @@ func (w *T[K, V]) mGetCache(ctx context.Context, key string, ids []K) (v map[K]V
}
var varT V
if err = c.Unmarshal(b, &varT); err != nil {
logger.Error("mGetCache#c.Unmarshal(%s) error(%v)", mk, err)
logger.Warn("mGetCache#c.Unmarshal(%s) error(%v)", mk, err)
} else {
v[mv] = varT
if c.local != nil {
Expand Down
9 changes: 2 additions & 7 deletions remote/goredisv8adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"time"

"github.com/go-redis/redis/v8"

"github.com/daoshenzzg/jetcache-go/logger"
)

var _ Remote = (*GoRedisV8Adaptor)(nil)
Expand Down Expand Up @@ -55,16 +53,13 @@ func (r *GoRedisV8Adaptor) MGet(ctx context.Context, keys ...string) (map[string

cmder, err := pipeline.Exec(ctx)
if err != nil && !errors.Is(err, r.Nil()) {
logger.Error("MGet:pipeline.Exec error(%v)", err)
return nil, err
}

for idx, cmd := range cmder {
if strCmd, ok := cmd.(*redis.StringCmd); ok {
key := keyIdxMap[idx]
val, err := strCmd.Result()
if err != nil && !errors.Is(err, r.Nil()) {
logger.Error("MGet#strCmd(%s) error(%v)", keyIdxMap[idx], err)
} else if len(val) > 0 {
if val, _ := strCmd.Result(); len(val) > 0 {
ret[key] = val
}
}
Expand Down

0 comments on commit 60d634e

Please sign in to comment.