Sorted Set與Hash一樣,可以存放很多key and value.Sorted Set 的每個key (member)不可以重覆,而且key必須為浮點數,被稱為score,可以依據score排序,存取方式與hash相同,也可以依照score與score sort 順序存取.
可用指令
資料結構
在sorted set 加入member.
127.0.0.1:6379> zadd engtest 90 jack 85 linda
(integer) 2
127.0.0.1:6379> zadd engtest 99 doris 80 eagle
(integer) 2
127.0.0.1:6379> zrange engtest 0 -1
1) "eagle"
2) "linda"
3) "jack"
4) "doris"
127.0.0.1:6379> zrange engtest 0 -1 withscores
1) "eagle"
2) "80"
3) "linda"
4) "85"
5) "jack"
6) "90"
7) "doris"
8) "99"
查詢sorted set中的指定最大與最小member並正向排序,可以包含score.
(如果score分數一樣則會按照value的字母進行排序)
127.0.0.1:6379> zadd engtest 90 jack 85 linda
(integer) 2
127.0.0.1:6379> zadd engtest 99 doris 80 eagle
(integer) 2
# 0 "eagle" -4
# 1 "linda" -3
# 2 "jack" -2
# 3 "doris" -1
# 取得engtest範圍內的member,將score正向排序
127.0.0.1:6379> zrange engtest 0 -1
1) "eagle"
2) "linda"
3) "jack"
4) "doris"
# 取得engtest範圍內的member,需要包含score內容
127.0.0.1:6379> zrange engtest 0 -1 withscores
1) "eagle"
2) "80"
3) "linda"
4) "85"
5) "jack"
6) "90"
7) "doris"
8) "99"
# 取得engtest範圍內的member,將score反向排序
127.0.0.1:6379> zrange engtest 0 -1 rev
1) "doris"
2) "jack"
3) "linda"
4) "eagle"
# 取得engtest範圍內的member中score最高的兩個
127.0.0.1:6379> zrange engtest 0 1 rev
1) "doris"
2) "jack"
查詢sorted set中的指定最大與最小member並反向排序,可以包含score.
(如果score分數一樣則會按照value的字母進行排序)
127.0.0.1:6379> zadd engtest 90 jack 85 linda 99 doris 80 eagle
(integer) 4
# 0 "eagle" -4
# 1 "linda" -3
# 2 "jack" -2
# 3 "doris" -1
# 取得engtest範圍內的member,將score反向排序
127.0.0.1:6379> zrevrange engtest 0 -1
1) "doris"
2) "jack"
3) "linda"
4) "eagle"
# 取得engtest範圍內的member,需要包含score內容
127.0.0.1:6379> zrevrange engtest 0 -1 withscores
1) "doris"
2) "99"
3) "jack"
4) "90"
5) "linda"
6) "85"
7) "eagle"
8) "80"
取得sorted set中member的個數.
127.0.0.1:6379> zadd engtest 90 jack 85 linda
(integer) 2
127.0.0.1:6379> zadd engtest 99 doris 80 eagle
(integer) 2
127.0.0.1:6379> zrange engtest 0 -1
1) "eagle"
2) "linda"
3) "jack"
4) "doris"
127.0.0.1:6379> zcard engtest
(integer) 4
取得sorted set中score符合範圍內的member總數.
127.0.0.1:6379> zadd engtest 90 jack 85 linda
(integer) 2
127.0.0.1:6379> zadd engtest 99 doris 80 eagle
(integer) 2
127.0.0.1:6379> zrange engtest 0 -1
1) "eagle"
2) "linda"
3) "jack"
4) "doris"
# 取得engtest中member score在80 ~ 90 的總數
127.0.0.1:6379> zcount engtest 80 90
(integer) 3
取得sorted set中member的value字串在範圍的個數.
127.0.0.1:6379> zadd mytest 0 a 0 b 0 c 0 d 0 e 0 f 0 g 0 h 0 i
(integer) 9
127.0.0.1:6379> zrangebylex mytest - +
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
6) "f"
7) "g"
8) "h"
9) "i"
# 取的mytest中member的value字串在 -無限大 到 +無限大 範圍內的個數
127.0.0.1:6379> zlexcount mytest - +
(integer) 9
# 取的mytest中member的value字串在 包含c 到 包含g 範圍內的個數
127.0.0.1:6379> zlexcount mytest [c [g
(integer) 5
取得sorted set中指定member的正向排序.
127.0.0.1:6379> zadd engtest 90 jack 85 linda
(integer) 2
127.0.0.1:6379> zadd engtest 99 doris 80 eagle
(integer) 2
127.0.0.1:6379> zrange engtest 0 -1
1) "eagle"
2) "linda"
3) "jack"
4) "doris"
127.0.0.1:6379> zrank engtest eagle
(integer) 0
127.0.0.1:6379> zrank engtest linda
(integer) 1
取得sorted set中指定member的反向排序.
127.0.0.1:6379> zadd engtest 90 jack 85 linda
(integer) 2
127.0.0.1:6379> zadd engtest 99 doris 80 eagle
(integer) 2
127.0.0.1:6379> zrevrange engtest 0 -1
1) "doris"
2) "jack"
3) "linda"
4) "eagle"
127.0.0.1:6379> zrevrank engtest eagle
(integer) 3
127.0.0.1:6379> zrevrank engtest linda
(integer) 2
取得sorted set中指定member的score.
127.0.0.1:6379> zadd engtest 90 jack 85 linda
(integer) 2
127.0.0.1:6379> zadd engtest 99 doris 80 eagle
(integer) 2
127.0.0.1:6379> zrange engtest 0 -1
1) "eagle"
2) "linda"
3) "jack"
4) "doris"
# 取得engtest中eagle的score
127.0.0.1:6379> zscore engtest eagle
"80"
設定sorted set中指定member的score增加.
127.0.0.1:6379> zadd engtest 90 jack 85 linda
(integer) 2
127.0.0.1:6379> zadd engtest 99 doris 80 eagle
(integer) 2
127.0.0.1:6379> zrange engtest 0 -1 withscores
1) "eagle"
2) "80"
3) "linda"
4) "85"
5) "jack"
6) "90"
7) "doris"
8) "99"
# 將engtest中member linda的score加6
127.0.0.1:6379> zincrby engtest 6 linda
"91"
127.0.0.1:6379> zrange engtest 0 -1 withscores
1) "eagle"
2) "80"
3) "jack"
4) "90"
5) "linda"
6) "91"
7) "doris"
8) "99"
取得sorted set 中member的score正向排序在指定範圍內.
127.0.0.1:6379> zadd engtest 90 jack 85 linda 99 doris 80 eagle
(integer) 4
# 取得engtest中member的score正向排序在 90 ~ 100
127.0.0.1:6379> zrangebyscore engtest 90 100
1) "jack"
2) "doris"
取得sorted set 中member的score反向排序在指定範圍內.
127.0.0.1:6379> zadd engtest 90 jack 85 linda 99 doris 80 eagle
(integer) 4
# 取得engtest中member的score反向排序在 90 ~ 100
127.0.0.1:6379> zrevrangebyscore engtest 90 100
(empty array)
# 取得engtest中member的score反向排序在 100 ~ 90
127.0.0.1:6379> zrevrangebyscore engtest 100 90
1) "doris"
2) "jack"
# 取得engtest中member的score反向排序在 -無限大 ~ +無限大 = 所有 取2個
127.0.0.1:6379> zrevrangebyscore engtest -inf +inf limit 0 2
(empty array)
# 取得engtest中member的score反向排序在 +無限大 ~ -無限大 =所有 取2個
127.0.0.1:6379> zrevrangebyscore engtest +inf -inf limit 0 2
1) "doris"
2) "jack"
取得sorted set 中membe的value字母正向排序在指定範圍內.
127.0.0.1:6379> zadd engtest 90 jack 85 linda 99 doris 80 eagle
(integer) 4
127.0.0.1:6379> zrangebylex engtest - +
1) "eagle"
2) "linda"
3) "jack"
4) "doris"
127.0.0.1:6379> zadd mytest 0 a 0 b 0 c 0 d 0 e 0 f 0 g 0 h 0 i
(integer) 9
# 取得mytest中member的value範圍 -無限大到包含d
127.0.0.1:6379> zrangebylex mytest - [d
1) "a"
2) "b"
3) "c"
4) "d"
# 取得mytest中member的value範圍 -無限大到不包含d
127.0.0.1:6379> zrangebylex mytest - (d
1) "a"
2) "b"
3) "c"
# 取得mytest中member的value範圍 包含b到不包含h
127.0.0.1:6379> zrangebylex mytest [b (h
1) "b"
2) "c"
3) "d"
4) "e"
5) "f"
6) "g"
刪除sorted test中指定member.
127.0.0.1:6379> zadd engtest 90 jack 85 linda 99 doris 80 eagle
(integer) 4
127.0.0.1:6379> zrange engtest 0 -1
1) "eagle"
2) "linda"
3) "jack"
4) "doris"
# 刪除engtest中member是eagle
127.0.0.1:6379> zrem engtest eagle
(integer) 1
127.0.0.1:6379> zrange engtest 0 -1
1) "jack"
2) "linda"
3) "doris"
刪除sorted test中member的score反向排序在指定範圍內.
127.0.0.1:6379> zadd engtest 90 jack 85 linda 99 doris 80 eagle
(integer) 4
# 刪除engtest中member的score在 0 ~ 90
127.0.0.1:6379> zremrangebyscore engtest 0 90
(integer) 3
127.0.0.1:6379> zrange engtest 0 -1
1) "doris"
刪除sorted set中member的value字母正向排序在指定範圍內.
127.0.0.1:6379> zadd engtest 90 jack 85 linda 99 doris 80 eagle
(integer) 4
# 刪除engtest中member的value字母在所有範圍內的
127.0.0.1:6379> zremrangebylex engtest - +
(integer) 4
127.0.0.1:6379> zadd mytest 0 a 0 b 0 c 0 d 0 e 0 f 0 g 0 h 0 i
(integer) 9
# 刪除engtest中member的value字母在 -無限大 到 包含d 範圍內的
127.0.0.1:6379> zremrangebylex mytest - [d
(integer) 4
127.0.0.1:6379> zrange mytest 0 -1
1) "e"
2) "f"
3) "g"
4) "h"
5) "i"
刪除sorted set中正向排序後指定範圍的member.
127.0.0.1:6379> zadd engtest 90 jack 85 linda 99 doris 80 eagle
(integer) 4
127.0.0.1:6379> zrange engtest 0 -1
1) "eagle"
2) "linda"
3) "jack"
4) "doris"
# 刪除engtest中正向排序 0 ~ 2 的member
127.0.0.1:6379> zremrangebyrank engtest 0 2
(integer) 3
127.0.0.1:6379> zrange engtest 0 -1
1) "doris"
掃描sorted set中指定member 特定match pattern與次數.
127.0.0.1:6379> zadd engtest 90 jack 85 linda 99 doris 80 eagle
(integer) 4
127.0.0.1:6379> zrange engtest 0 -1
1) "eagle"
2) "linda"
3) "jack"
4) "doris"
127.0.0.1:6379> zscan engtest 0 match e*
1) "0"
2) 1) "eagle"
2) "80"
取得多個sorted set中member的交集,並存入另外一個sorted set中,可加入weights進行加乘.
127.0.0.1:6379> zadd mytest1 1 a 2 b 3 c
(integer) 3
127.0.0.1:6379> zadd mytest2 5 a 7 b 3 c
(integer) 3
127.0.0.1:6379> zrange mytest1 0 -1 withscores
1) "a"
2) "1"
3) "b"
4) "2"
5) "c"
6) "3"
127.0.0.1:6379> zrange mytest2 0 -1 withscores
1) "c"
2) "3"
3) "a"
4) "5"
5) "b"
6) "7"
# 將mytest1與mytest2中交集的member進行weights後寫入到mytest3
127.0.0.1:6379> zunionstore mytest3 2 mytest1 mytest2 weights 2 3
(integer) 3
127.0.0.1:6379> zrange mytest3 0 -1 withscores
1) "c"
2) "15"
3) "a"
4) "17"
5) "b"
6) "25"
取得多個sorted set中member的聯集,並存入另外一個sorted set中.
127.0.0.1:6379> zadd mytest1 1 a 2 b 3 c
(integer) 3
127.0.0.1:6379> zadd mytest2 5 a 7 b 3 c
(integer) 3
127.0.0.1:6379> zrange mytest1 0 -1 withscores
1) "a"
2) "1"
3) "b"
4) "2"
5) "c"
6) "3"
127.0.0.1:6379> zrange mytest2 0 -1 withscores
1) "c"
2) "3"
3) "a"
4) "5"
5) "b"
6) "7"
# 將mytest1與mytest2中聯集的member後寫入到mytest4
127.0.0.1:6379> zinterstore mytest4 2 mytest1 mytest2
(integer) 3
127.0.0.1:6379> zrange mytest4 0 -1 withscores
1) "a"
2) "6"
3) "c"
4) "6"
5) "b"
6) "9"