歡迎大家加入 459479177QQ群進行交流
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:申請域名、虛擬主機、營銷軟件、網(wǎng)站建設(shè)、南寧網(wǎng)站維護、網(wǎng)站推廣。
這一章是redis Sorted Set有序集合五種方式的最后一種,
首先我們使用redis-cli進入redis
[root@web1 ~]# redis-cli 127.0.0.1:6379>
1>zadd
方法:zadd key source1 [key sourceN...]
描述:添加一個或多個有序集合成員元素,如果存在則更新成員的分數(shù)值,并插入此成語,確保該成員在正確的位置,如果key不存在則創(chuàng)建一個新的key,然后在執(zhí)行zadd操作
127.0.0.1:6379> zadd zkey1 1 Sunshine \\第一次成員分數(shù)1 (integer) 1 127.0.0.1:6379> zrange zkey1 0 -1 withscores 1) "Sunshine" 2) "1" 127.0.0.1:6379> zadd zkey1 3 Sunshine \\成員分數(shù)3 (integer) 0 127.0.0.1:6379> zrange zkey1 0 -1 withscores \\成員分數(shù)變成3,成員元素沒變 1) "Sunshine" 2) "3"
2>zcard
方法:zcard key
描述:查看key的成員元素個數(shù),一個分數(shù)值對應(yīng)多個元素
127.0.0.1:6379> zadd zkey 1 Sunshine 1 Sunshine2 1 Sunshine3 (integer) 3 127.0.0.1:6379> zcount zkey 1 3 (integer) 3 127.0.0.1:6379> zrange zkey 0 -1 withscores 1) "Sunshine" 2) "1" 3) "Sunshine2" 4) "1" 5) "Sunshine3" 6) "1"
3>zcount
方法:zacount key min max
描述:統(tǒng)計指定集合的成員元素
127.0.0.1:6379> zrange zkey2 0 -1 withscores (empty list or set) 127.0.0.1:6379> zadd zkey2 1 Sushine 1 Sunshine0 (integer) 2 127.0.0.1:6379> zrange zkey2 0 -1 withscores 1) "Sunshine0" 2) "1" 3) "Sushine" 4) "1" 127.0.0.1:6379> zcount zkey2 1 2 (integer) 2
4>zincrby
方法:zincrby key increment member
描述:對有序集合指定成員分數(shù)值增量,如果-則為減
127.0.0.1:6379> zrange zkey2 0 -1 withscores 1) "Sunshine0" 2) "1" 3) "Sushine" 4) "1" 5) "Sunshine" 6) "5" 127.0.0.1:6379> zincrby zkey2 5 "Sunshine" "10" 127.0.0.1:6379> zrange zkey2 0 -1 withscores 1) "Sunshine0" 2) "1" 3) "Sushine" 4) "1" 5) "Sunshine" 6) "10" 127.0.0.1:6379> zincrby zkey2 -5 "Sunshine" "5" 127.0.0.1:6379> zrange zkey2 0 -1 withscores 1) "Sunshine0" 2) "1" 3) "Sushine" 4) "1" 5) "Sunshine" 6) "5"
5>zintersotre
方法:zintersotre destination numkeys key [key...] [weights weighe [weight...]] [aggregate sun|min|max]
描述:把后面destination后面的集合相同成員分數(shù)值相加并添加至destinatio集合成員
127.0.0.1:6379> zrange zkey3 0 -1 withscores 1) "Sunshine" 2) "11" 3) "SunshineBoy" 4) "44" 127.0.0.1:6379> zrange zkey4 0 -1 withscores 1) "Sunshine" 2) "22" 3) "SunshineBoy" 4) "33" 127.0.0.1:6379> zinterstore zkey5 2 zkey4 zkey3 (integer) 2 127.0.0.1:6379> zrange zkey5 0 -1 withscores 1) "Sunshine" 2) "33" 3) "SunshineBoy" 4) "77"
6>zlexcount
方法:zlexcount key min max
描述:指定有序集合字典區(qū)間成員數(shù)
127.0.0.1:6379> zrange zkey6 0 -1 withscores 1) "a" 2) "0" 3) "b" 4) "0" 5) "c" 6) "0" 7) "d" 8) "0" 9) "e" 10) "0" 11) "f" 12) "0" 13) "g" 14) "0" 127.0.0.1:6379> zlexcount zkey6 [b [f (integer) 5 127.0.0.1:6379> zlexcount zkey6 [b [g (integer) 6
7>zrange
方法:zrange key start end [withscores]
描述:指定查看有序集合區(qū)間成員
127.0.0.1:6379> zrange zkey6 0 -1 withscores 1) "a" 2) "0" 3) "b" 4) "0" 5) "c" 6) "0" 7) "d" 8) "0" 9) "e" 10) "0" 11) "f" 12) "0" 13) "g" 14) "0" 127.0.0.1:6379> zrange zkey6 0 3 withscores 1) "a" 2) "0" 3) "b" 4) "0" 5) "c" 6) "0" 7) "d" 8) "0"
8>zrangebylex
方法:zrangebylex key min max [limit offset count]
描述:通過字典區(qū)間返回有序集合成員
127.0.0.1:6379> zrangebylex zkey6 - [c 1) "a" 2) "b" 3) "c" 127.0.0.1:6379> zrangebylex zkey6 - + 1) "a" 2) "b" 3) "c" 4) "d" 5) "e" 6) "f" 7) "g" 127.0.0.1:6379> zrange zkey6 0 -1 withscores 1) "a" 2) "0" 3) "b" 4) "0" 5) "c" 6) "0" 7) "d" 8) "0" 9) "e" 10) "0" 11) "f" 12) "0" 13) "g" 14) "0"
9>zrangesotre
方法:zrangesotre key min max [withscores] [limit offsec count]
描述:通過成員分數(shù)值返回區(qū)間成員
127.0.0.1:6379> zrangebyscore zkey10 -inf +inf \\顯示整個有序集合成員 1) "jack" 2) "tom" 3) "Sunshine" 127.0.0.1:6379> zrangebyscore zkey10 -inf +inf withscores \\顯示整個有序集合成員的score 1) "jack" 2) "2000" 3) "tom" 4) "8000" 5) "Sunshine" 6) "15000" 127.0.0.1:6379> zrangebyscore zkey10 -inf 5000 withscores \\顯示小于等于5000分數(shù)值的成員 1) "jack" 2) "2000" 127.0.0.1:6379> zrangebyscore zkey10 (5000 20000 withscores \\顯示分數(shù)值5000至20000之間的成員 1) "tom" 2) "8000" 3) "Sunshine" 4) "15000" 127.0.0.1:6379>
10>zrank
方法:zrank key member
描述:顯示有序集合成員的索引(index或者叫下標)
127.0.0.1:6379> zrank zkey10 tom (integer) 1 127.0.0.1:6379> zrank zkey10 Sunshine (integer) 2 127.0.0.1:6379> zrank zkey10 jack (integer) 0 127.0.0.1:6379> zrange zkey10 0 -1 withscores 1) "jack" 2) "2000" 3) "tom" 4) "8000" 5) "Sunshine" 6) "15000"
11>zrem
方法:zrem key member [member...]
描述:移除一個有序集合成員或多個
127.0.0.1:6379> zrange zkey10 0 -1 withscores 1) "jack" 2) "2000" 3) "tom" 4) "8000" 5) "Sunshine" 6) "15000" 127.0.0.1:6379> zrem zkey10 jack (integer) 1 127.0.0.1:6379> zrem zkey10 tom Sunshine (integer) 2 127.0.0.1:6379> zrange zkey10 0 -1 withscores (empty list or set)
12>zremrangebylex
方法:zremrangebylex key min max
描述:Zremrangebylex 命令用于移除有序集合中給定的字典區(qū)間的所有成員
127.0.0.1:6379> zrange zkey2 0 -1 withscores 1) "a" 2) "1" 3) "b" 4) "1" 5) "c" 6) "1" 7) "d" 8) "1" 9) "e" 10) "1" 11) "f" 12) "1" 13) "g" 14) "1" 15) "h" 16) "1" 17) "l" 18) "1" 19) "m" 20) "1" 127.0.0.1:6379> zremrangebylex zkey2 [a [c (integer) 3 127.0.0.1:6379> zrange zkey2 0 -1 withscores 1) "d" 2) "1" 3) "e" 4) "1" 5) "f" 6) "1" 7) "g" 8) "1" 9) "h" 10) "1" 11) "l" 12) "1" 13) "m" 14) "1" 127.0.0.1:6379> zremrangebylex zkey2 [d [h (integer) 5 127.0.0.1:6379> zrange zkey2 0 -1 withscores 1) "l" 2) "1" 3) "m" 4) "1"
13>zremrangeburank
方法:zremrangebyrank key start end
描述:移除有序集合中給定的排名區(qū)間的所有成員(指定是指下標)
127.0.0.1:6379> zrange zkey11 0 -1 withscores 1) "Sunshine" 2) "100" 3) "SunshineBoy" 4) "200" 5) "SnshineBoySZF" 6) "300" 127.0.0.1:6379> zremrangebyrank zkey11 0 0 (integer) 1 127.0.0.1:6379> zrange zkey11 0 -1 withscores 1) "SunshineBoy" 2) "200" 3) "SnshineBoySZF" 4) "300" 127.0.0.1:6379> zremrangebyrank zkey11 0 0 (integer) 1 127.0.0.1:6379> zrange zkey11 0 -1 withscores 1) "SnshineBoySZF" 2) "300" 127.0.0.1:6379> zadd zkey11 500 hehe (integer) 1 127.0.0.1:6379> zrange zkey11 0 -1 withscores 1) "SnshineBoySZF" 2) "300" 3) "hehe" 4) "500" 127.0.0.1:6379> zremrangebyrank zkey11 0 1 (integer) 2 127.0.0.1:6379> zrange zkey11 0 -1 withscores (empty list or set)
14>zremrangebyscore
方法:zremrangebyscore key min max
描述:指定有序集合,移除(score)區(qū)間的成員
127.0.0.1:6379> zadd zkey12 100 Sunshine 200 SunshineBoy 300 SnshineBoySZF (integer) 3 127.0.0.1:6379> zrange zkey12 0 -1 withscores 1) "Sunshine" 2) "100" 3) "SunshineBoy" 4) "200" 5) "SnshineBoySZF" 6) "300" 127.0.0.1:6379> zremrangebyscore zkey12 200 300 (integer) 2 127.0.0.1:6379> zrange zkey12 0 -1 withscores 1) "Sunshine" 2) "100"
15>zrevrage
方法:zrevrange key start end [withscores]
描述:指定有序集合倒序排序
127.0.0.1:6379> zrange zkey12 0 -1 withscores 1) "Sunshine" 2) "100" 3) "SunshineBoy" 4) "200" 5) "SnshineBoySZF" 6) "300" 127.0.0.1:6379> zrevrange zkey12 0 -1 withscores 1) "SnshineBoySZF" 2) "300" 3) "SunshineBoy" 4) "200" 5) "Sunshine" 6) "100"
16>zrevrangebybscore
方法:zrevrangebyscore key min max [withscores] [limit offsec count]
描述:指定有序集合,按指定分數(shù)值從高到底排序(通俗講就是,按照指定值,倒序)
127.0.0.1:6379> zrevrange zkey12 0 -1 withscores
1) "SnshineBoySZF"
2) "300"
3) "SunshineBoy"
4) "200"
5) "Sunshine"
6) "100"
127.0.0.1:6379> zrevrangebyscore zkey12 +inf -inf \\倒序排列
1) "SnshineBoySZF"
2) "SunshineBoy"
3) "Sunshine"
127.0.0.1:6379> zrevrangebyscore zkey12 200 100
1) "SunshineBoy"
2) "Sunshine"
17>zrevrank
方法:zrevrank key start end [withscores] [limit offsec count]
127.0.0.1:6379> zrange zkey 0 -1 withscores 1) "sunshine" 2) "100" 3) "tom" 4) "200" 5) "jack" 6) "500" 7) "hehe" 8) "2000" 127.0.0.1:6379> zrevrank zkey tom (integer) 2 127.0.0.1:6379> zrevrank zkey hehe (integer) 0
18>zscore
方法:zscore key member
描述:返回有序集合中,成員的分數(shù)值
127.0.0.1:6379> zrange zkey 0 -1 withscores 1) "sunshine" 2) "100" 3) "tom" 4) "200" 5) "jack" 6) "500" 7) "hehe" 8) "2000" 127.0.0.1:6379> zscore zkey tom "200" 127.0.0.1:6379> zscore zkey jack "500"
19>zunionstore
方法:ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] {直接復制官方,官方的例子沒整明白,容后補寫指理論}
描述:計算給定的一個或多個有序集的并集,并存儲在新的 key 中。
127.0.0.1:6379> zadd zkey1 100 a 200 b (integer) 2 127.0.0.1:6379> zadd zkey2 300 c 400 b 500 e (integer) 1 127.0.0.1:6379> zunionstore zkey3 2 zkey1 zkey2 (integer) 4 127.0.0.1:6379> zrange zkey3 0 -1 withscores 1) "a" 2) "100" 3) "c" 4) "300" 5) "e" 6) "500" 7) "b" 8) "600"
20>zscan
方法:ZSCAN key cursor [MATCH pattern] [COUNT count]
描述:迭代有序集合中的元素(包括元素成員和元素分值)
127.0.0.1:6379> zrange zkey 0 -1 withscores 1) "No_s\x94ql" 2) "1" 3) "abc" 4) "1" 5) "ao" 6) "1" 7) "apche" 8) "1" 9) "MySQL" 10) "1" 11) "nginx" 12) "1" 127.0.0.1:6379> zscan zkey 0 match a* count 1 1) "0" 2) 1) "abc" 2) "1" 3) "ao" 4) "1" 5) "apche" 6) "1"
至此Redis sorted set有序集合就介紹完畢了!
歡迎大家加入 459479177QQ群進行交流