Redis.Commands.Function (Redis v0.8.0)

Copy Markdown View Source

Command builders for Redis Functions (Redis 7+).

Redis Functions provide a persistent, named alternative to Lua scripting. Libraries are loaded once and survive server restarts, and individual functions within a library are invoked by name with FCALL / FCALL_RO.

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

Examples

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

# Call a function
Redis.command(conn, Function.fcall("myfunc", ["key1"], ["arg1"]))

# List all libraries
Redis.command(conn, Function.list())

Summary

Functions

FUNCTION DELETE -- remove a function library by name.

FUNCTION DUMP -- return a serialized payload of all loaded libraries.

FCALL -- invoke a Redis Function by name.

FCALL_RO -- invoke a read-only Redis Function by name.

FUNCTION FLUSH -- delete all function libraries.

FUNCTION LIST -- list loaded function libraries.

FUNCTION LOAD -- load a function library into Redis.

FUNCTION RESTORE -- restore libraries from a serialized payload.

FUNCTION STATS -- return information about function execution.

Functions

delete(library_name)

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

FUNCTION DELETE -- remove a function library by name.

dump()

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

FUNCTION DUMP -- return a serialized payload of all loaded libraries.

The result can be restored on another server with restore/2.

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

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

FCALL -- invoke a Redis Function by name.

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

Function.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()]

FCALL_RO -- invoke a read-only Redis Function by name.

Identical to fcall/3 but uses the read-only variant, which is safe for use on replicas.

flush(opts \\ [])

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

FUNCTION FLUSH -- delete all function libraries.

Options

  • :mode - :async or :sync (default: server decides)

list(opts \\ [])

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

FUNCTION LIST -- list loaded function libraries.

Options

  • :libraryname - filter by library name pattern
  • :withcode - include library source code in the response

load(function_code, opts \\ [])

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

FUNCTION LOAD -- load a function library into Redis.

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

Function.load("#!lua name=mylib\nredis.register_function(...)")
Function.load(code, replace: true)

restore(serialized_value, opts \\ [])

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

FUNCTION RESTORE -- restore libraries from a serialized payload.

Options

  • :flush - delete all existing libraries before restoring
  • :append - append to existing libraries (error on name conflict)
  • :replace - replace existing libraries with same name

stats()

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

FUNCTION STATS -- return information about function execution.