Skip to content

Commit

Permalink
refactor: redis-script
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed Feb 23, 2023
1 parent a74cb48 commit 741b846
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
12 changes: 6 additions & 6 deletions simba-spring-redis/src/main/resources/mutex_acquire.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ local contenderId = ARGV[1];
local transition = ARGV[2];
local mutexKey = 'simba:' .. mutex;
-- 1. 尝试获取锁资源,如果获取成功直接返回
local succeed = redis.call("set", mutexKey, contenderId, 'nx', 'px', transition)
local succeed = redis.call('set', mutexKey, contenderId, 'nx', 'px', transition)

if succeed then
local message = 'acquired@@' .. contenderId;
redis.call("publish", mutexKey, message)
redis.call('publish', mutexKey, message)
return contenderId..'@@'..transition;
end

-- 2. 将自己加入互斥体等待队列
local contenderQueueKey = mutexKey .. ":contender";
local contenderQueueKey = mutexKey .. ':contender';

local nowTime = redis.call('time')[1];
redis.call("zadd", contenderQueueKey, 'nx', nowTime, contenderId)
redis.call('zadd', contenderQueueKey, 'nx', nowTime, contenderId)
-- 获取当前持有者 & ttl
local ownerId=redis.call("get",mutexKey)
local ttl=redis.call("pttl",mutexKey)
local ownerId=redis.call('get',mutexKey)
local ttl=redis.call('pttl',mutexKey)
return ownerId..'@@'..ttl;
8 changes: 4 additions & 4 deletions simba-spring-redis/src/main/resources/mutex_guard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ local mutexKey = 'simba:' .. mutex;

-- 获取当前持有者 & ttl
local function getCurrentOwner(mutexKey)
local ownerId = redis.call("get", mutexKey)
local ownerId = redis.call('get', mutexKey)
if ownerId then
local ttl = redis.call("pttl", mutexKey)
local ttl = redis.call('pttl', mutexKey)
return ownerId .. '@@' .. ttl;
end
return '@@';
end

-- 1. 判断当前持有互斥体的是否为自己
if redis.call("get", mutexKey) ~= contenderId then
if redis.call('get', mutexKey) ~= contenderId then
return getCurrentOwner(mutexKey)
end

if redis.call("set", mutexKey, contenderId, 'xx', 'px', transition) then
if redis.call('set', mutexKey, contenderId, 'xx', 'px', transition) then
return contenderId .. '@@' .. transition;
else
return getCurrentOwner(mutexKey)
Expand Down
16 changes: 8 additions & 8 deletions simba-spring-redis/src/main/resources/mutex_release.lua
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
local mutex = KEYS[1];
local contenderId = ARGV[1];
local mutexKey = 'simba:' .. mutex;
local contenderQueueKey = mutexKey .. ":contender";
local contenderQueueKey = mutexKey .. ':contender';
-- 1. 获取当前锁的 contenderId 锁是否与自己持久的相同(判断当前锁是否是自己锁定的),如果不相同则直接退出,返回释放失败

if redis.call("get", mutexKey) ~= contenderId then
redis.call("zrem", contenderQueueKey, contenderId)
if redis.call('get', mutexKey) ~= contenderId then
redis.call('zrem', contenderQueueKey, contenderId)
return 0;
end

-- 2. 删除当前锁定的资源

local succeed = redis.call("del", mutexKey)
local succeed = redis.call('del', mutexKey)

if not succeed then
return succeed;
end

-- 3.从等待队列里边获取到第一个 contender,并发布锁释放通知

local contenderQueue = redis.call("zrevrange", contenderQueueKey, -1, -1);
local contenderQueue = redis.call('zrevrange', contenderQueueKey, -1, -1);

if #contenderQueue == 0 then
return succeed;
end

local nextContender = contenderQueue[1];
redis.call("zrem", contenderQueueKey, nextContender)
redis.call('zrem', contenderQueueKey, nextContender)

local channel = mutexKey .. ":" .. nextContender;
local channel = mutexKey .. ':' .. nextContender;
local message = 'released@@' .. contenderId;
redis.call("publish", channel, message)
redis.call('publish', channel, message)

return succeed;

0 comments on commit 741b846

Please sign in to comment.