Skip to content

Commit

Permalink
fix(redis): umarshal err override
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuigo committed Dec 5, 2023
1 parent 55278a8 commit ec58fe0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
5 changes: 4 additions & 1 deletion decorator.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ checkCache:

// 3.1 check if marshal needed
if hasCache && c.cacheMap.NeedMarshal() {
err = json.Unmarshal(value.([]byte), &retv)
err2 := json.Unmarshal(value.([]byte), &retv)
if err == nil {
err = err2
}
return retv, err
}

Expand Down
56 changes: 56 additions & 0 deletions examples/decorator-redis-err_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package examples

import (
"errors"
"testing"
"time"

"github.com/ahuigo/gofnext"
)

func TestRedisCacheClientPanic(t *testing.T) {
defer func() {
r := recover() //r.(string)
if r==nil{
t.Error("should panic")
}
}()
gofnext.NewCacheRedis("")

}

func TestRedisCacheFuncErr(t *testing.T) {
// Original function
executeCount := 0
getUserScore := func(more int) (int, error) {
executeCount++
return 98 + more, errors.New("db error")
}

// Cacheable Function
getUserScoreFromDbWithCache := gofnext.CacheFn1Err(getUserScore, &gofnext.Config{
TTL: 500*time.Second,
CacheMap: gofnext.NewCacheRedis("redis-cache-key").ClearAll(),
})

// Parallel invocation of multiple functions.
for i := 0; i < 10; i++ {
score, err := getUserScoreFromDbWithCache(1)
if err == nil {
t.Errorf("should be error, but get nil")
}
if score != 99 {
t.Errorf("score should be 99, but get %d", score)
}
score, _ = getUserScoreFromDbWithCache(2)
if score != 100 {
t.Fatalf("score should be 100, but get %d", score)
}
getUserScoreFromDbWithCache(3)
getUserScoreFromDbWithCache(3)
}

if executeCount != 3 {
t.Errorf("executeCount should be 1, but get %d", executeCount)
}
}
1 change: 1 addition & 0 deletions examples/decorator-redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestRedisCacheClient(t *testing.T) {
cache.SetRedisUniversalOpts(&redis.UniversalOptions{
Addrs: []string{"localhost:6379"},
})
cache.SetMaxHashKeyLen(0)
cache.SetMaxHashKeyLen(100)
}

Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.0.14
v0.0.15

0 comments on commit ec58fe0

Please sign in to comment.