zset命令
2021-07-05 本文已影响0人
lenny611
zset,排序好的set,默认按分值从小到大排序。
127.0.0.1:6379> zadd fruts 8 apple 2 banana 3 orange
(integer) 3
127.0.0.1:6379> ZRANGE fruts 0 -1
1) "banana"
2) "orange"
3) "apple"
排名情况
127.0.0.1:6379> ZRANK fruts apple
(integer) 2
127.0.0.1:6379> ZRANK fruts banana
(integer) 0
127.0.0.1:6379> ZRANK fruts orange
(integer) 1
可以通过ZRANGE
命令按照顺序排名取数,ZRANGEBYSCORE
按照分值范围取数,ZREVRANGE
是倒序排名取数。
127.0.0.1:6379> ZRANGEBYSCORE fruts 3 9
1) "orange"
2) "apple"
127.0.0.1:6379> ZRANGEBYSCORE fruts 4 9
1) "apple"
127.0.0.1:6379> ZRANGE fruts 0 1
1) "banana"
2) "orange"
127.0.0.1:6379> ZrevRANGE fruts 0 1
1) "apple"
2) "orange"
通过ZINCRBY
修改banana的分值,排名自动调整
127.0.0.1:6379> ZINCRBY fruts 2 banana
"4"
127.0.0.1:6379> ZRANGE fruts 0 -1
1) "orange"
2) "banana"
3) "apple"
127.0.0.1:6379> ZSCORE fruts banana
"4"