Command builders for Redis Lua scripting and Redis Functions.
This module covers two related subsystems:
- Lua scripting --
EVAL,EVALSHA, andSCRIPT *commands for running ad-hoc Lua scripts on the server. - Redis Functions (Redis 7+) --
FCALL,FCALL_RO, andFUNCTION *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 -- evaluate a Lua script on the server.
EVALSHA -- evaluate a cached Lua script by its SHA1 digest.
FCALL -- invoke a Redis Function by name (Redis 7+).
FUNCTION FLUSH -- delete all function libraries (Redis 7+).
FUNCTION LOAD -- load a function library into Redis (Redis 7+).
Functions
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"]
EVALSHA -- evaluate a cached Lua script by its SHA1 digest.
Use script_load/1 first to cache the script and obtain its SHA1.
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"]
@spec function_dump() :: [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 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)
@spec function_stats() :: [String.t()]
@spec script_kill() :: [String.t()]