0
votes

I am trying to use lua scripts stored in Redis as stored procedures.

I would like to be able to store these scripts in Redis once, and look them up and evoke them when needed.

I have been able to add these functions to the :function: keyspace, using the redis-cli to add them, as follows,

redis-cli
> SET :function:f1 "redis.call('SELECT', 0);local data=redis.call('HGETALL','key:{'..ARGV[1]..'}'); print('f1'); print(ARGV[1]); return data;"
> SET :function:f2 "redis.call('SELECT', 0); local data=redis.call('HGETALL','key:{'..ARGV[1]..'}'); print('f2'); print(ARGV[1]); return data;"
> SET :function:f3 "redis.call('SELECT', 0);local data=redis.call('HGETALL','key:{'..ARGV[1]..'}'); print('f3'); print(ARGV[1]); return data;"

I have also been able to use the following script load command to build a script that can look up these commands,

SCRIPT LOAD "local f=loadstring(redis.call('get',':function:' .. KEYS[1]));return f()"

This script load command provides me with an SHA key which I can use to call one of these stored functions, which I can run from the command line, like so,

redis-cli SCRIPT LOAD "local f=loadstring(redis.call('get',':function:' .. KEYS[1]));return f()"
#returns:
"31b98f9ad6a416c27e5af91ff4af12235d4da385"

Then I can call one of the functions from redis-cli,

redis-cli
> evalsha 31b98f9ad6a416c27e5af91ff4af12235d4da385 1 f3 1234567890

But I keep getting an error,

(error) ERR Error running script (call to f_ae7d0c88e2be3f907cc9a4f5943817bc380bf68e): @user_script:1: user_script:1: bad argument #1 to 'loadstring' (string expected, got boolean)

Any ideas? suggestions?

1
See this link for attribution: gist.github.com/rbraband/1901178 - ChuckCottrill

1 Answers

1
votes

You'll have to mangle the KEYS or the redis. namespace.

Josiah Carlson just released a python package for this.

See: here and here.

Josiah also added the package to Pypi

Hope this helps, TW