Redis Zset有序集合

在set的基础上,增加了一个值,set  k1 v1      zset k1  score v1

1.利用zadd 命令添加一个值

127.0.0.1:6379> zadd myset 1 one
(integer) 1

 

 2.利用zadd命令添加多个值

127.0.0.1:6379> zadd myset 2 two 3 three
(integer) 2
127.0.0.1:6379>    

 

 3.利用zrange key start stop 名利查看集合

127.0.0.1:6379> zrange myset 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379>                                                                                              

 

 4.利用zrangebyscore 命令排序

127.0.0.1:6379> zadd salary 2500 xiaohong
(integer) 1
127.0.0.1:6379> zadd salary 5000 zhangsan
(integer) 1
127.0.0.1:6379> zadd salary 500 ckfuture
(integer) 1
127.0.0.1:6379> zrangebyscore salary -inf +inf
1) "ckfuture"
2) "xiaohong"
3) "zhangsan"
127.0.0.1:6379>   

 

 -inf:负无穷

+inf:正无穷

127.0.0.1:6379> zrangebyscore salary -inf +inf withscores
1) "ckfuture"
2) "500"
3) "xiaohong"
4) "2500"
5) "zhangsan"
6) "5000"
127.0.0.1:6379>   

 

 5.利用zrem命令移除有序集合中的指定元素

127.0.0.1:6379> zrem salary xiaohong
(integer) 1
127.0.0.1:6379>   

 

 6.利用zcrad 命令获取有序集合中元素的个数

127.0.0.1:6379> zcard salary
(integer) 2
127.0.0.1:6379>    

 

 7.利用zrevrange 命令实现从大到小排序

127.0.0.1:6379> zrevrange salary 0 -1 withscores
1) "zhangsan"
2) "5000"
3) "ckfuture"
4) "500"
127.0.0.1:6379>  

 

 8.利用zcount 命令获取区间的成员数量

127.0.0.1:6379> zcount myset 1 3
(integer) 6

应用场景:

存储班级成绩、工资表,

带权重进行判断:例如:普通消息 1,重要消息2

排行榜应用实现:top n 测试

 

posted @ 2021-02-10 14:42  创客未来  阅读(302)  评论(0)    收藏  举报