1. 有序集合簡(jiǎn)介
有序集合保留了集合中不能有重復(fù)成員的特性,同時(shí)又對(duì)集合中的元素進(jìn)行排序孙技。列表是使用索引下標(biāo)作為
排序的依據(jù)潦嘶,有序集合則是給每個(gè)元素設(shè)置一個(gè)分?jǐn)?shù)(score)作為排序的依據(jù)。
2. 常用命令
# (1) 添加元素:ZADD key [NX|XX] [CH] [INCR] score value
# NX:value必須不存在才可以添加成功
# XX:value必須存在才可以添加成功,用于更新
# CH:返回score或者value發(fā)生變化的個(gè)數(shù)
# INCR:score
# 返回添加成功的個(gè)數(shù)
beh07:6379> ZADD students 100 tom
(integer) 1
beh07:6379> ZADD students 98 tony
(integer) 1
beh07:6379> ZADD students CH 77 bob 88 tom
(integer) 2
# 在原來(lái)score的基礎(chǔ)上+87晶渠,返回增加之后的分值
beh07:6379> ZADD students INCR 87 jack
"174"
# (2) 統(tǒng)計(jì)元素個(gè)數(shù)
beh07:6379> ZCARD students
(integer) 4
# (3) 獲取元素分值
beh07:6379> ZSCORE students tom
"88"
# (4) 計(jì)算排名
# 獲取[m,n]位排序后的元素,[0,-1]代表獲取全部元素,默認(rèn)是使用score升序排序
beh07:6379> ZRANGE students 0 -1
1) "bob"
2) "tom"
3) "tony"
4) "jack"
# 獲取[m,n]位排序后的元素首昔,[0,-1]代表獲取全部元素,降序排序糙俗,withscores參數(shù)可以返回分?jǐn)?shù)
beh07:6379> ZREVRANGE students 0 2 WITHSCORES
1) "jack"
2) "174"
3) "tony"
4) "98"
5) "tom"
6) "88"
# 獲取指定value的排名(升序勒奇,從0開始)
beh07:6379> ZRANK students tom
(integer) 1
# 獲取指定vaue的排名(降序,從0開始)
beh07:6379> ZREVRANK students tom
(integer) 2
# (5) 刪除元素
beh07:6379> ZREM students jack
(integer) 1
# (6) 增加分?jǐn)?shù)
beh07:6379> ZSCORE students tom
"88"
beh07:6379> ZINCRBY students 12 tom
"100"
# (7) 返回指定排名范圍的成員
# 返回倒數(shù)前3名
beh07:6379> zrange students 0 2 withscores
1) "Eirene"
2) "92"
3) "Habiba"
4) "93"
5) "Isoke"
6) "94"
# 返回前3名
beh07:6379> zrevrange students 0 2 withscores
1) "tom"
2) "100"
3) "Miki"
4) "99"
5) "tony"
6) "98"
# (8) 返回指定分?jǐn)?shù)范圍的成員
# zrangebyscore key min max [withscores] [limit offset count]
# zrevrangebyscore key min max [withscores] [limit offset count]
# zrangebyscore升序巧骚,zrevrangebyscore降序
# 指定的范圍min和max都會(huì)包括到輸出范圍,[min, max]
# [limit offset count]用于限制輸出的起始位置和個(gè)數(shù)
beh07:6379> zrangebyscore students 95 99 withscores
1) "Jayne"
2) "95"
3) "Joy"
4) "96"
5) "Kirima"
6) "97"
7) "Laura"
8) "98"
9) "tony"
10) "98"
11) "Miki"
12) "99"
# +inf:正無(wú)窮赊颠,-inf:負(fù)無(wú)窮
beh07:6379> zrangebyscore students 95 +inf withscores
1) "Jayne"
2) "95"
3) "Joy"
4) "96"
5) "Kirima"
6) "97"
7) "Laura"
8) "98"
9) "tony"
10) "98"
11) "Miki"
12) "99"
13) "tom"
14) "100"
beh07:6379> zrangebyscore students -inf 90 withscores
1) "bob"
2) "77"
3) "Allen"
4) "90"
# 從第4名開始輸出2個(gè)結(jié)果(第一名索引為0)
beh07:6379> zrangebyscore students -inf +inf withscores limit 3 2
1) "Eirene"
2) "92"
3) "Habiba"
4) "93"
# 支持開區(qū)間,'('代表開區(qū)間,默認(rèn)是閉區(qū)間
beh07:6379> zrangebyscore students (90 93 withscores
1) "Danica"
2) "91"
3) "Eirene"
4) "92"
5) "Habiba"
6) "93"
beh07:6379> zrangebyscore students 90 (93 withscores
1) "Allen"
2) "90"
3) "Danica"
4) "91"
5) "Eirene"
6) "92"
beh07:6379> zrangebyscore students (90 (93 withscores
1) "Danica"
2) "91"
3) "Eirene"
4) "92"
# (9) 返回指定分?jǐn)?shù)范圍內(nèi)的成員個(gè)數(shù)
beh07:6379> zcount students 90 100
(integer) 12
# (10) 刪除指定排名內(nèi)的成員
# 升序排序劈彪,刪除前3名
beh07:6379> zremrangebyrank students 0 2
(integer) 3
# (11) 刪除指定分?jǐn)?shù)范圍內(nèi)的成員
beh07:6379> zremrangebyscore students 95 99
(integer) 6
# (12) 刪除指定值的元素
beh07:6379> zrem students tom
(integer) 1
3. 集合間操作
準(zhǔn)備測(cè)試數(shù)據(jù):
beh07:6379> zadd user1 67 Allen 88 Mike 98 Candy 97 Ella 99 Hebe 56 Wawa
(integer) 6
beh07:6379> zadd user2 73 Bob 82 Mike 43 Hebe 77 Wawa
(integer) 4
# (1) 交集
# 對(duì)集合user1和user2做交集竣蹦,結(jié)果保存到user_inter1集合中
# 數(shù)字2的意思做交集的集合的個(gè)數(shù),zinterstore user_inter_tmp n user1 user2 ... usern
beh07:6379> zinterstore user_inter1 2 user1 user2
(integer) 3
# 可以看到對(duì)于相同的value的分?jǐn)?shù)做了加法
beh07:6379> zrange user_inter1 0 -1 withscores
1) "Wawa"
2) "133"
3) "Hebe"
4) "142"
5) "Mike"
6) "170"
# 指定權(quán)重沧奴,默認(rèn)為1
beh07:6379> zinterstore user_inter2 2 user1 user2 WEIGHTS 1 0.5 AGGREGATE max
(integer) 3
# 最終分?jǐn)?shù)的計(jì)算過(guò)程 score = max(user1.value.score * 1, user2.value.score * 0.5)
# 支持的聚合操作有max/min/sum痘括,默認(rèn)使用sum
beh07:6379> zrange user_inter2 0 -1 withscores
1) "Wawa"
2) "56"
3) "Mike"
4) "88"
5) "Hebe"
6) "99"
# (2) 并集
# zunionstore的參數(shù)用法與zinterstore相同
beh07:6379> zunionstore user_union1 2 user1 user2
(integer) 7
beh07:6379> zrange user_union1 0 -1 withscores
1) "Allen"
2) "67"
3) "Bob"
4) "73"
5) "Ella"
6) "97"
7) "Candy"
8) "98"
9) "Wawa"
10) "133"
11) "Hebe"
12) "142"
13) "Mike"
14) "170"
beh07:6379> zunionstore user_union2 2 user1 user2 WEIGHTS 1 0.5 AGGREGATE MIN
(integer) 7
beh07:6379> zrange user_union2 0 -1 withscores
1) "Hebe"
2) "21.5"
3) "Bob"
4) "36.5"
5) "Wawa"
6) "38.5"
7) "Mike"
8) "41"
9) "Allen"
10) "67"
11) "Ella"
12) "97"
13) "Candy"
14) "98"
4. 內(nèi)部編碼
相關(guān)參數(shù):
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
當(dāng)有序集合中的元素個(gè)數(shù)小于zset-max-ziplist-entries
配置的值同時(shí)每個(gè)元素的大小不超過(guò)64字節(jié)時(shí),使用ziplist
作為底層實(shí)現(xiàn)滔吠,否則使用skiplist
作為底層實(shí)現(xiàn)
beh07:6379> zadd zset1 1 tom
(integer) 1
beh07:6379> object encoding zset1
"ziplist"
beh07:6379> zadd zset2 1 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'
(integer) 1
beh07:6379> object encoding zset2
"skiplist"