Redis基礎系列-0x007:Sorted Set

followWinter發表於2018-05-17

0x001 新增元素

命令格式:ZADD score member [score member ...]

127.0.0.1:6379> ZADD star 100 game1 200 game2 300 game3  
(integer) 3

0x002 檢視成員的數量

命令格式:ZCARD key

127.0.0.1:6379> ZCARD star
(integer) 3

0x003 檢視某一區間分數的成員數量

命令格式:ZCOUNT key min max

127.0.0.1:6379> ZCOUNT star 1 200
(integer) 2

0x004 檢視某一區間索引的數量

命令格式:ZLEXCOUNT key min max

127.0.0.1:6379> ZLEXCOUNT star (game1 (game3
(integer) 1
127.0.0.1:6379> ZLEXCOUNT star [game1 [game3
(integer) 3
127.0.0.1:6379> ZLEXCOUNT star - [game3
(integer) 3

0x005 檢視某一元素的索引

命令格式:ZRANK key member

127.0.0.1:6379> ZRANK star game1
(integer) 0

0x006 獲取某個索引區間的值

命令格式:ZRANK key start stop [WITHSCORES]

127.0.0.1:6379> ZRANGE star 0 4 WITHSCORES
1) "game1"
2) "100"
3) "game2"
4) "200"
5) "game3"
6) "300"

0x007 獲取某個分數區間的值

命令格式:ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

127.0.0.1:6379> ZRANGEBYSCORE star 0 201 WITHSCORES
1) "game1"
2) "100"
3) "game2"
4) "200"

0x008 獲取某個索引區間的值

命令格式:ZRANGEBYLEX key min max [LIMIT offset count]

127.0.0.1:6379> ZRANGEBYLEX star (game1 (game3
1) "game2"

0x009 獲取某個元素的分數

命令格式:ZSCORE key member

127.0.0.1:6379> ZSCORE star game1
"100"

0x010 移除指定元素

命令格式:ZREM key member [member member ...]

127.0.0.1:6379> ZREM star game1 game2
(integer) 2
127.0.0.1:6379> ZRANGE star 0 100
1) "game3"

0x011 根據索引刪除元素

命令格式:ZREMRANGEBYLEX key min max

127.0.0.1:6379> DEL star
(integer) 1
127.0.0.1:6379> ZADD star 100 game1 200 game2 300 game3
(integer) 3
127.0.0.1:6379> ZREMRANGEBYLEX star game1 game2
(error) ERR min or max not valid string range item
127.0.0.1:6379> ZREMRANGEBYLEX star [game1 [game2
(integer) 2
127.0.0.1:6379> ZRANGE star 0 100
1) "game3"

0x013 根據分數刪除元素

命令格式:ZREMRANGEBYSCORE key min max

127.0.0.1:6379> DEL star
(integer) 1
127.0.0.1:6379> ZADD star 100 game1 200 game2 300 game3
(integer) 3
127.0.0.1:6379> ZREMRANGEBYSCORE star 0 201
(integer) 2
127.0.0.1:6379> ZRANGE star 0 100
1) "game3"

0x014 根據排行分數元素

命令格式:ZREMRANGEBYSCORE key star stop

127.0.0.1:6379> DEL star
(integer) 1
127.0.0.1:6379> ZADD star 100 game1 200 game2 300 game3
(integer) 3
127.0.0.1:6379> ZREMRANGEBYRANK star 1 2
(integer) 2
127.0.0.1:6379> ZRANGE star 0 100
1) "game1"

0x015 根據索引獲取元素,按分數從高到底

命令格式:ZREVRANGE key star stop [WITHSCORES]

127.0.0.1:6379> ZREVRANGE star 0 100 WITHSCORES
1) "game3"
2) "300"
3) "game2"
4) "200"
5) "game1"
6) "100"

0x016 根據分數區間獲取元素,按分數從高到底

命令格式:ZREVRANGE key max min [WITHSCORES] [LIMIT offset count]

127.0.0.1:6379> ZREVRANGEBYSCORE star 201 0 WITHSCORES
1) "game2"
2) "200"
3) "game1"
4) "100"

0x016 返回元素的排行

命令格式:ZREVRANK key member

127.0.0.1:6379> ZREVRANK star game2 
(integer) 1

0x017 增加元素的分數

命令格式:ZLEXCOUNT key min max

127.0.0.1:6379> ZINCRBY star 10 game2
"210"
127.0.0.1:6379> ZSCORE star game2
"210"

相關文章