High-level interface for Redis Functions (Redis 7+).
Redis Functions are persistent, named server-side routines that replace ad-hoc Lua scripting for production workloads. Libraries are loaded once and survive server restarts; individual functions within a library are invoked by name.
Usage
# Load a library
code = """
#!lua name=mylib
redis.register_function('myfunc', function(keys, args)
return redis.call('GET', keys[1])
end)
"""
:ok = Redis.Function.load(conn, code)
# Call the function
{:ok, result} = Redis.Function.call(conn, "myfunc", keys: ["mykey"])
# Read-only variant (safe on replicas)
{:ok, result} = Redis.Function.call_ro(conn, "myfunc", keys: ["mykey"])
# List loaded libraries
{:ok, libs} = Redis.Function.list(conn)
# Clean up
:ok = Redis.Function.delete(conn, "mylib")See also
Redis.Script for ad-hoc Lua scripting with SHA1-based caching.
Summary
Functions
Calls a function by name via FCALL.
Calls a function by name via FCALL_RO (read-only variant).
Deletes a function library by name.
Returns a serialized payload of all loaded function libraries.
Deletes all function libraries.
Lists loaded function libraries.
Loads a function library into Redis.
Restores function libraries from a serialized payload produced by dump/1.
Returns information about function execution statistics.
Functions
@spec call(GenServer.server(), String.t(), keyword()) :: {:ok, term()} | {:error, term()}
Calls a function by name via FCALL.
Options
:keys- list of Redis keys (default: []):args- list of additional arguments (default: [])
@spec call_ro(GenServer.server(), String.t(), keyword()) :: {:ok, term()} | {:error, term()}
Calls a function by name via FCALL_RO (read-only variant).
Safe for use on replicas. Accepts the same options as call/3.
@spec delete(GenServer.server(), String.t()) :: :ok | {:error, term()}
Deletes a function library by name.
Returns :ok on success or {:error, reason} on failure.
@spec dump(GenServer.server()) :: {:ok, binary()} | {:error, term()}
Returns a serialized payload of all loaded function libraries.
The result can be restored on another server with restore/3.
@spec flush( GenServer.server(), keyword() ) :: :ok | {:error, term()}
Deletes all function libraries.
Options
:mode-:asyncor:sync(default: server decides)
@spec list( GenServer.server(), keyword() ) :: {:ok, term()} | {:error, term()}
Lists loaded function libraries.
Options
:libraryname- filter by library name pattern:withcode- include library source code in the response (default: false)
@spec load(GenServer.server(), String.t(), keyword()) :: :ok | {:error, term()}
Loads a function library into Redis.
The code must include a shebang header declaring the engine and library
name, e.g. #!lua name=mylib.
Options
:replace- overwrite an existing library with the same name (default: false)
Returns :ok on success or {:error, reason} on failure.
@spec restore(GenServer.server(), binary(), keyword()) :: :ok | {:error, term()}
Restores function libraries from a serialized payload produced by dump/1.
Options
:flush- delete all existing libraries before restoring:append- append to existing libraries (error on name conflict):replace- replace existing libraries with same name
@spec stats(GenServer.server()) :: {:ok, term()} | {:error, term()}
Returns information about function execution statistics.