Redis五大数据类型之 Zset(有序集合)

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

127.0.0.1:6379[1]> ZADD myset 1 one    # 添加一个值
(integer) 1
127.0.0.1:6379[1]> ZADD myset 2 two 3 three    # 添加多个值
(integer) 2
127.0.0.1:6379[1]> ZRANGE myset 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379[1]> ZADD salary 2500 tom    # 添加三个用户
(integer) 1
127.0.0.1:6379[1]> ZADD salary 5000 lucy
(integer) 1
127.0.0.1:6379[1]> ZADD salary 2000 alex
(integer) 1
127.0.0.1:6379[1]> ZRANGEBYSCORE salary -inf +inf    # 显示所有用户 从下到大
1) "alex"
2) "tom"
3) "lucy"
127.0.0.1:6379[1]> ZRANGEBYSCORE salary -inf +inf withscores    # 显示所有用户 从下到大 并且附带成绩
1) "alex"
2) "2000"
3) "tom"
4) "2500"
5) "lucy"
6) "5000"
127.0.0.1:6379[1]> ZRANGEBYSCORE salary -inf 2500 withscores
1) "alex"
2) "2000"
3) "tom"
4) "2500"
127.0.0.1:6379[1]> ZREVRANGEBYSCORE salary +inf -inf withscores    # 显示工资小于等于2500的员工附带成绩,并且降序排序
1) "lucy"
2) "5000"
3) "tom"
4) "2500"
5) "alex"
6) "2000"
127.0.0.1:6379[1]> ZREVRANGEBYSCORE salary +inf (2500 withscores    # 显示工资小于2500的员工附带成绩,并且降序排序
1) "lucy"
2) "5000"
127.0.0.1:6379[1]> ZRANGE salary 0 -1
1) "alex"
2) "tom"
3) "lucy"
127.0.0.1:6379[1]> ZREM salary lucy    # 移除有序集合中的指定元素
(integer) 1
127.0.0.1:6379[1]> ZRANGE salary 0 -1
1) "alex"
2) "tom"
127.0.0.1:6379[1]> ZCARD salary    # 获取有序集合个数
(integer) 2
127.0.0.1:6379[1]> FLUSHDB
OK
127.0.0.1:6379[1]> zadd myset 1 1 2 2 3 3
(integer) 3
127.0.0.1:6379[1]> ZCOUNT myset 1 2    # 获取有序集合区间内元素的个数
(integer) 2

案例思路:
set排序,存储班级成绩表,工资表排序
普通消息:1,重要消息:2,带权重进行判断
排行榜应用,取Top N

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!