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 dde8498
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 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 readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
![Go Version](https://img.shields.io/badge/Go-%3E%3D%201.21-%23007d9c)
[![GoDoc](https://godoc.org/github.com/ahuigo/gofnext?status.svg)](https://pkg.go.dev/github.com/ahuigo/gofnext)
![Build Status](https://github.com/ahuigo/gofnext/actions/workflows/test.yml/badge.svg)
[![Go report](https://goreportcard.com/badge/github.com/ahuigo/gofnext)](https://goreportcard.com/report/github.com/ahuigo/gofnext)
[![Go report](https://goreportcard.com/badge/github.com/ahuigo/gofnext?a=1)](https://goreportcard.com/report/github.com/ahuigo/gofnext)
[![Coverage](https://img.shields.io/codecov/c/github/ahuigo/gofnext)](https://codecov.io/gh/ahuigo/gofnext)
[![Contributors](https://img.shields.io/github/contributors/ahuigo/gofnext)](https://github.com/ahuigo/gofnext/graphs/contributors)
[![License](https://img.shields.io/github/license/ahuigo/gofnext)](./LICENSE)
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 dde8498

Please sign in to comment.