Redis 有序集合和集合同樣也是string類型元素的集合,且不容許重複的成員。html
不一樣的是每一個元素都會關聯一個double類型的分數。redis正是經過分數來爲集合中的成員進行從小到大的排序。redis
有序集合的成員是惟一的,但分數(score)卻能夠重複。spa
1、向有序集合添加一個或多個成員,或者更新已存在成員的分數code
命令:zaddhtm
格式:zadd key score1 member1 [score2 member2] ...blog
127.0.0.1:6379> zadd k1 1.0 m1 2.0 m2 (integer) 2 127.0.0.1:6379> zadd k1 3.0 m1 4.0 m2 (integer) 0
2、經過索引區間返回有序集合指定區間內的成員排序
命令:zrange索引
格式:zrange key start stop [WITHSCORES]rem
127.0.0.1:6379> zadd k1 1.0 m1 2.0 m2 (integer) 2 127.0.0.1:6379> zrange k1 0 10 1) "m1" 2) "m2" 127.0.0.1:6379> zadd k1 3.0 m1 4.0 m3 (integer) 1 127.0.0.1:6379> zrange k1 0 10 1) "m2" 2) "m1" 3) "m3" 127.0.0.1:6379> zrange k1 0 10 WITHSCORES 1) "m2" 2) "2" 3) "m1" 4) "3" 5) "m3" 6) "4"
3、獲取有序集合的成員數字符串
命令:zcard
格式:zcard key
127.0.0.1:6379> zadd k1 1.0 m1 (integer) 1 127.0.0.1:6379> zcard k1 (integer) 1 127.0.0.1:6379> zadd k1 2.0 m1 3.0 m2 (integer) 1 127.0.0.1:6379> zcard k1 (integer) 2 127.0.0.1:6379> zrange k1 0 10 WITHSCORES 1) "m1" 2) "2" 3) "m2" 4) "3"
4、計算在有序集合中指定區間分數的成員數
命令:zcount
格式:zcount key min max
127.0.0.1:6379> zadd k1 1.1 m1 1.2 m2 1.3 m3 2.1 m4 2.2 m5 2.3 m6 (integer) 6 127.0.0.1:6379> zrange k1 0 -1 withscores 1) "m1" 2) "1.1000000000000001" 3) "m2" 4) "1.2" 5) "m3" 6) "1.3" 7) "m4" 8) "2.1000000000000001" 9) "m5" 10) "2.2000000000000002" 11) "m6" 12) "2.2999999999999998" 127.0.0.1:6379> zcount k1 1 2 (integer) 3 127.0.0.1:6379> zcount k1 1 3 (integer) 6
5、有序集合中對指定成員的分數加上增量 increment
命令:zincrby
格式:zincrby key increment member
127.0.0.1:6379> zadd k1 1.1 m1 1.2 m2 (integer) 2 127.0.0.1:6379> zrange k1 0 -1 withscores 1) "m1" 2) "1.1000000000000001" 3) "m2" 4) "1.2" 127.0.0.1:6379> zincrby k1 0.5 m1 "1.6000000000000001" 127.0.0.1:6379> zrange k1 0 -1 withscores 1) "m2" 2) "1.2" 3) "m1" 4) "1.6000000000000001"
6、計算給定的一個或多個有序集的交集並將結果集存儲在新的有序集合 key 中
命令:zinterstore
格式:zinterstore des numkeys key1 [key2] ...
127.0.0.1:6379> zadd k1 1.0 m1 2.0 m2 3.0 m3 (integer) 3 127.0.0.1:6379> zadd k2 1.0 m3 2.0 m4 3.0 m5 (integer) 3 127.0.0.1:6379> zrange k1 0 -1 withscores 1) "m1" 2) "1" 3) "m2" 4) "2" 5) "m3" 6) "3" 127.0.0.1:6379> zrange k2 0 -1 withscores 1) "m3" 2) "1" 3) "m4" 4) "2" 5) "m5" 6) "3" 127.0.0.1:6379> zinterstore d1 2 k1 k2 (integer) 1 127.0.0.1:6379> zrange d1 0 -1 withscores 1) "m3" 2) "4" 127.0.0.1:6379> zinterstore d2 2 k2 k1 (integer) 1 127.0.0.1:6379> zrange d2 0 -1 withscores 1) "m3" 2) "4" 127.0.0.1:6379> zadd k3 3.0 m1 4.0 m3 5.0 m5 (integer) 3 127.0.0.1:6379> zinterstore d3 3 k1 k2 k3 (integer) 1 127.0.0.1:6379> zrange d3 0 -1 withscores 1) "m3" 2) "8" 127.0.0.1:6379> zinterstore d4 3 k3 k2 k1 (integer) 1 127.0.0.1:6379> zrange d4 0 -1 withscores 1) "m3" 2) "8" 127.0.0.1:6379> zinterstore d5 3 k2 k3 k1 (integer) 1 127.0.0.1:6379> zrange d5 0 -1 withscores 1) "m3" 2) "8"
7、在相同分數的有序集合中,計算指定字典區間內成員數量
命令:zlexcount
格式:zlexcount key min max
說明:一、對於分數不相同的有序集合使用時,獲得的值可能不正確。
二、min/max的填寫,能夠寫 "-"(最小值),"+"(最大值),或者是以"["開頭
127.0.0.1:6379> zadd k1 1 m1 1 m2 1 m3 1 m4 1 m5 1 n1 1 n2 1 n3 (integer) 8 127.0.0.1:6379> zrange k1 0 -1 wighscores (error) ERR syntax error 127.0.0.1:6379> zrange k1 0 -1 withscores 1) "m1" 2) "1" 3) "m2" 4) "1" 5) "m3" 6) "1" 7) "m4" 8) "1" 9) "m5" 10) "1" 11) "n1" 12) "1" 13) "n2" 14) "1" 15) "n3" 16) "1" 127.0.0.1:6379> zlexcount k1 [m1 [m2 (integer) 2 127.0.0.1:6379> zlexcount k1 [m [n (integer) 5 127.0.0.1:6379> zlexcount k1 - [n (integer) 5 127.0.0.1:6379> zlexcount k1 - + (integer) 8
8、在相同分數的有序集合中,經過字典區間返回有序集合的成員
命令:zrangebylex
格式:zrangebylex key min max [LIMIT offset count]
127.0.0.1:6379> zadd k1 1 m1 1 m2 1 m3 1 m4 1 m5 1 n1 1 n2 1 n3 (integer) 8 127.0.0.1:6379> zrangebylex k1 [m [n 1) "m1" 2) "m2" 3) "m3" 4) "m4" 5) "m5" 127.0.0.1:6379> zrangebylex k1 [n1 + 1) "n1" 2) "n2" 3) "n3" 127.0.0.1:6379> zrangebylex k1 [m [n LIMIT 2 3 1) "m3" 2) "m4" 3) "m5" 127.0.0.1:6379> zrangebylex k1 [n1 + LIMIT 2 3 1) "n3"
9、經過分數返回有序集合指定區間內的成員
命令:zrangebyscore
格式:zrangebyscore key min max [WITHSCORES] [LIMIT offset count]
127.0.0.1:6379> zadd k1 1 a 2 b 3 c 4 d 5 e (integer) 5 127.0.0.1:6379> zrangebyscore k1 1 2 1) "a" 2) "b" 127.0.0.1:6379> zrangebyscore k1 3 5 1) "c" 2) "d" 3) "e" 127.0.0.1:6379> zrangebyscore k1 3 5 withscores 1) "c" 2) "3" 3) "d" 4) "4" 5) "e" 6) "5" 127.0.0.1:6379> zrangebyscore k1 3 5 withscores limit 2 1 1) "e" 2) "5"
10、返回有序集合中指定成員的索引
命令:zrank
格式:zrank key member
127.0.0.1:6379> zadd k1 1 a 2 b 3 c 4 d 5 e (integer) 5 127.0.0.1:6379> zrank k1 b (integer) 1 127.0.0.1:6379> zrange k1 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e" 127.0.0.1:6379> zrank k1 e (integer) 4
11、移除有序集合中的一個或多個成員
命令:zrem
格式:zrem key member1 [member2] ...
127.0.0.1:6379> zadd k1 1 a 2 b 3 c 4 d 5 e (integer) 5 127.0.0.1:6379> zrange k1 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e" 127.0.0.1:6379> zrem k1 a b c (integer) 3 127.0.0.1:6379> zrange k1 0 -1 1) "d" 2) "e"
12、移除相同分數的有序集合中給定的字典區間的全部成員
命令:zremrangebylex
格式:zremrangebylex key min max
127.0.0.1:6379> zadd k1 1 m1 1 m2 1 m3 1 m4 1 m5 1 n1 1 n2 1 n3 (integer) 8 127.0.0.1:6379> zrange k1 0 -1 1) "m1" 2) "m2" 3) "m3" 4) "m4" 5) "m5" 6) "n1" 7) "n2" 8) "n3" 127.0.0.1:6379> zremrangebylex k1 [m [n (integer) 5 127.0.0.1:6379> zrange k1 0 -1 1) "n1" 2) "n2" 3) "n3" 127.0.0.1:6379> del k1 (integer) 1 127.0.0.1:6379> zadd k1 1 m1 1 m2 1 m3 1 m4 1 m5 1 n1 1 n2 1 n3 (integer) 8 127.0.0.1:6379> zremrangebylex k1 [m [n1 (integer) 6 127.0.0.1:6379> zrange k1 0 -1 1) "n2" 2) "n3"
十3、移除有序集合中給定的排名區間的全部成員
命令:zremrangebyrank
格式:zremrangebyrank key start stop
127.0.0.1:6379> zadd k1 1 a 2 b 3 c 4 d 5 e (integer) 5 127.0.0.1:6379> zrange k1 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e" 127.0.0.1:6379> zremrangebyrank k1 2 3 (integer) 2 127.0.0.1:6379> zrange k1 0 -1 1) "a" 2) "b" 3) "e"
十4、移除有序集合中給定的分數區間的全部成員
命令:zremrangebyscore
格式:zremrangebyscore key min max
127.0.0.1:6379> zadd k1 1 a 2 b 3 c 4 d 5 e (integer) 5 127.0.0.1:6379> zrange k1 0 -1 withscores 1) "a" 2) "1" 3) "b" 4) "2" 5) "c" 6) "3" 7) "d" 8) "4" 9) "e" 10) "5" 127.0.0.1:6379> zremrangebyscore k1 3 4 (integer) 2 127.0.0.1:6379> zrange k1 0 -1 withscores 1) "a" 2) "1" 3) "b" 4) "2" 5) "e" 6) "5"
十5、返回有序集中指定區間內的成員,經過索引,分數從高到低
命令:zrevrange
格式:zrevrange key start stop [WITHSCORES]
127.0.0.1:6379> zadd k1 1 a 2 b 3 c 4 d 5 e (integer) 5 127.0.0.1:6379> zrevrange k1 1 3 1) "d" 2) "c" 3) "b" 127.0.0.1:6379> zrevrange k1 1 3 withscores 1) "d" 2) "4" 3) "c" 4) "3" 5) "b" 6) "2" 127.0.0.1:6379> zrange k1 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e"
十6、返回有序集中指定分數區間內的成員,分數從高到低排序
命令:zrevrangebyscore
格式:zrevrangebyscore key max min [WITHSCORES] [LIMIT offset count]
127.0.0.1:6379> zrange k1 0 -1 withscores 1) "a" 2) "1" 3) "b" 4) "2" 5) "c" 6) "3" 7) "d" 8) "4" 9) "e" 10) "5" 127.0.0.1:6379> zrevrangebyscore k1 5 3 1) "e" 2) "d" 3) "c" 127.0.0.1:6379> zrevrangebyscore k1 5 3 withscores 1) "e" 2) "5" 3) "d" 4) "4" 5) "c" 6) "3" 127.0.0.1:6379> zrevrangebyscore k1 5 3 withscores limit 2 1 1) "c" 2) "3"
十7、返回有序集合中指定成員的排名,有序集成員按分數值遞減(從大到小)排序
命令:zrevrank
格式:zrevrank key member
127.0.0.1:6379> zrange k1 0 -1 withscores 1) "a" 2) "1" 3) "b" 4) "2" 5) "c" 6) "3" 7) "d" 8) "4" 9) "e" 10) "5" 127.0.0.1:6379> zrevrank k1 a (integer) 4 127.0.0.1:6379> zrevrank k1 b (integer) 3 127.0.0.1:6379> zrevrank k1 c (integer) 2 127.0.0.1:6379> zrevrank k1 d (integer) 1 127.0.0.1:6379> zrevrank k1 e (integer) 0
十8、返回有序集中,成員的分數值
命令:zscore
格式:zscore key member
127.0.0.1:6379> zrange k1 0 -1 withscores 1) "a" 2) "1" 3) "b" 4) "2" 5) "c" 6) "3" 7) "d" 8) "4" 9) "e" 10) "5" 127.0.0.1:6379> zscore k1 a "1" 127.0.0.1:6379> zscore k1 b "2" 127.0.0.1:6379> zscore k1 c "3" 127.0.0.1:6379> zscore k1 d "4" 127.0.0.1:6379> zscore k1 e "5"
十9、計算給定的一個或多個有序集的並集,並存儲在新的 key 中
命令:zunionstore
格式:zunionstore des numbers key1 [key2] ....
127.0.0.1:6379> zadd k1 1 a 2 b 3 c (integer) 3 127.0.0.1:6379> zadd k2 1 a 2 c 3 e (integer) 3 127.0.0.1:6379> zadd k3 1 a 2 e 3 f (integer) 3 127.0.0.1:6379> zunionstore d1 2 k1 k2 (integer) 4 127.0.0.1:6379> zrange d1 0 -1 withscores 1) "a" 2) "2" 3) "b" 4) "2" 5) "e" 6) "3" 7) "c" 8) "5" 127.0.0.1:6379> zunionstore d2 3 k1 k2 k3 (integer) 5 127.0.0.1:6379> zrange d2 0 -1 withscores 1) "b" 2) "2" 3) "a" 4) "3" 5) "f" 6) "3" 7) "c" 8) "5" 9) "e" 10) "5"
二10、迭代有序集合中的元素(包括元素成員和元素分值)
命令:zscan
格式:zscan key cursor [MATCH pattern] [COUNT count]
127.0.0.1:6379> zadd k1 1 m1 2 m2 3 m3 4 m4 5 m5 6 member1 7 member2 8 member3 9 member4 10 member5 (integer) 10 127.0.0.1:6379> zrange k1 0 -1 withscores 1) "m1" 2) "1" 3) "m2" 4) "2" 5) "m3" 6) "3" 7) "m4" 8) "4" 9) "m5" 10) "5" 11) "member1" 12) "6" 13) "member2" 14) "7" 15) "member3" 16) "8" 17) "member4" 18) "9" 19) "member5" 20) "10" 127.0.0.1:6379> zscan k1 0 match m* 1) "0" 2) 1) "m1" 2) "1" 3) "m2" 4) "2" 5) "m3" 6) "3" 7) "m4" 8) "4" 9) "m5" 10) "5" 11) "member1" 12) "6" 13) "member2" 14) "7" 15) "member3" 16) "8" 17) "member4" 18) "9" 19) "member5" 20) "10" 127.0.0.1:6379> zscan k1 0 match me* 1) "0" 2) 1) "member1" 2) "6" 3) "member2" 4) "7" 5) "member3" 6) "8" 7) "member4" 8) "9" 9) "member5" 10) "10"