zoukankan      html  css  js  c++  java
  • redis_zset

    Zset

    • 集合中不能有重复元素

    • 集合中的元素可以排序,每个元素设置一个分数(score)作为排序的依据. score可以重复。

    • 1:集合内

      • 添加成员 zadd key score member [score member ...]

        127.0.0.1:6379> zadd user:ranking 250 tom   #向有序集合user:ranking 添加用户tom和分数250
        (integer) 1
        127.0.0.1:6379> zadd user:ranking 1 kris 91 mike 200 frank  220 tim  250 martin
        (integer) 5
        
        new特新:
        nx:member不存在,则设置成功
        xx:member必须存在,用于更新
        ch:返回此次操作后,有序集合元素和分数发生变化的个数
        incr:对score做增加
        
        #O(log(n))
        
      • 计算成员个数 zcard key

        127.0.0.1:6379> zcard user:ranking
        (integer) 6
        
      • 计算某个成员的分数 zscore key member

        127.0.0.1:6379> zscore user:ranking mike
        "91"
        
      • 计算成员的排名

        • zrank key member --从低到高
        • zrevrank key member --从高到低
        127.0.0.1:6379> zrank user:ranking tom
        (integer) 5
        127.0.0.1:6379> zrevrank user:ranking tom   #排名从0开始计数
        (integer) 0
        
      • 删除成员 zrem key member

        127.0.0.1:6379> zrem user:ranking mike
        (integer) 1
        
      • 增加成员的 分数 zincrby key increment member

        127.0.0.1:6379> zincrby user:ranking 9 tom   #给tom加9分
        "259"
        
      • 返回指定排名范围的成员

        • zrange key start end [withscores]
        • zrevrange key start end [withscores]
        127.0.0.1:6379> zrange user:ranking 0 2 withscores
        1) "kris"
        2) "1"
        3) "frank"
        4) "200"
        5) "tim"
        6) "220"
        
        127.0.0.1:6379> zrevrange user:ranking 0 2  withscores
        1) "tom"
        2) "259"
        3) "martin"
        4) "250"
        5) "tim"
        6) "220"
        
      • 返回指定分数范围的成员

        • zrangebyscore key min max [withscores] [limit offset count]

        • zrevrangebyscore key min max [withscores] [limit offset count]

          127.0.0.1:6379> zrangebyscore user:ranking 200 221 withscores
          1) "frank"
          2) "200"
          3) "tim"
          4) "220"
          
          #min,max 支持(开区间),[闭区间]
          #-inf 无限小
          #+inf 无限大
          127.0.0.1:6379> zrangebyscore user:ranking (200 +inf  withscores
          1) "tim"
          2) "220"
          3) "martin"
          4) "250"
          5) "tom"
          6) "259"
          
        • 返回指定分数范围成员的个数 zcount key min max

          127.0.0.1:6379> zcount user:ranking 200 221
          (integer) 2
          
        • 删除指定排名内的升序元素 zremrangebyrank key start end

          127.0.0.1:6379> zremrangebyrank  user:ranking  0 2
          (integer) 3
          
          
          127.0.0.1:6379> zrangebyscore user:ranking -inf  +inf  withscores  #查看所有成员
          1) "martin"
          2) "250"
          3) "tom"
          4) "259"
          
        • 删除指定分数范围内的成员 zremrangebyscore key min max

          127.0.0.1:6379> zremrangebyscore  user:ranking  (250  +inf  
          (integer) 1
          
      • 集合间的操作

        • 两个集合

        • 127.0.0.1:6379> zadd user:ranking:1  1 kris  91 mike  220  frank 220  tim 250 martin  251  tom 
          (integer) 6
          
          127.0.0.1:6379> zadd user:ranking:2  8 james  77 mike  625 martin  888 tom
          (integer) 4
          
          127.0.0.1:6379> keys *
          1) "user:ranking:1"
          2) "user:ranking:2"
          
          • 交集

          • zinterscore destination numkeys key1 [key2 ....] [weights weight1 [weight2 ...] ] [aggregate sum| min | max]

          • numkeys 代表key1,key2的总数

          • weights 权重, weight1 对应key1的权重,weight2对应key2的权重,默认值是1.

          • aggregate:聚合,计算交集后做汇总,默认是sum

            127.0.0.1:6379> zinterstore user:ranking:1_2_inter  2  user:ranking:1  user:ranking:2  weights  1  1  
            (integer) 3
            127.0.0.1:6379> keys *
            1) "user:ranking:2"
            2) "user:ranking:1_2_inter"
            3) "user:ranking:1"
            
            #查看一下交集的成员
            127.0.0.1:6379> zrange  user:ranking:1_2_inter 0 -1 withscores
            1) "mike"
            2) "168"
            3) "martin"
            4) "875"
            5) "tom"
            6) "1139"
            
            
            
          • 并集

          • zunionscore destination numkeys key1 [key2 ....] [weights weight1 [weight2 ...] ] [aggregate sum| min | max]

        • 内部编码

        • ziplist(压缩列表):zset类型元素个数<hash-max-ziplist-entries(default128) 或者 所有的值<hash-max-ziplist-value(64字节) 同时满足以上2个条件,则使用更加紧凑的ziplist实现多个元素的连续存储。

        • skiplist(跳跃表):

  • 相关阅读:
    MySQL语法
    SQL必知必会
    大话设计模式
    软件工程
    myeclipse中git的使用
    提取文件中图片标签
    pandas 分析各地区男女的分布情况
    提取图片标签
    Requwsts+正则表达式爬取猫眼电影Top100
    PyCharm激活码
  • 原文地址:https://www.cnblogs.com/zhoujun007/p/13131665.html
Copyright © 2011-2022 走看看