English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Redis Sorted Sets (sorted set)

Redis有序集合和集合一样也是string类型元素的集合,且不允许重复的成员。

The difference is that each element is associated with a double type of score. Redis sorts the members in the set from small to large through scores.

The members of the ordered set are unique, but the score (score) can be repeated.

The collection is implemented by hash table, so the complexity of adding, deleting, and searching is O(1) 232 - 1 (4294967295, each collection can store40 to 1 billion members).

Online examples

redis 127.0.0.1:6379>  ZADD w3codeboxkey 1 redis
(integer) 1
redis 127.0.0.1:6379>  ZADD w3codeboxkey 2 mongodb
(integer) 1
redis 127.0.0.1:6379>  ZADD w3codeboxkey 3 mysql
(integer) 1
redis 127.0.0.1:6379>  ZADD w3codeboxkey 3 mysql
(integer) 0
redis 127.0.0.1:6379>  ZADD w3codeboxkey 4 mysql
(integer) 0
redis 127.0.0.1:6379>  ZRANGE w3codeboxkey 0 10 WITHSCORES
1)  "redis"
2) "1"
3)  "mongodb"
4) "2"
5)  "mysql"
6) "4"

In the above example, we use the command ZADD Three values were added to the redis ordered set and associated with scores.

Redis ordered set commands

The following table lists the basic commands of redis ordered set:

Serial numberCommand and description
1ZADD key score1 member1 [score2 member2]
Adds one or more members to the ordered set, or updates the score of existing members
2ZCARD key
Gives the number of members in the ordered set
3ZCOUNT key min max
Calculates the number of members in the specified score range in the ordered set
4ZINCRBY key increment member
Increases the score of the specified member in the ordered set by the increment
5ZINTERSTORE destination numkeys key [key ...]
Calculates the intersection of one or more ordered sets and stores the result set in a new ordered set destination
6ZLEXCOUNT key min max
Calculates the number of members in the specified dictionary range in the ordered set
7ZRANGE key start stop [WITHSCORES]
Returns members in the specified range by index in the ordered set
8ZRANGEBYLEX key min max [LIMIT offset count]
Returns members in the ordered set by dictionary range
9ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT]
Returns members in the specified range by score in the ordered set
10ZRANK key member
Returns the index of the specified member in the ordered set
11ZREM key member [member ...]
Remove one or more members from the sorted set
12ZREMRANGEBYLEX key min max
Remove all members in the specified dictionary range from the sorted set
13ZREMRANGEBYRANK key start stop
Remove all members in the specified rank range from the sorted set
14ZREMRANGEBYSCORE key min max
Remove all members in the specified score range from the sorted set
15ZREVRANGE key start stop [WITHSCORES]
Return members in the specified range in the sorted set, by index, sorted from high to low
16ZREVRANGEBYSCORE key max min [WITHSCORES]
Return members in the specified score range in the sorted set, sorted from high to low
17ZREVRANK key member
Return the rank of the specified member in the sorted set, sorted by score in descending order (from largest to smallest)
18ZSCORE key member
Return the score of the member in the sorted set
19ZUNIONSTORE destination numkeys key [key ...]
Compute the union of one or more sorted sets and store the result in a new key
20ZSCAN key cursor [MATCH pattern] [COUNT count]
Iterate over elements in a sorted set (including element members and element scores)