Module: Redlock::Scripts

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

Constant Summary collapse

UNLOCK_SCRIPT =
"  if redis.call(\"get\",KEYS[1]) == ARGV[1] then\n    return redis.call(\"del\",KEYS[1])\n  else\n    return 0\n  end\n"
LOCK_SCRIPT =
"  if (redis.call(\"exists\", KEYS[1]) == 0 and ARGV[3] == \"yes\") or redis.call(\"get\", KEYS[1]) == ARGV[1] then\n    return redis.call(\"set\", KEYS[1], ARGV[1], \"PX\", ARGV[2])\n  end\n"
PTTL_SCRIPT =
"  return { redis.call(\"get\", KEYS[1]), redis.call(\"pttl\", KEYS[1]) }\n"
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)