Skip to content

Instantly share code, notes, and snippets.

@rbraband
Created February 24, 2012 14:12
Show Gist options
  • Save rbraband/1901178 to your computer and use it in GitHub Desktop.
Save rbraband/1901178 to your computer and use it in GitHub Desktop.
Howto implement stored procedures within Redis
# Step 0 -- create test data
redis-cli HSET :object:30343552:data foo bar
# Step 1 -- store sample function 'sampleFunction'
redis-cli SET :functions:sample "redis.call('SELECT', 0);local data=redis.call('HGETALL',':object:' .. ARGV[1] .. ':data');return data"
# Step 2 -- create function loader
redis-cli SCRIPT LOAD "f=loadstring(redis.call('get',':functions:' .. KEYS[1]));return f()"
# Step 3 -- test
redis-cli EVALSHA 7b951bb1cb58cb9de1dee3cb6f79fb089911ff8f 1 sampleFunction 30343552
# have fun :)
Copy link

ghost commented May 22, 2012

Thank you :)

@jcbooth
Copy link

jcbooth commented Jun 14, 2012

This is awesome - thanks! BTW, if anyone reading this has a similar problem than me, step 2 was choking on the global var. I had to scope it as a local var -> SCRIPT LOAD "local f=loadstring(redis.call('get',':functions:' .. KEYS[1]));return f()"

@rpeleg1970
Copy link

Thanks for this!
Another minor correction - the sample function above is stored as 'sample' and not 'sampleFunction', so the test call will be:
redis-cli EVALSHA 7b951bb1cb58cb9de1dee3cb6f79fb089911ff8f 1 sample 30343552

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment