Re: redis scripting - iterating through arguments

3,253 views
Skip to first unread message

nghia pham

unread,
Jun 19, 2012, 4:50:23 AM6/19/12
to redi...@googlegroups.com
It turns out that this is not possible if redis.call not support varargs/

Vào 02:37:39 UTC+7 Thứ ba, ngày 19 tháng sáu năm 2012, nghia pham đã viết:
Hi,

Is there anyway to iterate through the arguments (ARGV) pass to redis ? My case is to use it with some redis command that support multiple argument like SADD

Thanks.

Dvir Volk

unread,
Jun 19, 2012, 4:51:47 AM6/19/12
to redi...@googlegroups.com
this might be really useful though.

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To view this discussion on the web visit https://groups.google.com/d/msg/redis-db/-/-C3UnCsprSYJ.

To post to this group, send email to redi...@googlegroups.com.
To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.

Message has been deleted

Chris Love

unread,
Jun 19, 2012, 11:22:59 AM6/19/12
to redi...@googlegroups.com
If you do a type(ARGV) it is a table

This is a simple script to show how to iterate through ARGV

local s = {}
for i=1, #ARGV do // #ARGV is the size of the table
  s[i] = 'Processed' .. i  .. ' ' .. ARGV[i]
end
return cjson.encode(s)

You can test it by doing:

redis-cli --eval test_lua.lau key1 key2 ,  first second

Then do 
redis-cli --eval test_lua.lau key1 key2 ,  first second third fourth

Thanks

Chris

Daniele Alessandri

unread,
Jun 19, 2012, 11:45:57 AM6/19/12
to redi...@googlegroups.com
This is more of a trick but Lua's unpack() function, albeit with certain restrictions, can be useful with variadic commands. You can see http://pgl.yoyo.org/luai/i/unpack for reference, but here is a plain stupid example of a Lua script for Redis:

redis.call('sadd', KEYS[1], unpack(ARGV));

That said, unpack() actually has a limit on the size of the table which is defined by LUAI_MAXCSTACK in luaconf.h forcing a maximum number of Lua stack slots that a C function can use. This limit is set to 8000 by default, meaning that your ARGV table can contain up to 8000 elements (it's more than reasonable in this context).
 
--
Daniele Alessandri
http://clorophilla.net/
http://twitter.com/JoL1hAHN

Chris Love

unread,
Jun 19, 2012, 12:22:31 PM6/19/12
to redi...@googlegroups.com
Since we are talking stupid redis lua tricks.  We pass in a lot of our data in as JSON, and then decode it and handle it inside of redis.  Since cjson is part of redis, it makes it efficient and easy!

Good catch on the unpack :)

Chris

nghia pham

unread,
Jun 19, 2012, 3:01:46 PM6/19/12
to redi...@googlegroups.com
unpack solved my issue.

Thank you!
Reply all
Reply to author
Forward
0 new messages