redis基本数据类型之字符串有序集合(sorted sets)操作

一、有序集合使用场景

以某个条件为权重进行排序,比如按顶的次数排序。获得得分前100名用户,获取用户排名。比较适应做排行榜

二、有序集合主要命令

1、增
(1)zadd:将集合添加一个或多个成员、

用法:zadd key score member [score member]

如:在当前比较火的美女主播网站,某个礼物的排行榜,送出礼物为1001的用户排行榜

  • 用户1001送出2个
  • 1002送出20个
  • 1010的用户送出1个
  1. zadd gift:1001:ranking 2 user:1001 20 user:1002 1 user:1010
  1. 127.0.0.1:6379> zadd gift:1001:ranking 2 user:1002 20 user:1001 1 user:1010
  2. (integer) 2
  3. 127.0.0.1:6379> zrange gift:1001:ranking 0 -1 withscores
  4. 1) "user:1010"
  5. 2) "1"
  6. 3) "user:1001"
  7. 4) "2"
  8. 5) "user:1002"
  9. 6) "20"
2、删
(1)zrem:删除集合中某个或某几个成员

用法:zrem key member [member]

如:发现排行榜中有些用户是刷礼物,则删除它,如删除排行榜用户为1002的成员

  1. 127.0.0.1:6379> zrem gift:1001:ranking user:1002
  2. (integer) 1
  3. 127.0.0.1:6379> zrange gift:1001:ranking 0 -1 withscores
  4. 1) "user:1010"
  5. 2) "1"
  6. 3) "user:1001"
  7. 4) "20"
(2)zremrangebyscore:删除分数为指定范围的成员

用法:zremrangebyscore key min max

如:删除排行榜中数量为1至50的成员

  1. 127.0.0.1:6379> zrange gift:1001:ranking 0 -1 withscores
  2. 1) "user:1001"
  3. 2) "20"
  4. 3) "user:2321"
  5. 4) "22"
  6. 5) "user:2020"
  7. 6) "199"
  8. 7) "user:2567"
  9. 8) "2222"
  10. 127.0.0.1:6379> zremrangebyscore gift:1001:ranking 1 50
  11. (integer) 2
  12. 127.0.0.1:6379> zrange gift:1001:ranking 0 -1 withscores
  13. 1) "user:2020"
  14. 2) "199"
  15. 3) "user:2567"
  16. 4) "2222"
  17. ```shell
  18. ###### (3)zremrangebyrank:删除某个指定范围的成员(成员按得分从小到大排)
  19. 用法:`zremrangebyrank key start stop`
  20. 如:
  21. ```shell
  22. 127.0.0.1:6379> zrange gift:1001:ranking 0 -1 withscores
  23. 1) "user:1010"
  24. 2) "3"
  25. 3) "user:2452"
  26. 4) "11"
  27. 5) "user:1001"
  28. 6) "20"
  29. 7) "user:2321"
  30. 8) "22"
  31. 9) "user:2020"
  32. 10) "199"
  33. 11) "user:2567"
  34. 12) "2222"
  35. 127.0.0.1:6379> zremrangebyrank gift:1001:ranking 0 1
  36. (integer) 2
  37. 127.0.0.1:6379> zrange gift:1001:ranking 0 -1 withscores
  38. 1) "user:1001"
  39. 2) "20"
  40. 3) "user:2321"
  41. 4) "22"
  42. 5) "user:2020"
  43. 6) "199"
  44. 7) "user:2567"
  45. 8) "2222"
3、改
(1)zincrby:增加某个成员的得分

用法:zincrby key increment member

如:增加用户1010的得分

  1. 127.0.0.1:6379> zrange gift:1001:ranking 0 -1 withscores
  2. 1) "user:1010"
  3. 2) "1"
  4. 3) "user:2452"
  5. 4) "11"
  6. 5) "user:1001"
  7. 6) "20"
  8. 7) "user:2321"
  9. 8) "22"
  10. 9) "user:2020"
  11. 10) "199"
  12. 11) "user:2567"
  13. 12) "2222"
  14. 127.0.0.1:6379> zincrby gift:1001:ranking 2 user:1010
  15. "3"
  16. 127.0.0.1:6379> zscore gift:1001:ranking user:1010
  17. "3"
