Zset(有序集合)
排序
127.0.0.1:6379> zadd salary 3000 zhansan # 添加数据
(integer) 1
127.0.0.1:6379> zadd salary 5000 lisi
(integer) 1
127.0.0.1:6379> zadd salary 2500 wangwu
(integer) 1
127.0.0.1:6379> zrangebyscore salary -inf +inf # 从小到大排序,只显示用户的名字
1) "wangwu"
2) "zhansan"
3) "lisi"
127.0.0.1:6379> zrevrange salary 0 -1 # 从大到小排序
1) "lisi"
2) "zhangsan"
3) "wangwu"
127.0.0.1:6379> zrangebyscore salary -inf +inf withscores # 从小到大排序,显示用户名和薪水
1) "wangwu"
2) "2500"
3) "zhansan"
4) "3000"
5) "lisi"
6) "5000"
127.0.0.1:6379> zrangebyscore salary -inf 3000 withscores # 从小到大排序,只显示3000以下的
1) "wangwu"
2) "2500"
3) "zhansan"
4) "3000"
计算集合中元素的个数与移除集合中的元素
127.0.0.1:6379> zcard salary # 计算集合中元素的个数
(integer) 3
127.0.0.1:6379> zrem salary zhansan # 移除集合中的zhangsan
(integer) 1
127.0.0.1:6379> zrange salary 0 -1
1) "wangwu"
2) "lisi"
统计某个区间的个数
127.0.0.1:6379> zadd salary 1000 zhangsan 1500 lisi 2000 wangwu 3000 zkw
(integer) 4
127.0.0.1:6379> zcount salary 1000 2000 # 统计薪水在1000~2000之间的个数
(integer) 3
127.0.0.1:6379> zrangebyscore salary -inf +inf withscores
1) "zhangsan"
2) "1000"
3) "lisi"
4) "1500"
5) "wangwu"
6) "2000"
7) "zkw"
8) "3000"