Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store sorted set of objects in redis?

I would like to know how to store a list of objects in Redis. That is I have a key like this.

users:pro
{ 
name: "Bruce", age: "20", score: 100,
name: "Ed", age: "22", score: 80
}

Where I will want to store a list of hashes as value of a particular key. I would like to use the score field as the score field in the sorted set. How could I accomplish this?

I have seen writing a putting a single hash for a key, but what if I want multiple hashes and one of the hash fields must act as a score field for the sorted set?

like image 291
JavaTechnical Avatar asked Dec 23 '14 06:12

JavaTechnical


People also ask

How does Redis implement sorted sets?

In Redis, sorted sets are a data type similar to sets in that both are non repeating groups of strings. The difference is that each member of a sorted set is associated with a score, allowing them to be sorted from the smallest score to the largest.

Does Redis support sorting?

Sorting in Redis is similar to sorting in other languages: we want to take a sequence of items and order them according to some comparison between elements. SORT allows us to sort LIST s, SET s, and ZSET s according to data in the LIST / SET / ZSET data stored in STRING keys, or even data stored in HASH es.

How list items are sorted in Redis?

Redis and PHP Redis Sorted Sets are similar to Redis Sets with the unique feature of values stored in a set. The difference is, every member of a Sorted Set is associated with a score, that is used in order to take the sorted set ordered, from the smallest to the greatest score.

What does Redis use to sort the elements of a sorted set?

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.


1 Answers

Using a single key to store all your hashes will require some serialization as Redis doesn't support nested data structures. The result would be the following:

key: users:pro
         |
         +-----> field       value
                 name:Bruce  "age: 20, score: 100"
                 name:Ed     "age: 22, score: 80"

> HMSET users:pro name:Bruce "age: 20, score: 100" name:Ed "age:22, score:80"

The corresponding Sorted Set would be:

key: users:pro.by_scores
         |
         +---> scores:    80           100
         +---> values: "name:Ed"   "name:Bruce"

> ZADD users:pro.by_scores 80 "name:Ed" 100 "name:Bruce"

Note 1: this approach mandates a unique ID per-user, currently the name property is used which could be problematic.

Note 2: to avoid the serialization (and deserialization), you can consider using a dedicated key per user. That means doing:

key: users:pro:Bruce
         |
         +-----> field       value
                 age         20
                 score       100

key: users:pro:Ed
         |
         +-----> field       value
                 age         22
                 score       80

> HMSET users:pro:Bruce age 20 score 100
> HMSET users:pro:Ed age 22 score 80

key: users:pro.by_scores
         |
         +---> scores:      80                100
         +---> values: "users:pro:Ed"   "users:pro:Bruce"

> ZADD users:pro.by_scores 80 "users:pro:Ed" 100 "users:pro:Bruce"
like image 122
Itamar Haber Avatar answered Nov 15 '22 10:11

Itamar Haber