123456789_123456789_123456789_123456789_123456789_

Module: Redis::Commands::Scripting

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/redis/commands/scripting.rb

Instance Method Summary

Instance Method Details

#_eval(cmd, args) (private)

[ GitHub ]

  
# File 'lib/redis/commands/scripting.rb', line 211

def _eval(cmd, args)
  script = args.shift
  options = args.pop if args.last.is_a?(Hash)
  options ||= {}

  keys = args.shift || options[:keys] || []
  argv = args.shift || options[:argv] || []

  send_command([cmd, script, keys.length] + keys + argv)
end

#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:

[ GitHub ]

  
# 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:

[ GitHub ]

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

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

#fcall(*args) ⇒ Object

Invoke a ::Redis Function loaded with FUNCTION LOAD.

Examples:

FCALL without keys nor arguments

redis.fcall("myfunc")
  # => <depends on the function>

FCALL with keys and arguments as array arguments

redis.fcall("myfunc", ["k1", "k2"], ["a1", "a2"])
  # => <depends on the function>

FCALL with keys and arguments in a hash argument

redis.fcall("myfunc", :keys => ["k1", "k2"], :argv => ["a1", "a2"])
  # => <depends on the function>

Parameters:

  • keys (Array<String>)

    optional array with keys to pass to the function

  • argv (Array<String>)

    optional array with arguments to pass to the function

  • options (Hash)
    • :keys => Array<String>: optional array with keys to pass to the function
    • :argv => Array<String>: optional array with arguments to pass to the function

Returns:

  • depends on the function

See Also:

[ GitHub ]

  
# File 'lib/redis/commands/scripting.rb', line 184

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

#fcall_ro(*args) ⇒ Object

Invoke a read-only ::Redis Function loaded with FUNCTION LOAD.

The function must have been registered with the no-writes flag.

Examples:

FCALL_RO with a key

redis.fcall_ro("myfunc", ["k1"])
  # => <depends on the function>

Parameters:

  • keys (Array<String>)

    optional array with keys to pass to the function

  • argv (Array<String>)

    optional array with arguments to pass to the function

  • options (Hash)
    • :keys => Array<String>: optional array with keys to pass to the function
    • :argv => Array<String>: optional array with arguments to pass to the function

Returns:

  • depends on the function

See Also:

[ GitHub ]

  
# File 'lib/redis/commands/scripting.rb', line 205

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

#function(subcommand, *args, **options) ⇒ String, ...

Manage Redis Functions libraries.

Examples:

Load a library

redis.function(:load, "#!lua name=mylib\n...")
  # => "mylib"

Replace an existing library

redis.function(:load, code, replace: true)
  # => "mylib"

List loaded libraries

redis.function(:list)
  # => [{ "library_name" => "mylib", "engine" => "LUA", "functions" => [...] }]

List one library including its source code

redis.function(:list, libraryname: "mylib", withcode: true)

Delete a library

redis.function(:delete, "mylib")
  # => "OK"

Dump and restore all libraries

payload = redis.function(:dump)
redis.function(:restore, payload, policy: :replace)
  # => "OK"

Get engine and running-function statistics

redis.function(:stats)
  # => { "running_script" => nil,
  #      "engines" => { "LUA" => { "libraries_count" => 1, "functions_count" => 2 } } }

Kill the currently running function

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

Parameters:

  • subcommand (String, Symbol)

    e.g. load, delete, flush, list, dump, restore, stats, kill

  • args (Array<String>)

    depends on subcommand

  • options (Hash)
    • :replace => true: (load) overwrite an existing library of the same name
    • :libraryname => String: (list) only list libraries matching this pattern
    • :withcode => true: (list) include each library's source code in the reply
    • :policy => Symbol: (restore) one of :append (default), :flush, :replace

Returns:

  • (String, Array<Hash>, Hash)

    depends on subcommand

See Also:

[ GitHub ]

  
# File 'lib/redis/commands/scripting.rb', line 139

def function(subcommand, *args, **options)
  subcommand = subcommand.to_s.downcase

  case subcommand
  when "load"
    command = %i[function load]
    command << "REPLACE" if options[:replace]
    send_command(command + args)
  when "list"
    command = %i[function list] + args
    command << "LIBRARYNAME" << options[:libraryname] if options[:libraryname]
    command << "WITHCODE" if options[:withcode]
    send_command(command, &HashifyFunctionList)
  when "restore"
    command = %i[function restore] + args
    command << options[:policy].to_s.upcase if options[:policy]
    send_command(command)
  when "stats"
    send_command(%i[function stats], &HashifyFunctionStats)
  else
    send_command([:function, subcommand] + args)
  end
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:

[ GitHub ]

  
# 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