Module: Redis::Commands::Scripting

Included in:
Redis::Commands
Defined in:
lib/redis/commands/scripting.rb

Instance Method Summary collapse

Instance Method Details

#eval(*args) ⇒ Object

Evaluate Lua script.

Examples:

EVAL without KEYS nor ARGV

redis.eval("return 1")
  # => 1

EVAL with KEYS and ARGV as array arguments

redis.eval("return { KEYS, ARGV }", ["k1", "k2"], ["a1", "a2"])
  # => [["k1", "k2"], ["a1", "a2"]]

EVAL with KEYS and ARGV in a hash argument

redis.eval("return { KEYS, ARGV }", :keys => ["k1", "k2"], :argv => ["a1", "a2"])
  # => [["k1", "k2"], ["a1", "a2"]]

Parameters:

  • keys (Array<String>)

    optional array with keys to pass to the script

  • argv (Array<String>)

    optional array with arguments to pass to the script

  • options (Hash)
    • ‘:keys => Array<String>`: optional array with keys to pass to the script

    • ‘:argv => Array<String>`: optional array with arguments to pass to the script

Returns:

  • depends on the script

See Also:



71
72
73
# File 'lib/redis/commands/scripting.rb', line 71

def eval(*args)
  _eval(:eval, args)
end

#evalsha(*args) ⇒ Object

Evaluate Lua script by its SHA.

Examples:

EVALSHA without KEYS nor ARGV

redis.evalsha(sha)
  # => <depends on script>

EVALSHA with KEYS and ARGV as array arguments

redis.evalsha(sha, ["k1", "k2"], ["a1", "a2"])
  # => <depends on script>

EVALSHA with KEYS and ARGV in a hash argument

redis.evalsha(sha, :keys => ["k1", "k2"], :argv => ["a1", "a2"])
  # => <depends on script>

Parameters:

  • keys (Array<String>)

    optional array with keys to pass to the script

  • argv (Array<String>)

    optional array with arguments to pass to the script

  • options (Hash)
    • ‘:keys => Array<String>`: optional array with keys to pass to the script

    • ‘:argv => Array<String>`: optional array with arguments to pass to the script

Returns:

  • depends on the script

See Also:



96
97
98
# File 'lib/redis/commands/scripting.rb', line 96

def evalsha(*args)
  _eval(:evalsha, args)
end

#script(subcommand, *args) ⇒ String, ...

Control remote script registry.

Examples:

Load a script

sha = redis.script(:load, "return 1")
  # => <sha of this script>

Check if a script exists

redis.script(:exists, sha)
  # => true

Check if multiple scripts exist

redis.script(:exists, [sha, other_sha])
  # => [true, false]

Flush the script registry

redis.script(:flush)
  # => "OK"

Kill a running script

redis.script(:kill)
  # => "OK"

Parameters:

  • subcommand (String)

    e.g. ‘exists`, `flush`, `load`, `kill`

  • args (Array<String>)

    depends on subcommand

Returns:

  • (String, Boolean, Array<Boolean>, ...)

    depends on subcommand

See Also:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/redis/commands/scripting.rb', line 30

def script(subcommand, *args)
  subcommand = subcommand.to_s.downcase

  if subcommand == "exists"
    arg = args.first

    send_command([:script, :exists, arg]) do |reply|
      reply = reply.map { |r| Boolify.call(r) }

      if arg.is_a?(Array)
        reply
      else
        reply.first
      end
    end
  else
    send_command([:script, subcommand] + args)
  end
end