mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
[fix bug] Fix redis cache adapter GetOrSetFunc, GetOrSetFuncLock method bug and add unit test
This commit is contained in:
@ -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
|
||||
|
||||
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user