ZSet类型
zset是有序set集合, 专门用来做数据排序。
- 添加元素:zadd key [NX|XX] [CH] [INCR] score member [score member …]
127.0.0.1:6379> zadd z1 80 math 70 english 90 chinese (integer) 3
- 查询指定成员的分数:zscore key member
127.0.0.1:6379> zscore z1 math "80"
- 删除集合中指定的成员:zrem key member [member …]
127.0.0.1:6379> zrem z1 math english (integer) 2
- 范围查询:zrange key start stop [WITHSCORES]
127.0.0.1:6379> zrange z1 0 -1 withscores 1) "english" 2) "70" 3) "math" 4) "80" 5) "chinese" 6) "90"
- 获取倒序结果:zrevrange key start stop [WITHSCORES]
127.0.0.1:6379> zrevrange z1 0 -1 withscores 1) "chinese" 2) "90" 3) "math" 4) "80" 5) "english" 6) "70"
- 按分数范围删除元素:zremrangebyscore key min max
127.0.0.1:6379> zremrangebyscore z1 75 85 (integer) 1
- 在指定的分数范围之内进行排序:zrangebyscore key min max [WITHSCORES] [LIMIT offset count]
127.0.0.1:6379> zrangebyscore z1 65 95 withscores 1) "english" 2) "70" 3) "chinese" 4) "90"
- 返回成员在集合中的排名:zrank key member
127.0.0.1:6379> zrank z1 english (integer) 0 127.0.0.1:6379> zrank z1 math (integer) 1
- 反转排名:zrevrank key member
127.0.0.1:6379> zrevrank z1 english (integer) 2 127.0.0.1:6379> zrevrank z1 math (integer) 1
- 给指定的成员增加指定的分数,返回增加以后的分数:zincrby key increment member
127.0.0.1:6379> zincrby z1 4 math "84"
- 获取分数在[min, max]之间的成员的个数:zcount key min max
127.0.0.1:6379> zcount z1 65 85 (integer) 2
- 按排名范围删除元素:zremrangebyrank key start stop
127.0.0.1:6379> zremrangebyrank z1 0 1 (integer) 2 127.0.0.1:6379> zrange z1 0 -1 withscores 1) "chinese" 2) "90"
- 获取集合数据总量:zcard key
127.0.0.1:6379> zcard z1 (integer) 1
- zinterstore destination numkeys key [key …] [WEIGHTS weight] [AGGREGATE SUM|MI
127.0.0.1:6379> zadd z2 85 sport 75 chinese 90 math (integer) 3 127.0.0.1:6379> zadd z1 80 math 90 chinese 60 english (integer) 3 127.0.0.1:6379> zinterstore zz1 2 z1 z2 (integer) 2 127.0.0.1:6379> zrange zz1 0 -1 1) "chinese" 2) "math"
- zunionstore destination numkeys key [key …] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
127.0.0.1:6379> zunionstore zz2 2 z1 z2 (integer) 4 127.0.0.1:6379> zrange zz2 0 -1 1) "english" 2) "sport" 3) "chinese" 4) "math"