4、查
(1)zrange:根据索引查指定范围的成员(结果按得分从小到大排序)

用法:zrange key start end [withscores]

注:

  • start或end如果是负数则从集合尾部开始读取
  • 如果添加withscores这个参数会把成员的得分也返回

如:查询榜单所有成员

  1. 127.0.0.1:6379> zrange gift:1001:ranking 0 -1 withscores
  2. 1) "user:1010"
  3. 2) "1"
  4. 3) "user:2452"
  5. 4) "11"
  6. 5) "user:1001"
  7. 6) "20"
  8. 7) "user:2321"
  9. 8) "22"
  10. 9) "user:2020"
  11. 10) "199"
  12. 11) "user:2567"
  13. 12) "2222"

查询得分最低的5个人

  1. 127.0.0.1:6379> zrange gift:1001:ranking 0 4 withscores
  2. 1) "user:1010"
  3. 2) "1"
  4. 3) "user:2452"
  5. 4) "11"
  6. 5) "user:1001"
  7. 6) "20"
  8. 7) "user:2321"
  9. 8) "22"
  10. 9) "user:2020"
  11. 10) "199"
(2)zrevrange:根据索引查询指定范围的成员(结果按得分从大到小的排序,即倒序)

用法:zrevrange key start end [withscore]

如:获得排行榜的前3名

  1. 127.0.0.1:6379> zrevrange gift:1001:ranking 0 2 withscores
  2. 1) "user:2567"
  3. 2) "2222"
  4. 3) "user:2020"
  5. 4) "199"
  6. 5) "user:2321"
  7. 6) "22"
(3)zrevrangebyscore:查询得分在某个范围的成员(结果按分数从高往低排)

用法:zrevrangebyscore key max min [withscores] [limit offset count]

如:获得排行榜得分在200到1之间的用户

  1. 127.0.0.1:6379> zrevrangebyscore gift:1001:ranking 200 1 withscores
  2. 1) "user:2020"
  3. 2) "199"
  4. 3) "user:2321"
  5. 4) "22"
  6. 5) "user:1001"
  7. 6) "20"
  8. 7) "user:2452"
  9. 8) "11"
  10. 9) "user:1010"
  11. 10) "1"
(4)zscore:获取某个成员的得分

用法:zscore key member

如:获取user:1001的分数

  1. 127.0.0.1:6379> zscore gift:1001:ranking user:1001
  2. "20"
(4)zrank:获取某个成员在集合中的索引值(可以理解为排名值,索引值是从0开始的,得分从低往高排)

用法:zrank key member

如:获取user:1001的索引值

  1. 127.0.0.1:6379> zrange gift:1001:ranking 0 -1
  2. 1) "user:1010"
  3. 2) "user:2452"
  4. 3) "user:1001"
  5. 4) "user:2321"
  6. 5) "user:2020"
  7. 6) "user:2567"
  8. 127.0.0.1:6379> zrank gift:1001:ranking user:1001
  9. (integer) 2
(4)zrevrank:获取某个成员在集合中的排名(按得分从高到低)

用法:zrevrank key member

  1. 127.0.0.1:6379> zrevrange gift:1001:ranking 0 -1
  2. 1) "user:2567"
  3. 2) "user:2020"
  4. 3) "user:2321"
  5. 4) "user:1001"
  6. 5) "user:2452"
  7. 6) "user:1010"
  8. 127.0.0.1:6379> zrevrank gift:1001:ranking user:1001
  9. (integer) 3
(5)zcard:获取集合的成员个数

用法:zcard key

如:获取排行榜中的人数

  1. 127.0.0.1:6379> zcard gift:1001:ranking
  2. (integer) 6
(6)zcount:获取得分在指定范围内的成员的个数

用法:zcount key min max

如:获取得分在1到100之间的成员的个数

  1. 127.0.0.1:6379> zcount gift:1001:ranking 1 100
  2. (integer) 4