[fix bug] Fix redis cache adapter GetOrSetFunc, GetOrSetFuncLock method bug and add unit test

This commit is contained in:
yxh
2022-03-09 17:21:02 +08:00
parent 8f326dcac5
commit 2510e0412d
2 changed files with 35 additions and 2 deletions

View File

@ -189,7 +189,7 @@ func (c *AdapterRedis) GetOrSet(ctx context.Context, key interface{}, value inte
if err != nil {
return nil, err
}
if result == nil {
if result.IsNil() {
return gvar.New(value), c.Set(ctx, key, value, duration)
}
return
@ -207,7 +207,7 @@ func (c *AdapterRedis) GetOrSetFunc(ctx context.Context, key interface{}, f Func
if err != nil {
return nil, err
}
if v == nil {
if v.IsNil() {
value, err := f(ctx)
if err != nil {
return nil, err

View File

@ -7,6 +7,7 @@
package gcache_test
import (
"context"
"testing"
"time"
@ -177,3 +178,35 @@ func Test_AdapterRedis_SetIfNotExist(t *testing.T) {
t.Assert(d <= time.Second, true)
})
}
func Test_AdapterRedis_GetOrSetFunc(t *testing.T) {
defer cacheRedis.Clear(ctx)
gtest.C(t, func(t *gtest.T) {
var (
key = "key"
value1 = "valueFunc"
)
v, err := cacheRedis.GetOrSetFunc(ctx, key, func(ctx context.Context) (value interface{}, err error) {
value = value1
return
}, 0)
t.AssertNil(err)
t.Assert(v, value1)
})
}
func Test_AdapterRedis_GetOrSetFuncLock(t *testing.T) {
defer cacheRedis.Clear(ctx)
gtest.C(t, func(t *gtest.T) {
var (
key = "key"
value1 = "valueFuncLock"
)
v, err := cacheRedis.GetOrSetFuncLock(ctx, key, func(ctx context.Context) (value interface{}, err error) {
value = value1
return
}, time.Second*60)
t.AssertNil(err)
t.Assert(v, value1)
})
}