官术网_书友最值得收藏!

Sorted sets

In Redis, the sorted-set data type combines the characteristics of both Redis lists and sets. Similarly to those of a Redis list, a sorted set's values are ordered, and like a set, each value is assured to be unique. Of all the various data structures in Redis, the sorted set is the closest to a killer feature. The flexibility of a sorted set allows for multiple types of access patterns depending on the needs of the application. Using a single sorted set for a player's scores in a game both the top and the bottom players are easily fetched for a leaderboard by either the ZRANGE or ZREVRANGE Redis commands.

For sorted sets, the ZADD command adds a member with a score to the sorted set. The time complexity of ZADD is O(log(n)), meaning that as the size of the sorted set increases, the rate of increase in the processing time is a constant. Therefore, the difference between adding a new member to a large sorted set is trivial; the difference between log(10000) ~ 9.21034037 and log(10001) ~ 9.21044036 is .000099.

Another, very nice feature of Redis's sorted sets is that if the score is the same for all or part of the elements in a sorted set, then the values in the sorted set are ordered lexicographically, that is, by alphanumeric ordering. This characteristic can be easily exploited as an easy way to order text strings in alphabetical order. We can demonstrate this feature as follows: first we'll add seven colors to a sorted set called colors:

127.0.0.1:6379> ZADD colors 0 red 0 blue 0 green 0 orange 0 yellow 0 purple 0 pink

Now, we can use the ZRANGE command to extract the colors in alphabetical order:

127.0.0.1:6379> ZRANGE colors 0 -1 1) "blue" 2) "green" 3) "orange" 4) "pink" 5) "purple" 6) "red" 7) "yellow" 
 

With the ZREVRANGE command, we reverse retrieve our values in a reverse alphabetical order:

127.0.0.1:6379> ZREVRANGE colors 0 -1 1) "yellow" 2) "red" 3) "purple" 4) "pink" 5) "orange" 6) "green" 7) "blue" 

In either case, in the colors sorted set, all scores are set to the same value as demonstrated with a ZREVRANGE command with the keyword WITHSCORES as follows:

127.0.0.1:6379> ZREVRANGE colors 0 -1 WITHSCORES 1) "yellow" 2) "0" 3) "red" 4) "0" 5) "purple" 6) "0" 7) "pink" 8) "0" 9) "orange" 10) "0" 11) "green" 12) "0" 13) "blue" 14) "0" 
 
Sorted sets

Redis also provides specific commands to retrieve elements in a lexicographical order through the LRANGEBYLEX and LREVRANGEBYLEX commands introduced in Redis 2.8. These commands allow us to specify the start and the end of a sorted set through a special syntax. The ( character before a character string will exclude that value, while the [ will include it. Also, using a + is shorthand for all positive strings and - for all negative strings. The following commands using the previous colors sorted set can help illustrate these differences:

127.0.0.1:6379> ZRANGEBYLEX colors (b [p 1) "blue" 2) "green" 3) "orange" 127.0.0.1:6379> ZRANGEBYLEX colors - + 1) "blue" 2) "green" 3) "orange" 4) "pink" 5) "purple" 6) "red" 7) "yellow" 
主站蜘蛛池模板: 阿瓦提县| 子洲县| 通化市| 深水埗区| 特克斯县| 曲沃县| 泰兴市| 开原市| 淮阳县| 乌海市| 新竹县| 新野县| 灵丘县| 花莲县| 哈巴河县| 永靖县| 临清市| 前郭尔| 东港市| 益阳市| 镶黄旗| 龙海市| 清远市| 科技| 金华市| 济阳县| 惠州市| 浑源县| 彰武县| 龙海市| 宣城市| 广元市| 乐至县| 集安市| 昔阳县| 阿尔山市| 辰溪县| 文登市| 望谟县| 江西省| 新沂市|