Redis.Commands.Script (Redis v0.8.0)

Copy Markdown View Source

Command builders for Redis Lua scripting and Redis Functions.

This module covers two related subsystems:

  • Lua scripting -- EVAL, EVALSHA, and SCRIPT * commands for running ad-hoc Lua scripts on the server.
  • Redis Functions (Redis 7+) -- FCALL, FCALL_RO, and FUNCTION * commands for managing and invoking persistent, named functions.

All functions are pure and return a command list for use with Redis.command/2 or Redis.pipeline/2.

Examples

# Evaluate a Lua script that increments a key by a given amount
Redis.command(conn, Script.eval("return redis.call('INCRBY', KEYS[1], ARGV[1])", ["counter"], ["5"]))

# Call a previously loaded function
Redis.command(conn, Script.fcall("myfunc", ["key1"], ["arg1", "arg2"]))

# Load a function library (Redis 7+)
Redis.command(conn, Script.function_load("#!lua name=mylib\nredis.register_function('myfunc', function(keys, args) return keys[1] end)"))

Summary

Functions

eval(script, keys \\ [], args \\ [])

@spec eval(String.t(), [String.t()], [String.t()]) :: [String.t()]

EVAL -- evaluate a Lua script on the server.

The keys list is passed as KEYS and args as ARGV inside the script. The number of keys is computed automatically.

Script.eval("return redis.call('SET', KEYS[1], ARGV[1])", ["mykey"], ["myval"])
#=> ["EVAL", "return redis.call('SET', KEYS[1], ARGV[1])", "1", "mykey", "myval"]

eval_ro(script, keys \\ [], args \\ [])

@spec eval_ro(String.t(), [String.t()], [String.t()]) :: [String.t()]

evalsha(sha1, keys \\ [], args \\ [])

@spec evalsha(String.t(), [String.t()], [String.t()]) :: [String.t()]

EVALSHA -- evaluate a cached Lua script by its SHA1 digest.

Use script_load/1 first to cache the script and obtain its SHA1.

evalsha_ro(sha1, keys \\ [], args \\ [])

@spec evalsha_ro(String.t(), [String.t()], [String.t()]) :: [String.t()]

fcall(function, keys \\ [], args \\ [])

@spec fcall(String.t(), [String.t()], [String.t()]) :: [String.t()]

FCALL -- invoke a Redis Function by name (Redis 7+).

Like eval/3, the keys and args lists map to KEYS and ARGV inside the function body.

Script.fcall("myfunc", ["key1"], ["arg1"])
#=> ["FCALL", "myfunc", "1", "key1", "arg1"]

fcall_ro(function, keys \\ [], args \\ [])

@spec fcall_ro(String.t(), [String.t()], [String.t()]) :: [String.t()]

function_delete(library_name)

@spec function_delete(String.t()) :: [String.t()]

function_dump()

@spec function_dump() :: [String.t()]

function_flush(opts \\ [])

@spec function_flush(keyword()) :: [String.t()]

FUNCTION FLUSH -- delete all function libraries (Redis 7+).

Pass mode: :async or mode: :sync to control flush behaviour.

Script.function_flush()
#=> ["FUNCTION", "FLUSH"]

Script.function_flush(mode: :async)
#=> ["FUNCTION", "FLUSH", "ASYNC"]

function_list(opts \\ [])

@spec function_list(keyword()) :: [String.t()]

function_load(function_code, opts \\ [])

@spec function_load(
  String.t(),
  keyword()
) :: [String.t()]

FUNCTION LOAD -- load a function library into Redis (Redis 7+).

Pass replace: true to overwrite an existing library with the same name.

Script.function_load("#!lua name=mylib\nredis.register_function('myfunc', function(keys, args) return 'ok' end)")
Script.function_load(code, replace: true)

function_restore(serialized_value, opts \\ [])

@spec function_restore(
  String.t(),
  keyword()
) :: [String.t()]

function_stats()

@spec function_stats() :: [String.t()]

script_exists(sha1s)

@spec script_exists([String.t()]) :: [String.t()]

script_flush(opts \\ [])

@spec script_flush(keyword()) :: [String.t()]

script_kill()

@spec script_kill() :: [String.t()]

script_load(script)

@spec script_load(String.t()) :: [String.t()]