Redis有序集合类型

命令

  1. 增加元素

    ZADD score member [score member ...]
    
    > ZADD scoreboard 89 Tom 76 Peter 100 David
    (integer 3)
    
    • ZADD 命令用来向有序集合中加入一个元素和该元素的分数,如果该元素已经存在则会用新的分数替换原有的分数。
    • ZADD 命令的返回值是新加入到集合中的元素个数
  2. 获得元素的分数

    ZSCORE key member
    
    > ZSCORE scoreboard Tom
    "89"
    
  3. 获得排名在某个范围的元素列表

    ZRANGE key start stop [WITHSCORES]  // 元素分数从小到大
    
    ZREVRANGE key start stop [WITHSCORES]   //元素分数从大到小
    
    > ZRANGE scoreboard 0 2
    1) "Peter"
    2) "Tom"
    3) "David"
    > ZRANGE scoreboard 0 2 WITHSCORES
    1) "Peter"
    2) "76
    3) "Tom"
    4) "89"
    5) "David"
    6) "100"
    
  4. 获得指定分数范围的元素

    ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
    
    ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
    
    > ZRANGEBYSCORE scoreboard 80 100 // 80 <= score <= 100
    1) "Tom"
    2) "David"
    > ZRANGEBYSCORE scoreboard 80 (100 // 80 <= score < 100
    1) "Tom"
    > ZRANGEBYSCORE scoreboard 80 +inf // 80 <= score
    1) "Tom"
    
  5. 增加某个元素的分数

    ZINCRBY key increment memeber
    
    > ZINCRBY scoreboard 4 Peter
    "80"
    > ZINCRBY scoreboard -4 Peter
    "76"
    
  6. 获得集合中元素的数量

    ZCARD key
    
    > ZCARD scoreboard
    (integer) 3
    
  7. 获得指定分数范围内的元素个数

    ZCOUNT key min max
    
    > ZCOUNT scoreboard 90 100
    (integer) 1
    
  8. 删除一个或多个元素

    ZREM key member [member ...]
    
    > ZREM scoreboard Jerry
    (integer) 1
    
    • 返回值是成功删除的元素数量
  9. 按照排名范围删除元素

    按照元素分数从小到大的顺序删除(即索引0表示最小的值)删除处于指定排名范围内的所有元素,并返回删除的元素个数

    ZREMRANGEBYRANK key start stop 
    
    > ZADD testRem 1 a 2 b 3 c 4 d 5 e 6 f
    (integer) 6
    > ZREMRANGEBYRANK testRem 0 2
    (integer) 3
    > ZRANGE testRem 0 -1
    1) "d"
    2) "e"
    3) "f"
    
  10. 按照分数范围删除元素

    ZREMRANGEBYSCORE key mix max
    
    > ZREMRANGEBYSCORE key (4 5
    (integer) 1
    > ZRANGE testRem 0 -1
    1) "d"
    3) "f"
    
  11. 获得元素的排名

    ZRANK key member // 按元素分数从小到大
    ZREVRANK key member //按元素分数从大到小
    
    > ZRANK scoreboard Tom
    (integer) 0
    > ZREVRANK scoreboard Tom
    (integer) 1
    
  12. 计算有序集合的交集

    ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ..]] [AGGREGATE SUM|MIN|MAX]
    
posted @ 2018-11-26 22:55  暮晨  阅读(160)  评论(0编辑  收藏  举报

Aaron Swartz was and will always be a hero