-
Notifications
You must be signed in to change notification settings - Fork 24.5k
Description
Hi,
I'm making a webapp that makes intensive use of sorted sets. Many times I need to intersect subranges of sets so I use zrevrangebyscore and zinterstore. They work great. However since I just need to intersect subranges of given sets, and not complete sets, I need to first query the subranges with zrevrangebyscore and save the results in a temporary keys. Then I can use zinterstore to intersect the temporary keys. Initially I made the first step in memory in the client application. Now I'm using this Lua script:
local t = redis.call('zrangebyscore', KEYS[1], ARGV[1], ARGV[2], 'withscores')
local i=1
while(i<=#t) do
redis.call('zadd', KEYS[2], t[i+1], t[i])
i=i+2
end
return #t/2
Then I call the script: eval script 2 original_key result_key min max
So everything happens inside redis. However it would be great to have an optional STORE option in the zrangebyscore command because the execution of this script could result in thousands of zadd calls depending on the zset size. Maybe I'm missing a better way to do this, if it is the case please point me out with a better solution. For example I don't know if it's possible/recommended to use zadd with many score+member pairs inside the lua script. This is, using the variadic functionality of this command.
Thanks, and congratulations for redis :)