Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I store sorted data in Redis with a repeated member?

Tags:

redis

I am new to Redis and have the following problem:

Given the Sorted Set myzset: [ [1,"A"], [2, "B"], [3, "C"] ]

I want to be able to add [4, "A"] in the set.

So far if I use

ZADD myzset 4 "A"

because the member "A" is already in the set I get back [ [4,"A"], [2, "B"], [3, "C"] ]

rather than

[ [1,"A"], [2, "B"], [3, "C"], [4, "A"] ]

How can I insert data such that the set would be

[ [1,"A"], [2, "B"], [3, "C"], [4, "A"] ] ?

like image 466
Angel Avatar asked Oct 19 '25 04:10

Angel


1 Answers

Redis' Sorted Sets (and regular Sets) do not allow duplicate members. You should reconsider what you're trying to do (perhaps even edit your question to explain about the data you're storing and how you want to retrieve it) and possibly use a different approach and/or data structure.

In cases where it is necessary and makes sense to store a non-unique member in a Sorted Set you'd usually concatenate some sort of a unique identifier to the member. For example, if you're storing a timeseries (e.g. measurements from a device) you'd store the timestamp as score and id:timestamp as the member.

like image 79
Itamar Haber Avatar answered Oct 22 '25 04:10

Itamar Haber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!