Redis Functions

createFunctionLibrary(redis, options) wraps Redis 7’s Functions feature so you can register Lua libraries on connect and call them like ordinary methods. Functions are stored on the server (no EVALSHA cache to manage) and survive Redis restarts.

Requires Redis 7+. The wrapper runs INFO server on activate and throws on Redis older than 7. There is no EVAL or EVALSHA fallback. On Redis 6, use redis.eval directly.

Setup

import { createRedisClient } from 'svelte-adapter-uws-extensions/redis';
import { createFunctionLibrary } from 'svelte-adapter-uws-extensions/redis/functions';

const redis = createRedisClient();

const fns = await createFunctionLibrary(redis, {
  name: 'myapp',
  source: `#!lua name=myapp
    redis.register_function('hash_inc', function(keys, args)
      return redis.call('HINCRBY', keys[1], args[1], tonumber(args[2]))
    end)`
});

The library is loaded on activate and replaced if its source has changed (atomic via FUNCTION LOAD REPLACE).

Usage

const newCount = await fns.call('hash_inc', { keys: ['counter:42'], args: ['hits', 1] });

API

MethodDescription
call(name, { keys, args })Invoke a registered function. Returns the function’s return value.
replace(source)Hot-reload the library source.
unload()FUNCTION DELETE. Use for tests / teardown.

See also

Was this page helpful?