Creates a library.
FUNCTION
LOAD
[REPLACE
] function-code
Load a library to Valkey.
The command’s gets a single mandatory parameter which is the source
code that implements the library. The library payload must start with
Shebang statement that provides a metadata about the library (like the
engine to use and the library name). Shebang format:
#!<engine name> name=<library name>
. Currently
engine name must be lua
.
For the Lua engine, the implementation should declare one or more
entry points to the library with the server.register_function()
API. Once loaded, you can call the functions in the library with the
FCALL
(or FCALL_RO
when applicable)
command.
When attempting to load a library with a name that already exists,
the Valkey server returns an error. The REPLACE
modifier
changes this behavior and overwrites the existing library with the new
contents.
The command will return an error in the following circumstances:
REPLACE
modifier.REPLACE
is specified).For more information please refer to Introduction to Valkey Functions.
Bulk string reply: the library name that was loaded.
O(1) (considering compilation time is redundant)
@scripting @slow @write
The following example will create a library named mylib
with a single function, myfunc
, that returns the first
argument it gets.
127.0.0.1:6379> FUNCTION LOAD "#!lua name=mylib \n server.register_function('myfunc', function(keys, args) return args[1] end)"
mylib
127.0.0.1:6379> FCALL myfunc 0 hello
"hello"
EVAL, EVALSHA, EVALSHA_RO, EVAL_RO, FCALL, FCALL_RO, FUNCTION, FUNCTION DELETE, FUNCTION DUMP, FUNCTION FLUSH, FUNCTION HELP, FUNCTION KILL, FUNCTION LIST, FUNCTION RESTORE, FUNCTION STATS, SCRIPT, SCRIPT DEBUG, SCRIPT EXISTS, SCRIPT FLUSH, SCRIPT HELP, SCRIPT KILL, SCRIPT LOAD, SCRIPT SHOW.