-
Notifications
You must be signed in to change notification settings - Fork 71
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
Support EVALSHA #21
Merged
Merged
Support EVALSHA #21
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c3ad74e
feat: support cancellable sleep
WqyJh 6fe30bb
feat: support evalsha to optimize redis script call
WqyJh 8cafd1d
refactor: add new line before every script
WqyJh e71837c
fix: add coverage tests
WqyJh 299fede
fix: ci lint
WqyJh b20aab9
fix: coverage
WqyJh 392b16d
refactor: simplify code
WqyJh 2040bb9
test: fix golangci-lint
WqyJh 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
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
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,77 @@ | ||
package rockscache | ||
|
||
import "github.com/redis/go-redis/v9" | ||
|
||
var ( | ||
deleteScript = redis.NewScript(`redis.call('HSET', KEYS[1], 'lockUntil', 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a new line at the first of the script? That may make the code more readable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
redis.call('HDEL', KEYS[1], 'lockOwner') | ||
redis.call('EXPIRE', KEYS[1], ARGV[1])`) | ||
|
||
getScript = redis.NewScript(`local v = redis.call('HGET', KEYS[1], 'value') | ||
local lu = redis.call('HGET', KEYS[1], 'lockUntil') | ||
if lu ~= false and tonumber(lu) < tonumber(ARGV[1]) or lu == false and v == false then | ||
redis.call('HSET', KEYS[1], 'lockUntil', ARGV[2]) | ||
redis.call('HSET', KEYS[1], 'lockOwner', ARGV[3]) | ||
return { v, 'LOCKED' } | ||
end | ||
return {v, lu}`) | ||
|
||
setScript = redis.NewScript(`local o = redis.call('HGET', KEYS[1], 'lockOwner') | ||
if o ~= ARGV[2] then | ||
return | ||
end | ||
redis.call('HSET', KEYS[1], 'value', ARGV[1]) | ||
redis.call('HDEL', KEYS[1], 'lockUntil') | ||
redis.call('HDEL', KEYS[1], 'lockOwner') | ||
redis.call('EXPIRE', KEYS[1], ARGV[3])`) | ||
|
||
lockScript = redis.NewScript(`local lu = redis.call('HGET', KEYS[1], 'lockUntil') | ||
local lo = redis.call('HGET', KEYS[1], 'lockOwner') | ||
if lu == false or tonumber(lu) < tonumber(ARGV[2]) or lo == ARGV[1] then | ||
redis.call('HSET', KEYS[1], 'lockUntil', ARGV[2]) | ||
redis.call('HSET', KEYS[1], 'lockOwner', ARGV[1]) | ||
return 'LOCKED' | ||
end | ||
return lo`) | ||
|
||
unlockScript = redis.NewScript(`local lo = redis.call('HGET', KEYS[1], 'lockOwner') | ||
if lo == ARGV[1] then | ||
redis.call('HSET', KEYS[1], 'lockUntil', 0) | ||
redis.call('HDEL', KEYS[1], 'lockOwner') | ||
redis.call('EXPIRE', KEYS[1], ARGV[2]) | ||
end`) | ||
|
||
getBatchScript = redis.NewScript(`local rets = {} | ||
for i, key in ipairs(KEYS) | ||
do | ||
local v = redis.call('HGET', key, 'value') | ||
local lu = redis.call('HGET', key, 'lockUntil') | ||
if lu ~= false and tonumber(lu) < tonumber(ARGV[1]) or lu == false and v == false then | ||
redis.call('HSET', key, 'lockUntil', ARGV[2]) | ||
redis.call('HSET', key, 'lockOwner', ARGV[3]) | ||
table.insert(rets, { v, 'LOCKED' }) | ||
else | ||
table.insert(rets, {v, lu}) | ||
end | ||
end | ||
return rets`) | ||
|
||
setBatchScript = redis.NewScript(`local n = #KEYS | ||
for i, key in ipairs(KEYS) | ||
do | ||
local o = redis.call('HGET', key, 'lockOwner') | ||
if o ~= ARGV[1] then | ||
return | ||
end | ||
redis.call('HSET', key, 'value', ARGV[i+1]) | ||
redis.call('HDEL', key, 'lockUntil') | ||
redis.call('HDEL', key, 'lockOwner') | ||
redis.call('EXPIRE', key, ARGV[i+1+n]) | ||
end`) | ||
|
||
deleteBatchScript = redis.NewScript(`for i, key in ipairs(KEYS) do | ||
redis.call('HSET', key, 'lockUntil', 0) | ||
redis.call('HDEL', key, 'lockOwner') | ||
redis.call('EXPIRE', key, ARGV[1]) | ||
end`) | ||
) |
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
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.
Why not put ticker inside the for loop? Code can be cleaner
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.
reuse & reduce allocation
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.
The performance gained in this way should be very very little and can be ignored. Why not use the cleaner way?
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.
simplified