Module: Redlock::Scripts

Included in:
Redlock
Defined in:
lib/redlock/scripts.rb

Constant Summary collapse

UNLOCK_SCRIPT =
<<-eos
  if redis.call("get",KEYS[1]) == ARGV[1] then
    return redis.call("del",KEYS[1])
  else
    return 0
  end
eos
LOCK_SCRIPT =
<<-eos
  if (redis.call("exists", KEYS[1]) == 0 and ARGV[3] == "yes") or redis.call("get", KEYS[1]) == ARGV[1] then
    return redis.call("set", KEYS[1], ARGV[1], "PX", ARGV[2])
  end
eos
PTTL_SCRIPT =
<<-eos
  return { redis.call("get", KEYS[1]), redis.call("pttl", KEYS[1]) }
eos
UNLOCK_SCRIPT_SHA =

We do not want to load the scripts on every Redlock::Client initialization. Hence, we rely on Redis handing out SHA1 hashes of the cached scripts and pre-calculate them instead of loading the scripts unconditionally. If the scripts have not been cached on Redis, ‘recover_from_script_flush` has our backs.

Digest::SHA1.hexdigest(UNLOCK_SCRIPT)
LOCK_SCRIPT_SHA =
Digest::SHA1.hexdigest(LOCK_SCRIPT)
PTTL_SCRIPT_SHA =
Digest::SHA1.hexdigest(PTTL_SCRIPT)