Advertisement
mechanicker

Redis based distributed read/write locks

Sep 20th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. RLOCK = "redis.call('HSETNX', KEYS[1], 'mode', 'r'); local ret = redis.call('HGET', KEYS[1], 'mode'); if ret == 'r' then redis.call('EXPIRE', KEYS[1], ARGV[2]); return redis.call('HSETNX', KEYS[1], ARGV[1], 1) end"
  2. WLOCK = "local ret = redis.call('HSETNX', KEYS[1], 'mode', 'w'); if ret == 0 then return ret end; redis.call('EXPIRE', KEYS[1], ARGV[2]); return redis.call('HSETNX', KEYS[1], ARGV[1], 1)"
  3. RELEASE = "local ret = redis.call('HDEL', KEYS[1], ARGV[1]); local sz = redis.call('HLEN', KEYS[1]); if sz == 1 then redis.call('DEL', KEYS[1]) end; return ret"
  4. RENEW = "local ret = redis.call('HEXISTS', KEYS[1], ARGV[1]); if ret > 0 then ret = redis.call('EXPIRE', KEYS[1], ARGV[2]) end; return ret"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement