Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(os/gcache): optimize locking mechanism in SetWithLock method #4153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions os/gcache/gcache_adapter_memory_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,13 @@ func (d *memoryData) SetMap(data map[interface{}]interface{}, expireTime int64)
}

func (d *memoryData) SetWithLock(ctx context.Context, key interface{}, value interface{}, expireTimestamp int64) (interface{}, error) {
d.mu.Lock()
defer d.mu.Unlock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SetWithLock方法的作用就是要在锁中执行用户给定的Func,建议参考我在issue中评论的方案。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我仔细思考了一下,感觉那个提问者说的对不同的 key 值进行锁定的方案可能会更好一些

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

他那个问题是内部递归调用涉及到同一个缓存对象的Lock函数的时候死锁了,用我说的那个方案就可以很简单解决。

var (
err error
)
if v, ok := d.data[key]; ok && !v.IsExpired() {
d.mu.Lock()
v, ok := d.data[key]
d.mu.Unlock()
if ok && !v.IsExpired() {
return v.v, nil
}
f, ok := value.(Func)
Expand All @@ -203,7 +204,9 @@ func (d *memoryData) SetWithLock(ctx context.Context, key interface{}, value int
return nil, nil
}
}
d.mu.Lock()
d.data[key] = memoryDataItem{v: value, e: expireTimestamp}
d.mu.Unlock()
return value, nil
}

Expand Down
89 changes: 89 additions & 0 deletions os/gcache/gcache_z_unit_issue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package gcache_test

import (
"context"
"testing"
"time"

"github.com/gogf/gf/v2/os/gcache"
"github.com/gogf/gf/v2/test/gtest"
)

// https://github.com/gogf/gf/issues/4145
func Test_Issue4145(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
ctx = context.Background()
cache = gcache.New()
cacheKey1 = "GetTest-1"
cacheKey2 = "GetTest2-1"
cacheValue = "123456789"
)

// 定义需要测试的闭包函数
getTestCached := func(ctx context.Context) (*string, error) {
v, err := cache.GetOrSetFuncLock(ctx, cacheKey1, func(ctx context.Context) (interface{}, error) {
str := cacheValue
return &str, nil
}, 1*time.Minute)

if err != nil {
return nil, err
}

var res *string
if err := v.Struct(&res); err != nil {
return nil, err
}
return res, nil
}

getTest2Cached := func(ctx context.Context) (*string, error) {
v, err := cache.GetOrSetFuncLock(ctx, cacheKey2, func(ctx context.Context) (interface{}, error) {
// 内部调用 getTestCached
return getTestCached(ctx)
}, 1*time.Minute)

if err != nil {
return nil, err
}

var res *string
if err := v.Struct(&res); err != nil {
return nil, err
}
return res, nil
}

// 测试用例
// 第一次获取应该走实际逻辑
value, err := getTestCached(ctx)
t.AssertNil(err)
t.Assert(*value, cacheValue)

// 第二次获取应该走缓存
v, err := cache.Get(ctx, cacheKey1)
t.AssertNil(err)
t.Assert(v, cacheValue)

// 测试嵌套缓存调用
value, err = getTest2Cached(ctx)
t.AssertNil(err)
t.Assert(*value, cacheValue)

// 验证二级缓存
v, err = cache.Get(ctx, cacheKey2)
t.AssertNil(err)
t.Assert(v, cacheValue)

// 清理所有缓存
_, err = cache.Remove(ctx, cacheKey1, cacheKey2)
t.AssertNil(err)

// 验证清理结果
v1, _ := cache.Get(ctx, cacheKey1)
v2, _ := cache.Get(ctx, cacheKey2)
t.Assert(v1, nil)
t.Assert(v2, nil)
})
}
Loading