ZADD:將元素及其分數添加到集合中app
語法:ZADD key courseScore member [courseScore member]編碼
ZADD courseScore 100 tomes5
ZADD courseScore 99 jerry 88 mario 77 jack 66 lucy 55 chrisspa
ZADD courseScore 60 tom 67 apple 56 cherry #這裏 tom 已經存在了,直接略過不執行,返回值爲 2 #code
ZADD courseScore 12.3 lenovoblog
ZADD courseScore +inf maxInt -inf minInt排序
ZSCORE courseScore maxInt索引
ZSCORE courseScore minIntrem
127.0.0.1:6379> ZADD courseScore 100 tom (integer) 1 127.0.0.1:6379> ZADD courseScore 99 jerry 88 mario 77 jack 66 lucy 55 chris (integer) 5 127.0.0.1:6379> ZADD courseScore 60 tom 67 apple 56 cherry (integer) 2 127.0.0.1:6379> ZADD courseScore 12.3 lenovo (integer) 1 127.0.0.1:6379> ZADD courseScore +inf maxInt -inf minInt (integer) 1 127.0.0.1:6379> ZSCORE courseScore maxInt "inf" 127.0.0.1:6379> ZSCORE courseScore minInt "-inf"
ZSCORE:得到指定元素的分數io
語法:ZSCORE key member
ZSCORE courseScore mario
ZSCORE courseScore lenovo
127.0.0.1:6379> ZSCORE courseScore mario "88" 127.0.0.1:6379> ZSCORE courseScore lenovo "12.300000000000001"
ZRANGE:按照元素分數從小到大的順序返回指定索引 start 到 stop 之間全部元素(包含兩端)
語法:ZRANGE key start stop [WITHSCORES]
ZRANGE courseScore 0 -1
ZRANGE courseScore 0 -1 WITHSCORES
ZRANGE courseScore 0 2 WITHSCORES
ZRANGE courseScore 0 2000 WITHSCORES
ZRANGE courseScore 1000 2000 WITHSCORES
ZADD courseScore 60 test1 60 test2 60 test3 60 test4
ZRANGE courseScore 0 -1 WITHSCORES
#當兩個元素的分數相同的時候,Redis 在排序按照字典的順序 (0<9<A<Z<a<z) ,若是使用的是 UTF-8 的編碼方式的中文一樣按照字典順序排列#
127.0.0.1:6379> ZRANGE courseScore 0 -1 1) "minInt" 2) "minInx" 3) "lenovo" 4) "chris" 5) "cherry" 6) "tom" 7) "lucy" 8) "apple" 9) "jack" 10) "mario" 11) "jerry" 12) "maxInt" 127.0.0.1:6379> ZRANGE courseScore 0 -1 WITHSCORES 1) "minInt" 2) "-inf" 3) "minInx" 4) "-inf" 5) "lenovo" 6) "12.300000000000001" 7) "chris" 8) "55" 9) "cherry" 10) "56" 11) "tom" 12) "60" 13) "lucy" 14) "66" 15) "apple" 16) "67" 17) "jack" 18) "77" 19) "mario" 20) "88" 21) "jerry" 22) "99" 23) "maxInt" 24) "inf" 127.0.0.1:6379> ZRANGE courseScore 0 2 WITHSCORES 1) "minInt" 2) "-inf" 3) "minInx" 4) "-inf" 5) "lenovo" 6) "12.300000000000001" 127.0.0.1:6379> ZRANGE courseScore 0 2000 WITHSCORES 1) "minInt" 2) "-inf" 3) "minInx" 4) "-inf" 5) "lenovo" 6) "12.300000000000001" 7) "chris" 8) "55" 9) "cherry" 10) "56" 11) "tom" 12) "60" 13) "lucy" 14) "66" 15) "apple" 16) "67" 17) "jack" 18) "77" 19) "mario" 20) "88" 21) "jerry" 22) "99" 23) "maxInt" 24) "inf" 127.0.0.1:6379> ZRANGE courseScore 1000 2000 WITHSCORES (empty list or set) 127.0.0.1:6379> ZADD courseScore 60 test1 60 test2 60 test3 60 test4 (integer) 4 127.0.0.1:6379> ZRANGE courseScore 0 -1 WITHSCORES 1) "minInt" 2) "-inf" 3) "minInx" 4) "-inf" 5) "lenovo" 6) "12.300000000000001" 7) "chris" 8) "55" 9) "cherry" 10) "56" 11) "test1" 12) "60" 13) "test2" 14) "60" 15) "test3" 16) "60" 17) "test4" 18) "60" 19) "tom" 20) "60" 21) "lucy" 22) "66" 23) "apple" 24) "67" 25) "jack" 26) "77" 27) "mario" 28) "88" 29) "jerry" 30) "99" 31) "maxInt" 32) "inf"
ZREVRANGE:和 ZRANGE 相反,按照分數從大到小的順序
語法:ZREVRANGE key start stop [WITHSCORES]
ZREVRANGE courseScore 0 -1 WITHSCORES
127.0.0.1:6379> ZREVRANGE courseScore 0 -1 WITHSCORES 1) "maxInt" 2) "inf" 3) "jerry" 4) "99" 5) "mario" 6) "88" 7) "jack" 8) "77" 9) "apple" 10) "67" 11) "lucy" 12) "66" 13) "tom" 14) "60" 15) "test4" 16) "60" 17) "test3" 18) "60" 19) "test2" 20) "60" 21) "test1" 22) "60" 23) "cherry" 24) "56" 25) "chris" 26) "55" 27) "lenovo" 28) "12.300000000000001" 29) "minInx" 30) "-inf" 31) "minInt" 32) "-inf"
ZRANGEBYSCORE:得到指定分數範圍內的元素,按照分數從小到大的順序,返回的是分數在指定的 min 到 max 的元素
語法:ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
ZRANGEBYSCORE courseScore 80 90 #得到分數80~90之間的全部元素
ZRANGEBYSCORE courseScore 60 90 WITHSCORES #得到分數60~90之間的全部元素
ZRANGEBYSCORE courseScore 60 (88 WITHSCORES
ZRANGEBYSCORE courseScore (60 (88 WITHSCORES #經過 '(' 表明不包端點#
ZRANGEBYSCORE courseScore 90 +inf WITHSCORES
ZRANGEBYSCORE courseScore 80 +inf WITHSCORES LIMIT 0 3
ZRANGEBYSCORE courseScore 60 +inf WITHSCORES LIMIT 3 3
127.0.0.1:6379> ZRANGEBYSCORE courseScore 80 90 1) "mario" 127.0.0.1:6379> ZRANGEBYSCORE courseScore 60 90 WITHSCORES 1) "test1" 2) "60" 3) "test2" 4) "60" 5) "test3" 6) "60" 7) "test4" 8) "60" 9) "tom" 10) "60" 11) "lucy" 12) "66" 13) "apple" 14) "67" 15) "jack" 16) "77" 17) "mario" 18) "88" 127.0.0.1:6379> ZRANGEBYSCORE courseScore 60 (88 WITHSCORES 1) "test1" 2) "60" 3) "test2" 4) "60" 5) "test3" 6) "60" 7) "test4" 8) "60" 9) "tom" 10) "60" 11) "lucy" 12) "66" 13) "apple" 14) "67" 15) "jack" 16) "77" 127.0.0.1:6379> ZRANGEBYSCORE courseScore (60 (88 WITHSCORES 1) "lucy" 2) "66" 3) "apple" 4) "67" 5) "jack" 6) "77" 127.0.0.1:6379> ZRANGEBYSCORE courseScore 90 +inf WITHSCORES 1) "jerry" 2) "99" 3) "maxInt" 4) "inf" 127.0.0.1:6379> ZRANGEBYSCORE courseScore 80 +inf WITHSCORES LIMIT 0 3 1) "mario" 2) "88" 3) "jerry" 4) "99" 5) "maxInt" 6) "inf" 127.0.0.1:6379> ZRANGEBYSCORE courseScore 60 +inf WITHSCORES LIMIT 3 3 1) "test4" 2) "60" 3) "tom" 4) "60" 5) "lucy" 6) "66"
ZREVRANGEBYSCORE:得到指定分數範圍內的元素,按照元素的分數從大到小的順序返回 min 和 max 之間的元素
語法:ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
ZREVRANGEBYSCORE courseScore 90 60 WITHSCORES
127.0.0.1:6379> ZREVRANGEBYSCORE courseScore 90 60 WITHSCORES 1) "mario" 2) "88" 3) "jack" 4) "77" 5) "apple" 6) "67" 7) "lucy" 8) "66" 9) "tom" 10) "60" 11) "test4" 12) "60" 13) "test3" 14) "60" 15) "test2" 16) "60" 17) "test1" 18) "60"
ZINCRBY:操做某個元素的分數,返回操做以後的分數
語法:ZINCRBY key increment member
ZINCRBY courseScore 5 tom
ZINCRBY courseScore -15 tom
127.0.0.1:6379> ZINCRBY courseScore 5 tom "65" 127.0.0.1:6379> ZINCRBY courseScore -15 tom "50"
ZCARD:得到集合中元素的數量
語法:ZCARD key
ZCARD courseScore
127.0.0.1:6379> ZCARD courseScore (integer) 16
ZCONUT:得到指定分數內的元素的數量
語法:ZCOUNT key min max
ZCOUNT courseScore 80 90
127.0.0.1:6379> ZCOUNT courseScore 80 90 (integer) 1
ZREM:刪除一個或者多個元素,返回刪除元素的個數
語法:ZREM key member ...
ZREM courseScore tom
127.0.0.1:6379> ZREM courseScore tom (integer) 1
ZREMRANGEBYRANK:按照排名範圍刪除元素,按照分數從小到大的順序刪除所指定的排名範圍內的全部元素
語法:ZREMRANGEBYRANK key start stop
ZREMRANGEBYRANK courseScore 0 5
ZRANGE courseScore 0 -1 WITHSCORES
127.0.0.1:6379> ZREMRANGEBYRANK courseScore 0 5 (integer) 6 127.0.0.1:6379> ZRANGE courseScore 0 -1 WITHSCORES 1) "test4" 2) "60" 3) "lucy" 4) "66" 5) "apple" 6) "67" 7) "jack" 8) "77" 9) "mario" 10) "88" 11) "jerry" 12) "99" 13) "tom" 14) "100" 15) "maxInt" 16) "inf"
ZREMRANGEBYSCORE:按照分數範圍刪除元素
語法:ZREMRANGEBYSCORE key min max
ZREMRANGEBYSCORE courseScore 60 70
ZRANGE courseScore 0 -1 WITHSCORES
127.0.0.1:6379> ZREMRANGEBYSCORE courseScore 60 70 (integer) 3 127.0.0.1:6379> ZRANGE courseScore 0 -1 WITHSCORES 1) "jack" 2) "77" 3) "mario" 4) "88" 5) "jerry" 6) "99" 7) "tom" 8) "100" 9) "maxInt" 10) "inf"
ZRANK:得到指定元素的排名,根據分數從小到大的順序
語法:ZRANK key member
ZRANK courseScore tom
127.0.0.1:6379> ZRANK courseScore tom (integer) 3
ZREVRANK:得到指定元素的排名,根據分數從大到小的順序
語法:ZREVRANK key member
ZREVRANK courseScore tom
127.0.0.1:6379> ZREVRANK courseScore tom (integer) 1
ZINTERSTORE:計算有序集合的交集,並將結果保存起來
語法:ZINTERSTORE destination numkeys [WEIGHTS weight weight...] [AGGREGATE SUM | MIN | MAX]
ZADD testSortedSet1 1 a 2 b 3 c
ZADD testSortedSet2 10 a 20 b 30 c
ZINTERSTORE resTestSorted1 2 testSortedSet1 testSortedSet2
ZRANGE resTestSorted1 0 -1 WITHSCORES
ZINTERSTORE resTestSorted2 2 testSortedSet1 testSortedSet2 AGGREGATE SUM #返回多個集合中的分數的和#
ZRANGE resTestSorted2 0 -1 WITHSCORES # 1+10 2+20 3+30 #
ZINTERSTORE resTestSorted3 2 testSortedSet1 testSortedSet2 AGGREGATE MIN #返回多個集合中分數最小的值#
ZRANGE resTestSorted3 0 -1 WITHSCORES # 1 2 3 #
ZINTERSTORE resTestSorted4 2 testSortedSet1 testSortedSet2 AGGREGATE MAX #返回多個集合中分數最小的值#
ZRANGE resTestSorted4 0 -1 WITHSCORES # 10 20 30 #
ZINTERSTORE resTestSorted5 2 testSortedSet1 testSortedSet2 WEIGHTS 2 0.2
#返回第一個集合分數的 2 倍,第二個集合分數的 0.2倍,這裏第三個參數沒寫默認使用 SUM 求兩次重賦值的和#
ZRANGE resTestSorted5 0 -1 WITHSCORES # 1x2+10x0.2 2x2+20x0.2 3x2+30x0.2 #
127.0.0.1:6379> ZADD testSortedSet1 1 a 2 b 3 c (integer) 3 127.0.0.1:6379> ZADD testSortedSet2 10 a 20 b 30 c (integer) 3 127.0.0.1:6379> ZINTERSTORE resTestSorted1 2 testSortedSet1 testSortedSet2 (integer) 3 127.0.0.1:6379> ZRANGE resTestSorted1 0 -1 1) "a" 2) "b" 3) "c" 127.0.0.1:6379> ZRANGE resTestSorted1 0 -1 WITHSCORES 1) "a" 2) "11" 3) "b" 4) "22" 5) "c" 6) "33" 127.0.0.1:6379> ZINTERSTORE resTestSorted2 2 testSortedSet2 testSortedSet2 AGGREGATE SUM (integer) 3 127.0.0.1:6379> ZRANGE resTestSorted2 0 -1 WITHSCORES 1) "a" 2) "20" 3) "b" 4) "40" 5) "c" 6) "60" 127.0.0.1:6379> ZINTERSTORE resTestSorted2 2 testSortedSet1 testSortedSet2 AGGREGATE SUM (integer) 3 127.0.0.1:6379> ZRANGE resTestSorted2 0 -1 WITHSCORES 1) "a" 2) "11" 3) "b" 4) "22" 5) "c" 6) "33" 127.0.0.1:6379> ZINTERSTORE resTestSorted3 2 testSortedSet1 testSortedSet2 AGGREGATE MIN (integer) 3 127.0.0.1:6379> ZRANGE resTestSorted3 0 -1 WITHSCORES 1) "a" 2) "1" 3) "b" 4) "2" 5) "c" 6) "3" 127.0.0.1:6379> ZINTERSTORE resTestSorted4 2 testSortedSet1 testSortedSet2 AGGREGATE MAX (integer) 3 127.0.0.1:6379> ZRANGE resTestSorted4 0 -1 WITHSCORES 1) "a" 2) "10" 3) "b" 4) "20" 5) "c" 6) "30" 127.0.0.1:6379> ZINTERSTORE resTestSorted5 2 testSortedSet1 testSortedSet2 WEIGHTS 2 0.2 (integer) 3 127.0.0.1:6379> ZRANGE resTestSorted5 0 -1 WITHSCORES 1) "a" 2) "4" 3) "b" 4) "8" 5) "c" 6) "12"
ZUNIONSTORE:計算有序集合並集,將結果保存起來
語法:ZUNIONSTORE destination numkeys key key ... [WEIGHTS weight weight...] [AGGREGATE SUM | MIN | MAX]
ZADD TESTUNIONSET1 1 a 2 b 3 c
ZADD TESTUNIONSET2 4 d 5 e 6 f 7 a
ZUNIONSTORE uniRes1 2 TESTUNIONSET1 TESTUNIONSET2 #返回多個集合的交際,這裏第二個參數沒寫使用默認值 SUM #
ZRANGE uniRes1 0 -1 WITHSCORES # a=1+7 b=2 c=3 d=4 e=5 f=6 #
ZUNIONSTORE uniRes2 2 TESTUNIONSET1 TESTUNIONSET2 AGGREGATE SUM #返回多個集合的交際 #
ZRANGE uniRes2 0 -1 WITHSCORES # a=1+7 b=2 c=3 d=4 e=5 f=6 #
ZUNIONSTORE uniRes3 2 TESTUNIONSET1 TESTUNIONSET2 AGGREGATE MIN
ZRANGE uniRes3 0 -1 WITHSCORES # a=1 b=2 c=3 d=4 e=5 f=6 #
ZUNIONSTORE uniRes4 2 TESTUNIONSET1 TESTUNIONSET2 AGGREGATE MAX
ZRANGE uniRes4 0 -1 WITHSCORES # a=7 b=2 c=3 d=4 e=5 f=6 #
ZUNIONSTORE uniRes5 2 TESTUNIONSET1 TESTUNIONSET2 WEIGHTS 2 2
ZRANGE uniRes5 0 -1 WITHSCORES # a=1x2+7x2 b=2x2 c=3x2 d=4x2 e=5x2 f=6x2 #
127.0.0.1:6379> ZADD TESTUNIONSET1 1 a 2 b 3 c (integer) 3 127.0.0.1:6379> ZADD TESTUNIONSET2 4 d 5 e 6 f 7 a (integer) 4 127.0.0.1:6379> ZUNIONSTORE uniRes1 2 TESTUNIONSET1 TESTUNIONSET2 (integer) 6 127.0.0.1:6379> ZRANGE uniRes1 0 -1 WITHSCORES 1) "b" 2) "2" 3) "c" 4) "3" 5) "d" 6) "4" 7) "e" 8) "5" 9) "f" 10) "6" 11) "a" 12) "8" 127.0.0.1:6379> ZUNIONSTORE uniRes2 2 TESTUNIONSET1 TESTUNIONSET2 AGGREGATE SUM (integer) 6 127.0.0.1:6379> ZRANGE uniRes2 0 -1 WITHSCORES 1) "b" 2) "2" 3) "c" 4) "3" 5) "d" 6) "4" 7) "e" 8) "5" 9) "f" 10) "6" 11) "a" 12) "8" 127.0.0.1:6379> ZUNIONSTORE uniRes3 2 TESTUNIONSET1 TESTUNIONSET2 AGGREGATE MIN (integer) 6 127.0.0.1:6379> ZRANGE uniRes3 0 -1 WITHSCORES 1) "a" 2) "1" 3) "b" 4) "2" 5) "c" 6) "3" 7) "d" 8) "4" 9) "e" 10) "5" 11) "f" 12) "6" 127.0.0.1:6379> ZUNIONSTORE uniRes4 2 TESTUNIONSET1 TESTUNIONSET2 AGGREGATE MAX (integer) 6 127.0.0.1:6379> ZRANGE uniRes4 0 -1 WITHSCORES 1) "b" 2) "2" 3) "c" 4) "3" 5) "d" 6) "4" 7) "e" 8) "5" 9) "f" 10) "6" 11) "a" 12) "7" 127.0.0.1:6379> ZUNIONSTORE uniRes5 2 TESTUNIONSET1 TESTUNIONSET2 WEIGHTS 2 2 (integer) 6 127.0.0.1:6379> ZRANGE uniRes5 0 -1 WITHSCORES 1) "b" 2) "4" 3) "c" 4) "6" 5) "d" 6) "8" 7) "e" 8) "10" 9) "f" 10) "12" 11) "a" 12) "16"