reference

01-Redis Installation: how2j.cn/k/redis/red…

02- Redis Learning: redis Development and Operations (see end of article for PDF of this book)

define

The elements in the set cannot be repeated, but they can be sorted, ordered. It assigns a score to each element as a basis for sorting. Ordered collections provide queries to get a specified score and range of elements, and calculate the ranking of members.

Comparison of list, set and ordered set:

The command

1. Within a collection

(1) Add members (zadd)
127.00.1:6379> zadd key [NX|XX] [CH] [INCR] score member [score member ...] · Nx: member must not exist before it can be set successfully and used for adding. ·xx: member must exist before it can be set successfully and used for update. · CH: Returns the number of elements and fractions of the ordered set changed after this operation. · INCR: Add score, which is equivalent to zincrby.Copy the code

Single add and multiple add

127.00.1:6379> zadd user:ranking 251 tom
(integer) 1
127.00.1:6379> zrange user:ranking 0 -1
1) "tom"
127.00.1:6379> zadd user:ranking 1 kris 91 mike 200 frank 220 tim 250 martin
(integer) 5
127.00.1:6379> zrange user:ranking 0 -1
1) "kris"
2) "mike"
3) "frank"
4) "tim"
5) "martin"
6) "tom"
Copy the code
(2) Calculate the number of members (Zcard)

The time complexity of zcard is O (1)

127.00.1:6379> zcard user:ranking
(integer) 6
Copy the code
(3) Calculate a member’s score (ZScore)
127.00.1:6379> zscore user:ranking mike
"91"
Copy the code
(4) Calculate zrank

Sort starts at 0 by default.

127.00.1:6379> zrank user:ranking mike
(integer)1 127.0.0.1:6379 > zrank user: rankingkris
(integer) 0
Copy the code
(5) Deleting members (ZREM)
127.00.1:6379> zrem user:ranking tim
(integer)1 127.0.0.1:6379> Zrange user:ranking 0-1 1) "Kris" 2) "Mike" 3) "Frank" 4) "Martin"Copy the code
(6) Increase the score of members (zincrby)
127.00.1:6379> zrange user:ranking 0 -1 withscores
1) "kris"
2) "1"
3) "mike"
4) "91"
5) "frank"
6) "200"
7) "martin"
8) "250"
127.00.1:6379> zincrby user:ranking 9 mike
"100"
Copy the code
(7) Return the members of the specified ranking range (zrevRange from high to low)
127.00.1:6379> zrange user:ranking 0 -1 withscores
1) "kris"
2) "1"
3) "mike"
4) "100"
5) "frank"
6) "200"
7) "martin"
8) "250"
127.00.1:6379> zrevrange user:ranking 0 -1 withscores
1) "martin"
2) "250"
3) "frank"
4) "200"
5) "mike"
6) "100"
7) "kris"
8) "1"
Copy the code
(8) Return members of the specified score range (ZrevRangeByScore from highest to lowest)
127.00.1:6379> zrangebyscore user:ranking 100 200 withscores
1) "mike"
2) "100"
3) "frank"
4) "200"
127.00.1:6379> zrevrangebyscore user:ranking 300 10 withscores
1) "martin"
2) "250"
3) "frank"
4) "200"
5) "mike"
6) "100"
Copy the code
(9) Return the number of members in the specified score range (zcount)
127.00.1:6379> zrange user:ranking 0 -1 withscores
1) "kris"
2) "1"
3) "mike"
4) "100"
5) "frank"
6) "200"
7) "martin"
8) "250"
127.00.1:6379> zcount user:ranking 100 200
(integer) 2
Copy the code
(10) Delete the ascending element from the specified ranking (zremrangebyrank)
127.00.1:6379> zrange user:ranking 0 -1 withscores
1) "kris"
2) "1"
3) "mike"
4) "100"
5) "frank"
6) "200"
7) "martin"
8) "250"
127.00.1:6379> zremrangebyrank user:ranking 0 2
(integer) 3
127.00.1:6379> zrange user:ranking 0 -1 withscores
1) "martin"
2) "250"
Copy the code
(11) Delete members in the specified score range (zremrangebyScore)
127.00.1:6379> zrange user:ranking 0 -1 withscores
1) "tom"
2) "100"
3) "martin"
4) "250"
5) "tik"
6) "400"
7) "jkl"
8) "500"
127.00.1:6379> zremrangebyscore user:ranking 200 500
(integer) 3
127.00.1:6379> zrange user:ranking 0 -1 withscores
1) "tom"
2) "100"
Copy the code

2. Between collections

Add 2 ordered collections to Redis

127.00.1:6379> zadd user:ranking:1 1 kris 91 mike 200 frank 220 tim 250 martin 251 tom
(integer) 6
 127.00.1:6379> zrange user:ranking:1 0 -1 withscores
 1) "kris"
 2) "1"
 3) "mike"
 4) "91"
 5) "frank"
 6) "200"
 7) "tim"
 8) "220"
 9) "martin"
10) "250"
11) "tom"
12) "251"
127.00.1:6379> zadd user:ranking:2 8 james 77 mike 625 martin 888 tom
(integer) 4
127.00.1:6379> zrange user:ranking:2 0 -1 withscores
1) "james"
2) "8"
3) "mike"
4) "77"
5) "martin"
6) "625"
7) "tom"
8) "888"
Copy the code
(1) Intersection
127.00.1:6379> zinterstore destination numkeys key [key ...]  [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]Copy the code

· Destination: The intersection calculation result is saved to this key. ·numkeys: the number of keys that need to be calculated by intersection. , the key [key]… : Indicates the key for intersection calculation. 139 · weights weight/weight… : Weight of each key. When doing intersection calculation, each member in each key will multiply its score by this weight. The default weight of each key is 1. , aggregate sum | min | Max: calculated members after the intersection, scores can be in accordance with the sum (and), min (minimum), Max (maximum) do a summary, the default value is the sum.

127.00.1:6379> zinterstore s1_s2_inter 2 user:ranking:1 user:ranking:2
(integer) 3
127.00.1:6379> zrange s1_s2_inter 0 -1 withscores
1) "mike"
2) "168"
3) "martin"
4) "875"
5) "tom"
6) "1139"
Copy the code
(2) Union
127.00.1:6379> zunionstore s1_s2_union 2 user:ranking:1 user:ranking:2
(integer) 7
127.00.1:6379> zrange s1_s2_union  0 -1 withscores
 1) "kris"
 2) "1"
 3) "james"
 4) "8"
 5) "mike"
 6) "168"
 7) "frank"
 8) "200"
 9) "tim"
10) "220"
11) "martin"
12) "875"
13) "tom"
14) "1139"
Copy the code

The internal encoding

.ziplist (compressed list)

If the number of elements is less than 128 (default: 128) and the value of all elements is less than 64 bytes (default: 64 bytes), Redis uses Ziplist to reduce memory usage.

1) The number of elements is small and the elements are small,
127.00.1:6379> zadd a 1 b
(integer) 1
127.00.1:6379> object encoding a
"ziplist"
Copy the code
.skiplist

If the Ziplist condition is not met, Redis uses Ziplist because the ziplist read and write efficiency decreases.

1) If the number of elements exceeds 128, the internal code will be ziplist
127.00.1:6379> zadd zsetkey 50 e1 60 e2 30 e3 12e4 ... Ignore...84 e129
(integer) 129
127.00.1:6379> object encoding zsetkey
"skiplist"
Copy the code
2) When the number of elements is greater than 64 bytes, the internal encoding becomes hashTable

Usage scenarios

Ranking version System, which records the ranking of videos uploaded by users every day.

(1) Add user likes
127.00.1:6379> zadd user:ranking:2019 _11_26 1 mike 2 jack 3 bok 4 you 5 lkl 6 ioj 7 iui 8 jio 9 bhb 10 ahjd 11 afs
(integer) 11
Copy the code
(2) Cancel the user’s likes
127.00.1:6379> zrem user:ranking:2019 _11_26 mike
(integer) 1
Copy the code
(3) Display the top ten likes
127.00.1:6379> zrevrange user:ranking:2019 _11_26 0 9
 1) "afs"
 2) "ahjd"
 3) "bhb"
 4) "jio"
 5) "iui"
 6) "ioj"
 7) "lkl"
 8) "you"
 9) "bok"
10) "jack"
Copy the code
(4) Display user information and user scores
127.00.1:6379> zrevrange user:ranking:2019 _11_26 0 9 withscores
 1) "afs"
 2) "11"
 3) "ahjd"
 4) "10"
 5) "bhb"
 6) "9"
 7) "jio"
 8) "8"
 9) "iui"
10) "Seven"
11) "ioj"
12) "6"
13) "lkl"
14) "5"
15) "you"
16) "4"
17) "bok"
18) "3"
19) "jack"
20) "2"
Copy the code

Link: Elevenkeep and redis.