Reputation: 1199
Is there a way to increment all scores of a zset with one command? This is what I would like to do:
ZADD myzset 1 "first"
ZADD myzset 2 "second"
ZINCRBY myzset 2 "*"
ZRANGE myzset 0 -1 WITHSCORES
1) "first"
2) "3"
3) "second"
4) "4"
but, ZINCRBY myzset 2 "*"
does not work like that.
Upvotes: 2
Views: 2493
Reputation: 4687
If you need do this task often Or this is a piece code of your app server, maybe you could use the pipeline to speedup the task. if your zset size if N, then there will be N RTTs between your client and redis server. If you use the pipeline ,just nedd one RTT which will save network transport time. more about this, refer this document. And the algorithm is same as @Liviu Costea.
Upvotes: 0
Reputation: 3794
There is no command for that, ZINCRBY can only increment for 1 member at a time. So if you want to accomplish this in an atomic and fast way you need to do a Lua script. Which should look something like this (first you get all members of the sorted set and then iterate through them and increment the score):
local zsetMembers = redis.call('zrange', KEYS[1], '0', '-1')
for k,member in pairs(zsetMembers) do
redis.call('zincrby', KEYS[1], 1, member)
end
And you can push this script to Redis with the EVAL
command.
Upvotes: 10