Alice52 / database

ddf13ad8d4be76a80a336418b5cf5727bf6e3059
gitee.com
MIT License
0 stars 0 forks source link

[redis] redission lock #57

Closed Alice52 closed 2 years ago

Alice52 commented 2 years ago

redission lock lua

------- LOCK 30s --------------- 返回 Boolean
-- keys: this.getName()
-- values: this.internalLockLeaseTime, this.getLockName(threadId)
-- hash <lockname, <threadid, count>>
if (redis.call('exists', KEYS[1]) == 0) then 
  redis.call('hincrby', KEYS[1], ARGV[2], 1); 
  redis.call('pexpire', KEYS[1], ARGV[1]); 
  return nil; 
end; 

if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then 
  redis.call('hincrby', KEYS[1], ARGV[2], 1); 
  redis.call('pexpire', KEYS[1], ARGV[1]); 
  return nil; 
end; 

return redis.call('pttl', KEYS[1]);

-------- RENEW 10s -------------------------------
-- keys: this.getName()
-- vaules: this.internalLockLeaseTime, this.getLockName(threadId)
if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then 
  redis.call('pexpire', KEYS[1], ARGV[1]); 
  return 1; 
end; 
return 0;

-------- UNLOCK ------------------------------- 返回 Long: 0 可重入次数减少一次 1解锁成功 nil 其他线程 解锁失败
-- keys: Arrays.asList(this.getName(), this.getChannelName()),
-- vaules: LockPubSub.UNLOCK_MESSAGE, this.internalLockLeaseTime, this.getLockName(threadId)
if (redis.call('hexists', KEYS[1], ARGV[3]) == 0) then 
  return nil;
end;
local counter = redis.call('hincrby', KEYS[1], ARGV[3], -1); 
if (counter > 0) then 
  redis.call('pexpire', KEYS[1], ARGV[2]); 
  return 0; 
else 
  redis.call('del', KEYS[1]); 
  redis.call('publish', KEYS[2], ARGV[1]);
  return 1; 
end;
return nil; 
Alice52 commented 2 years ago

image