I am using Redis to keep user scores sorted but I need to set all scores a given value, e.g. 100, periodically.
So far what I found is to use ZUINIONSTORE command to multiply values with a weight and this does not solve my case precisely.
Is there a proper way to accomplish this?
It does not matter to do this operation in-place or copying to another sorted set.
Example flow:
redis> ZADD zset 101 "one"
(integer) 1
redis> ZADD zset 102 "two"
(integer) 1
redis> ZADD zset 103 "three"
(integer) 1
redis> ZUNIONSTORE zset 1 zset WEIGHTS 0 // sets all scores to 0
(integer) 3
You could potentially use two calls. Maintain a separate set of scores that stay static at
100. Set the weight to zero like you do above, then call ZUNIONSTORE again with the SUM aggregation. Perhaps something like this:User set:
100 set:
Actions Start Here
Then clear the user set, and then SUM aggregate with the other:
EDIT: if the scores were guaranteed to be >100 then you could alternatively just do the following and save a call: