01-redis安裝:how2j.cn/k/redis/red…html
02-redis學習:<Redis開發與運維>(獲取本書PDF見文章末)java
集合中的元素不能重複,可是能夠排序,是有序的。它給每一個元素設置一個分數(score)做爲排序的依據,有序集合提供了獲取指定分數和元素範圍查詢,計算成員排名。redis
列表、集合、有序集合三者對比:運維
127.0.0.1:6379> zadd key [NX|XX] [CH] [INCR] score member [score member ...]
·nx:member必須不存在,才能夠設置成功,用於添加。
·xx:member必須存在,才能夠設置成功,用於更新。
·ch:返回這次操做後,有序集合元素和分數發生變化的個數。
·incr:對score作增長,至關於後面介紹的zincrby。
複製代碼
單個添加和多個添加學習
127.0.0.1:6379> zadd user:ranking 251 tom
(integer) 1
127.0.0.1:6379> zrange user:ranking 0 -1
1) "tom"
127.0.0.1:6379> zadd user:ranking 1 kris 91 mike 200 frank 220 tim 250 martin
(integer) 5
127.0.0.1:6379> zrange user:ranking 0 -1
1) "kris"
2) "mike"
3) "frank"
4) "tim"
5) "martin"
6) "tom"
複製代碼
zcard的時間複雜度爲O(1)ui
127.0.0.1:6379> zcard user:ranking
(integer) 6
複製代碼
127.0.0.1:6379> zscore user:ranking mike
"91"
複製代碼
排序默認是從0開始。編碼
127.0.0.1:6379> zrank user:ranking mike (integer) 1 127.0.0.1:6379> zrank user:ranking kris (integer) 0 複製代碼
127.0.0.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" 複製代碼
127.0.0.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.0.0.1:6379> zincrby user:ranking 9 mike
"100"
複製代碼
127.0.0.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.0.0.1:6379> zrevrange user:ranking 0 -1 withscores
1) "martin"
2) "250"
3) "frank"
4) "200"
5) "mike"
6) "100"
7) "kris"
8) "1"
複製代碼
127.0.0.1:6379> zrangebyscore user:ranking 100 200 withscores
1) "mike"
2) "100"
3) "frank"
4) "200"
127.0.0.1:6379> zrevrangebyscore user:ranking 300 10 withscores
1) "martin"
2) "250"
3) "frank"
4) "200"
5) "mike"
6) "100"
複製代碼
127.0.0.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.0.0.1:6379> zcount user:ranking 100 200
(integer) 2
複製代碼
127.0.0.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.0.0.1:6379> zremrangebyrank user:ranking 0 2
(integer) 3
127.0.0.1:6379> zrange user:ranking 0 -1 withscores
1) "martin"
2) "250"
複製代碼
127.0.0.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.0.0.1:6379> zremrangebyscore user:ranking 200 500
(integer) 3
127.0.0.1:6379> zrange user:ranking 0 -1 withscores
1) "tom"
2) "100"
複製代碼
添加2個有序集合到redis中spa
127.0.0.1:6379> zadd user:ranking:1 1 kris 91 mike 200 frank 220 tim 250 martin 251 tom
(integer) 6
127.0.0.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.0.0.1:6379> zadd user:ranking:2 8 james 77 mike 625 martin 888 tom
(integer) 4
127.0.0.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"
複製代碼
127.0.0.1:6379> zinterstore destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
複製代碼
·destination:交集計算結果保存到這個鍵。 ·numkeys:須要作交集計算鍵的個數。 ·key[key...]:須要作交集計算的鍵。 139 ·weights weight[weight...]:每一個鍵的權重,在作交集計算時,每一個鍵中 的每一個member會將本身分數乘以這個權重,每一個鍵的權重默認是1。 ·aggregate sum|min|max:計算成員交集後,分值能夠按照sum(和)、 min(最小值)、max(最大值)作彙總,默認值是sum。code
127.0.0.1:6379> zinterstore s1_s2_inter 2 user:ranking:1 user:ranking:2
(integer) 3
127.0.0.1:6379> zrange s1_s2_inter 0 -1 withscores
1) "mike"
2) "168"
3) "martin"
4) "875"
5) "tom"
6) "1139"
複製代碼
127.0.0.1:6379> zunionstore s1_s2_union 2 user:ranking:1 user:ranking:2
(integer) 7
127.0.0.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"
複製代碼
元素個數小於(默認是128),同時元素的值都小於(默認是64字節),redis會採用ziplist,能夠有效減小內存的使用。cdn
127.0.0.1:6379> zadd a 1 b
(integer) 1
127.0.0.1:6379> object encoding a
"ziplist"
複製代碼
當ziplist條件不知足的時候,redis會採用ziplist,由於此時ziplist的讀寫效率會降低。
127.0.0.1:6379> zadd zsetkey 50 e1 60 e2 30 e3 12 e4 ... 忽略 ... 84 e129
(integer) 129
127.0.0.1:6379> object encoding zsetkey
"skiplist"
複製代碼
排行版系統、記錄用戶天天上傳視頻的排行版。
127.0.0.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
複製代碼
127.0.0.1:6379> zrem user:ranking:2019_11_26 mike
(integer) 1
複製代碼
127.0.0.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"
複製代碼
127.0.0.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) "7"
11) "ioj"
12) "6"
13) "lkl"
14) "5"
15) "you"
16) "4"
17) "bok"
18) "3"
19) "jack"
20) "2"
複製代碼
連接:關注公衆號Elevenkeep,回覆redis便可得到連接。