Zset(有序集合)
在set的基础上,增加了一个值,set k1 v1, zset k1 score v1。所有命令都是以z开头
zadd key score value … 给key集合添加一个值,score是排序 ,还能同时添加多个值
zrangebyscore key min max withscores : 根据key集合中的value根据min到max进行排序,withscores将score显示出来
zrem key member : 将key集合中的member(元素)移除
zcard key :查看key集合的大小
zcount key min max :从key集合中找出min到max中包含的member元素个数
zrevrange key start stop withscores :将key集合倒序排列,从大到小排序
###############################################
127.0.0.1:6379> zadd set 1 one
(integer) 1
127.0.0.1:6379> zadd set 2 two
(integer) 1
127.0.0.1:6379> zadd set 3 three
(integer) 1
127.0.0.1:6379> zadd set 4 four 5 five
(integer) 2
127.0.0.1:6379> zrange set 0 -1
1) "one"
2) "two"
3) "three"
4) "four"
5) "five"
#####################################################
127.0.0.1:6379> zadd salary 2500 zhangsan 4000 lisi 500 wangwu
(integer) 3
127.0.0.1:6379> zrange salary 0 -1
1) "wangwu"
2) "zhangsan"
3) "lisi"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf ## 从小到大排序
1) "wangwu"
2) "zhangsan"
3) "lisi"
127.0.0.1:6379> zrangebyscore salary -inf +inf withscores
1) "wangwu"
2) "500"
3) "zhangsan"
4) "2500"
5) "lisi"
6) "4000"
127.0.0.1:6379> zrangebyscore salary -inf 2500 withscores
1) "wangwu"
2) "500"
3) "zhangsan"
4) "2500"
#################################################
127.0.0.1:6379> zrem salary lisi
(integer) 1
127.0.0.1:6379> zrange salary 0 -1
1) "wangwu"
2) "zhangsan"
127.0.0.1:6379> zcard salary
(integer) 2
##########################################
127.0.0.1:6379> zcount salary 1 10000
(integer) 2
127.0.0.1:6379> zrange salary 0 -1 withscores
1) "wangwu"
2) "500"
3) "zhangsan"
4) "2500"
########################################
127.0.0.1:6379> zadd salary 2000 zhangsan 3400 lisi 1200 wangwu
(integer) 3
127.0.0.1:6379> zrevrange salary 0 -1 withscores
1) "lisi"
2) "3400"
3) "zhangsan"
4) "2000"
5) "wangwu"
6) "1200"
案例:set排序,存储班级成绩、工资表排序!