FUNCTION LOAD
FUNCTION LOAD [REPLACE] function-code
- Group
- Scripting and functions
- Complexity
- O(N) where N is the length of the code
- Arity
- -3 (at least 3 words, counting the command name)
- HotKV Cloud
- All plans
- On-Premise
- no license needed
FUNCTION LOAD compiles a library and registers its functions. The code must
start with a metadata line naming the engine and the library, such as
#!lua name=mylib; library names may contain letters, digits and
underscores. The code then calls redis.register_function once per function,
either as redis.register_function('name', callback) or with a table giving
function_name, callback, and optionally flags and description.
The library is saved with the dataset and replicated. Loading a library whose
name is taken fails unless you pass REPLACE, which swaps the old library for
the new one in one step. No function name may be registered by two libraries.
Options
Section titled “Options”| Option | Effect |
|---|---|
REPLACE |
Replace a library of the same name instead of failing. |
Return value
Section titled “Return value”| Case | RESP2 | RESP3 |
|---|---|---|
| Loaded | Bulk string: the library name | Bulk string |
Errors:
ERR Library '<name>' already existswithoutREPLACE.ERR Function <name> already existswhen another library registers that function.ERR No functions registeredwhen the code registers no function.ERR Missing library metadata,ERR Library name was not given,ERR Engine '<engine>' not foundand similar errors for a bad first line.ERR Error compiling function: ...when the code does not compile.ERR Unknown option given: <option>for an option other thanREPLACE.
Examples
Section titled “Examples”127.0.0.1:6379> FUNCTION LOAD "#!lua name=util\nredis.register_function('echo', function(keys, args) return args[1] end)""util"127.0.0.1:6379> FUNCTION LOAD "#!lua name=util\nredis.register_function('echo', function(keys, args) return args[1] end)"(error) ERR Library 'util' already exists127.0.0.1:6379> FUNCTION LOAD REPLACE "#!lua name=util\nredis.register_function('echo', function(keys, args) return 'v2: ' .. args[1] end)""util"127.0.0.1:6379> FCALL echo 0 "hi""v2: hi"Compatibility
Section titled “Compatibility”Same as Redis 7 and Valkey 8.
See also
Section titled “See also”Was this page helpful?
Thanks for the feedback.