Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to duplicate a Redis Sorted Set?

Tags:

redis

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.

like image 642
Pete Avatar asked Feb 14 '12 19:02

Pete


People also ask

How does Redis sorted sets work?

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.

Can members be added to a sorted set with the same score?

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.

Is Redis sorted?

Redis lists are lists of strings sorted by insertion order.

What is ZADD in Redis?

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.


1 Answers

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"
like image 182
Linus Thiel Avatar answered Nov 16 '22 02:11

Linus Thiel