I'm working with two sorted sets in Redis. At a certain point i need to expire set 1
, rename set 2
to set 1
. This removes set 2
. I then need to duplicate set 1
to recreate set 2
as a replica. Is this possible?
I've spotted zunionstore in the Redis documentation but i'm not sure it's what i'm after.
A Redis sorted set is a collection of unique strings (members) ordered by an associated score. When more than one string has the same score, the strings are ordered lexicographically. Some use cases for sorted sets include: Leaderboards.
While the same element can't be repeated in a sorted set since every element is unique, it is possible to add multiple different elements having the same score.
Redis lists are lists of strings sorted by insertion order.
Redis ZADD command is used to add all the specified members with the specified scores to the sorted set stored at key. If a specified member is an existing member of the stored set, the score is updated and the element reinserted at the right position to ensure the correct ordering.
Indeed, zunionstore
seems to work fine. It does a union between the specified sets, and stores the result. If you only supply one set, it will make a copy. At least, I just tried it out and it seems to work:
zadd foo 1 a
zadd foo 2 b
zunionstore bar 1 foo
zrange bar 0 -1
1) "a"
2) "b"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With