Sha256: ecb02d4f964f02e50130fa53c3ce7c3e23b4bf5303bdd3a33758d8d74a422ad9

Contents?: true

Size: 1.32 KB

Versions: 11

Compression:

Stored size: 1.32 KB

Contents

require 'digest'

module Redlock
  module Scripts
    UNLOCK_SCRIPT = <<-eos
      if redis.call("get",KEYS[1]) == ARGV[1] then
        return redis.call("del",KEYS[1])
      else
        return 0
      end
    eos

    # thanks to https://github.com/sbertrang/redis-distlock/blob/master/lib/Redis/DistLock.pm
    # also https://github.com/sbertrang/redis-distlock/issues/2 which proposes the value-checking
    # and @maltoe for https://github.com/leandromoreira/redlock-rb/pull/20#discussion_r38903633
    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

    # 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.
    UNLOCK_SCRIPT_SHA = Digest::SHA1.hexdigest(UNLOCK_SCRIPT)
    LOCK_SCRIPT_SHA   = Digest::SHA1.hexdigest(LOCK_SCRIPT)
    PTTL_SCRIPT_SHA   = Digest::SHA1.hexdigest(PTTL_SCRIPT)
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
redlock-2.0.6 lib/redlock/scripts.rb
redlock-2.0.5 lib/redlock/scripts.rb
redlock-2.0.4 lib/redlock/scripts.rb
redlock-2.0.3 lib/redlock/scripts.rb
redlock-2.0.2 lib/redlock/scripts.rb
redlock-2.0.1 lib/redlock/scripts.rb
redlock-2.0.0 lib/redlock/scripts.rb
redlock-1.3.2 lib/redlock/scripts.rb
redlock-1.3.1 lib/redlock/scripts.rb
redlock-1.3.0 lib/redlock/scripts.rb
redlock-1.2.2 lib/redlock/scripts.rb