-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
wlynxg
wants to merge
1
commit into
gogf:master
Choose a base branch
from
wlynxg:issue4145
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SetWithLock
方法的作用就是要在锁中执行用户给定的Func
,建议参考我在issue
中评论的方案。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我仔细思考了一下,感觉那个提问者说的对不同的 key 值进行锁定的方案可能会更好一些
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
他那个问题是内部递归调用涉及到同一个缓存对象的Lock函数的时候死锁了,用我说的那个方案就可以很简单解决。