mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
locker
This commit is contained in:
33
locker/redis/redis_locker.lua
Normal file
33
locker/redis/redis_locker.lua
Normal file
@ -0,0 +1,33 @@
|
||||
#!lua name=charlie_locker
|
||||
|
||||
-- 安装命令
|
||||
-- cat redis_locker.lua | redis-cli -x --cluster-only-masters --cluster call 192.168.123.30:6379 FUNCTION LOAD REPLACE
|
||||
|
||||
local function lock(keys, args)
|
||||
if redis.call("GET", keys[1]) == args[1] then
|
||||
redis.call("SET", keys[1], args[1], "PX", args[2])
|
||||
return "OK"
|
||||
else
|
||||
return redis.call("SET", keys[1], args[1], "NX", "PX", args[2])
|
||||
end
|
||||
end
|
||||
|
||||
local function del(keys, args)
|
||||
if redis.call("GET", keys[1]) == args[1] then
|
||||
return redis.call("DEL", keys[1])
|
||||
else
|
||||
return '0'
|
||||
end
|
||||
end
|
||||
|
||||
local function expire(keys, args)
|
||||
if redis.call('get', keys[1]) == args[1] then
|
||||
return redis.call('expire', keys[1], args[2])
|
||||
else
|
||||
return '0'
|
||||
end
|
||||
end
|
||||
|
||||
redis.register_function('locker_lock',lock)
|
||||
redis.register_function('locker_unlock',del)
|
||||
redis.register_function('locker_expire',expire)
|
Reference in New Issue
Block a user