Redis有序集合Zset(sorted set)

zadd/zrange

127.0.0.1:6379> zadd zset01 60 v1 70 v2 80 v3 90 v4 100 v5
(integer) 5
127.0.0.1:6379> ZRANGE zset01 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
5) "v5"
127.0.0.1:6379> ZRANGE zset01 0 -1 withscores
1) "v1"
2) "60"
3) "v2"
4) "70"
5) "v3"
6) "80"
7) "v4"
8) "90"
9) "v5"
10) "100"
127.0.0.1:6379>

 

 

zrangebyscore key 开始score 结束score   (获取一个范围内的)

127.0.0.1:6379> ZRANGEBYSCORE zset01 60 90  (>=60,<=90)
1) "v1"
2) "v2"
3) "v3"
4) "v4"
127.0.0.1:6379>

包含和不包含

127.0.0.1:6379> ZRANGEBYSCORE zset01 60 (90  (>=60,<90)
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379>

 

127.0.0.1:6379> ZRANGEBYSCORE zset01 (60 (90  (>60,<90)
1) "v2"
2) "v3"
127.0.0.1:6379>

 

在结果集中在截取

127.0.0.1:6379> ZRANGEBYSCORE zset01 60 90
1) "v1"
2) "v2"
3) "v3"
4) "v4"
127.0.0.1:6379> ZRANGEBYSCORE zset01 60 90 limit 2 2
1) "v3"
2) "v4"
127.0.0.1:6379>

 

zrem key value   (删除一个)

127.0.0.1:6379> zrem zset01 v5
(integer) 1
127.0.0.1:6379> zrange zset01 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
127.0.0.1:6379>

 

 

zcard/zcount key score区间/zrank key values值,作用是获得下标值/zscore key 对应值,获得分数

127.0.0.1:6379> zcard zset01  //获取个数
(integer) 4
127.0.0.1:6379>

 

获取符合范围内的个数

127.0.0.1:6379> ZCOUNT zset01 60 80
(integer) 3
127.0.0.1:6379>

 

获取对应的下标

127.0.0.1:6379> ZRANK zset01 v4
(integer) 3

获取对应的分数值
127.0.0.1:6379> ZSCORE zset01 v4
"90"
127.0.0.1:6379>

 

逆序获得下标值

127.0.0.1:6379> ZREVRANK zset01 v4
(integer) 0

逆序打印
127.0.0.1:6379> zrevrange zset01 0 -1
1) "v4"
2) "v3"
3) "v2"
4) "v1"
127.0.0.1:6379>

 

ZREVRANGEBYSCORE key 结束 开始

127.0.0.1:6379> ZREVRANGEBYSCORE zset01 90 60
1) "v4"
2) "v3"
3) "v2"
4) "v1"
127.0.0.1:6379>

 

posted @ 2016-11-19 02:10  wq3435  阅读(1946)  评论(0编辑  收藏  举报