Skip to main content
Redis functions act as a superset of Lua scripts. Instead of sending the script content with every request (like EVAL), you load the script once as a “library” and call the functions it registers.
Currently, LUA is the only supported engine.
The library source code must start with the #!lua name=<library_name> shebang. Inside the script, use redis.register_function to expose your functions. The registered functions can be called with the FCALL command.

Arguments

options
Object
required
The load options.

Response

The name of the loaded library.
const code = `#!lua name=mylib
  
  -- Simple function that returns a string
  redis.register_function(
    'helloworld',
    function() return 'Hello World!' end
  )

  -- Complex function that modifies data with logic
  local function my_hset(keys, args)
    local hash = keys[1]
    local time = redis.call('TIME')[1]
    return redis.call('HSET', hash, '_last_modified_', time, unpack(args))
  end

  redis.register_function('my_hset', my_hset)
`;

const libraryName = await redis.functions.load({ code, replace: true });

console.log(libraryName); // "mylib"