Redis Note9:Redis-Key—ZSet

Redis Note9:Redis-Key—ZSet

1. ZSet

有序集合

1.1 添加:zadd

127.0.0.1:6379> zadd myzset 1 one 2 two 3 three (integer) 3 127.0.0.1:6379> ZRANGE myzset 0 -1 1) "one" 2) "two" 3) "three"

1.2 排序:ZRANGEBYSCORE

  • -inf:负无穷
127.0.0.1:6379> zadd salary 2500 james (integer) 1 127.0.0.1:6379> zadd salary 5000 curry (integer) 1 127.0.0.1:6379> zadd salary 500 wade (integer) 1 # 排序 127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf 1) "wade" 2) "james" 3) "curry" # 排序带参数 127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf withscores 1) "wade" 2) "500" 3) "james" 4) "2500" 5) "curry" 6) "5000" # 薪水小于2500员工的升序排列 127.0.0.1:6379> ZRANGEBYSCORE salary -inf 2500 withscores 1) "wade" 2) "500" 3) "james" 4) "2500" # 从大大小排序 127.0.0.1:6379> ZREVRANGE salary 0 -1 1) "james" 2) "wade"

1. 3 移除元素:zrem

127.0.0.1:6379> ZRANGE myzset 0 -1 1) "one" 2) "two" 3) "three" 127.0.0.1:6379> ZRANGE salary 0 -1 1) "wade" 2) "james" 3) "curry" # 移除元素 127.0.0.1:6379> zrem salary curry (integer) 1 127.0.0.1:6379> ZRANGE salary 0 -1 1) "wade" 2) "james"

1. 4 获取有序集合的个数:ZCARD

127.0.0.1:6379> ZCARD salary (integer) 2

1. 5 获取区间成员数量:zcount

127.0.0.1:6379> ZRANGE myset 0 -1 1) "hello" 2) "world" 3) "redis" 127.0.0.1:6379> zcount myset 1 3 (integer) 3 127.0.0.1:6379> zcount myset 1 2 (integer) 2

2.总结

使用:班级成绩表、工资表