Created
February 24, 2012 14:12
-
-
Save rbraband/1901178 to your computer and use it in GitHub Desktop.
Howto implement stored procedures within Redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 :) |
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()"
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
Thank you :)