redis之Set(有序)类型常用方法总结

redis之Set(有序)类型常用方法总结

存--ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
  • XX: 仅仅更新存在的成员,不添加新成员。
  • NX: 不更新存在的成员。只添加新成员。
  • CH: 修改返回值为发生变化的成员总数,原始是返回新添加成员的总数 (CH 是 changed 的意思)。更改的元素是新添加的成员,已经存在的成员更新分数。 所以在命令中指定的成员有相同的分数将不被计算在内。注:在通常情况下,ZADD返回值只计算新添加成员的数量。
  • INCR: 当ZADD指定这个选项时,成员的操作就等同ZINCRBY命令,对成员的分数进行递增操作。
取--ZRANGE key start stop [WITHSCORES]
  1. ZADD key score1 member1 [score2 member2] 向有序集合添加一个或多个成员,或者更新已存在成员的分数

    redis> ZADD myzset 1 "one"
    (integer) 1
    redis> ZADD myzset 1 "uno"
    (integer) 1
    redis> ZADD myzset 2 "two" 3 "three"
    (integer) 2
    redis> ZRANGE myzset 0 -1 WITHSCORES
    1) "one"
    2) "1"
    3) "uno"
    4) "1"
    5) "two"
    6) "2"
    7) "three"
    8) "3"
    redis> 
    
  2. ZCARD key 获取有序集合的成员数

    redis> ZADD myzset 1 "one"
    (integer) 1
    redis> ZADD myzset 2 "two"
    (integer) 1
    redis> ZCARD myzset
    (integer) 2
    redis> 
    
  3. ZCOUNT key min max 计算在有序集合中指定区间分数的成员数

    redis 127.0.0.1:6379> ZADD myzset 1 "hello"
    (integer) 1
    redis 127.0.0.1:6379> ZADD myzset 1 "foo"
    (integer) 1
    redis 127.0.0.1:6379> ZADD myzset 2 "world" 3 "bar"
    (integer) 2
    redis 127.0.0.1:6379> ZCOUNT myzset 1 3
    (integer) 4
    
  4. ZINCRBY key increment member 有序集合中对指定成员的分数加上增量 increment

    redis> ZADD myzset 1 "one"
    (integer) 1
    redis> ZADD myzset 2 "two"
    (integer) 1
    redis> ZINCRBY myzset 2 "one"
    "3"
    redis> ZRANGE myzset 0 -1 WITHSCORES
    1) "two"
    2) "2"
    3) "one"
    4) "3"
    redis> 
    
  5. ZINTERSTORE destination numkeys key [key ...] 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中

    # 有序集 mid_test
    redis 127.0.0.1:6379> ZADD mid_test 70 "Li Lei"
    (integer) 1
    redis 127.0.0.1:6379> ZADD mid_test 70 "Han Meimei"
    (integer) 1
    redis 127.0.0.1:6379> ZADD mid_test 99.5 "Tom"
    (integer) 1
    
    # 另一个有序集 fin_test
    redis 127.0.0.1:6379> ZADD fin_test 88 "Li Lei"
    (integer) 1
    redis 127.0.0.1:6379> ZADD fin_test 75 "Han Meimei"
    (integer) 1
    redis 127.0.0.1:6379> ZADD fin_test 99.5 "Tom"
    (integer) 1
    
    # 交集
    redis 127.0.0.1:6379> ZINTERSTORE sum_point 2 mid_test fin_test
    (integer) 3
    
    # 显示有序集内所有成员及其分数值
    redis 127.0.0.1:6379> ZRANGE sum_point 0 -1 WITHSCORES     
    1) "Han Meimei"
    2) "145"
    3) "Li Lei"
    4) "158"
    5) "Tom"
    6) "199"
    
  6. ZLEXCOUNT key min max 在有序集合中计算指定字典区间内成员数量

    redis 127.0.0.1:6379> ZADD myzset 0 a 0 b 0 c 0 d 0 e
    (integer) 5
    redis 127.0.0.1:6379> ZADD myzset 0 f 0 g
    (integer) 2
    redis 127.0.0.1:6379> ZLEXCOUNT myzset - +
    (integer) 7
    redis 127.0.0.1:6379> ZLEXCOUNT myzset [b [f
    (integer) 5
    
  7. ZRANGE key start stop [WITHSCORES] 通过索引区间返回有序集合成指定区间内的成员

    redis 127.0.0.1:6379> ZRANGE salary 0 -1 WITHSCORES             # 显示整个有序集成员
    1) "jack"
    2) "3500"
    3) "tom"
    4) "5000"
    5) "boss"
    6) "10086"
    
    redis 127.0.0.1:6379> ZRANGE salary 1 2 WITHSCORES              # 显示有序集下标区间 1 至 2 的成员
    1) "tom"
    2) "5000"
    3) "boss"
    4) "10086"
    
    redis 127.0.0.1:6379> ZRANGE salary 0 200000 WITHSCORES         # 测试 end 下标超出最大下标时的情况
    1) "jack"
    2) "3500"
    3) "tom"
    4) "5000"
    5) "boss"
    6) "10086"
    
    redis > ZRANGE salary 200000 3000000 WITHSCORES                  # 测试当给定区间不存在于有序集时的情况
    (empty list or set)
    
  8. ZRANGEBYLEX key min max [LIMIT offset count] 通过字典区间返回有序集合的成员

    redis 127.0.0.1:6379> ZADD myzset 0 a 0 b 0 c 0 d 0 e 0 f 0 g
    (integer) 7
    redis 127.0.0.1:6379> ZRANGEBYLEX myzset - [c
    1) "a"
    2) "b"
    3) "c"
    redis 127.0.0.1:6379> ZRANGEBYLEX myzset - (c
    1) "a"
    2) "b"
    redis 127.0.0.1:6379> ZRANGEBYLEX myzset [aaa (g
    1) "b"
    2) "c"
    3) "d"
    4) "e"
    5) "f"
    redis> 
    
  9. ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] 通过分数返回有序集合指定区间内的成员

    redis 127.0.0.1:6379> ZADD salary 2500 jack                        # 测试数据
    (integer) 0
    redis 127.0.0.1:6379> ZADD salary 5000 tom
    (integer) 0
    redis 127.0.0.1:6379> ZADD salary 12000 peter
    (integer) 0
    
    redis 127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf               # 显示整个有序集
    1) "jack"
    2) "tom"
    3) "peter"
    
    redis 127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf WITHSCORES    # 显示整个有序集及成员的 score 值
    1) "jack"
    2) "2500"
    3) "tom"
    4) "5000"
    5) "peter"
    6) "12000"
    
    redis 127.0.0.1:6379> ZRANGEBYSCORE salary -inf 5000 WITHSCORES    # 显示工资 <=5000 的所有成员
    1) "jack"
    2) "2500"
    3) "tom"
    4) "5000"
    
    redis 127.0.0.1:6379> ZRANGEBYSCORE salary (5000 400000            # 显示工资大于 5000 小于等于 400000 的成员
    1) "peter"
    
  10. ZRANK key member 返回有序集合中指定成员的索引

    redis 127.0.0.1:6379> ZRANGE salary 0 -1 WITHSCORES        # 显示所有成员及其 score 值
    1) "peter"
    2) "3500"
    3) "tom"
    4) "4000"
    5) "jack"
    6) "5000"
    
    redis 127.0.0.1:6379> ZRANK salary tom                     # 显示 tom 的薪水排名,第二
    (integer) 1
    
  11. ZREM key member [member ...] 移除有序集合中的一个或多个成员

    # 测试数据
    
    redis 127.0.0.1:6379> ZRANGE page_rank 0 -1 WITHSCORES
    1) "bing.com"
    2) "8"
    3) "baidu.com"
    4) "9"
    5) "google.com"
    6) "10"
    
    
    # 移除单个元素
    
    redis 127.0.0.1:6379> ZREM page_rank google.com
    (integer) 1
    
    redis 127.0.0.1:6379> ZRANGE page_rank 0 -1 WITHSCORES
    1) "bing.com"
    2) "8"
    3) "baidu.com"
    4) "9"
    
    
    # 移除多个元素
    
    redis 127.0.0.1:6379> ZREM page_rank baidu.com bing.com
    (integer) 2
    
    redis 127.0.0.1:6379> ZRANGE page_rank 0 -1 WITHSCORES
    (empty list or set)
    
    
    # 移除不存在元素
    
    redis 127.0.0.1:6379> ZREM page_rank non-exists-element
    (integer) 0
    
  12. ZREMRANGEBYLEX key min max 移除有序集合中给定的字典区间的所有成员

    redis 127.0.0.1:6379> ZADD myzset 0 aaaa 0 b 0 c 0 d 0 e
    (integer) 5
    redis 127.0.0.1:6379> ZADD myzset 0 foo 0 zap 0 zip 0 ALPHA 0 alpha
    (integer) 5
    redis 127.0.0.1:6379> ZRANGE myzset 0 -1
    1) "ALPHA"
     2) "aaaa"
     3) "alpha"
     4) "b"
     5) "c"
     6) "d"
     7) "e"
     8) "foo"
     9) "zap"
    10) "zip"
    redis 127.0.0.1:6379> ZREMRANGEBYLEX myzset [alpha [omega
    (integer) 6
    redis 127.0.0.1:6379> ZRANGE myzset 0 -1
    1) "ALPHA"
    2) "aaaa"
    3) "zap"
    4) "zip"
    redis> 
    
  13. ZREMRANGEBYRANK key start stop 移除有序集合中给定的排名区间的所有成员

    redis 127.0.0.1:6379> ZADD salary 2000 jack
    (integer) 1
    redis 127.0.0.1:6379> ZADD salary 5000 tom
    (integer) 1
    redis 127.0.0.1:6379> ZADD salary 3500 peter
    (integer) 1
    
    redis 127.0.0.1:6379> ZREMRANGEBYRANK salary 0 1       # 移除下标 0 至 1 区间内的成员
    (integer) 2
    
    redis 127.0.0.1:6379> ZRANGE salary 0 -1 WITHSCORES    # 有序集只剩下一个成员
    1) "tom"
    2) "5000"
    
  14. ZREMRANGEBYSCORE key min max 移除有序集合中给定的分数区间的所有成员

    redis 127.0.0.1:6379> ZRANGE salary 0 -1 WITHSCORES          # 显示有序集内所有成员及其 score 值
    1) "tom"
    2) "2000"
    3) "peter"
    4) "3500"
    5) "jack"
    6) "5000"
    
    redis 127.0.0.1:6379> ZREMRANGEBYSCORE salary 1500 3500      # 移除所有薪水在 1500 到 3500 内的员工
    (integer) 2
    
    redis> ZRANGE salary 0 -1 WITHSCORES          # 剩下的有序集成员
    1) "jack"
    2) "5000"
    
  15. ZREVRANGE key start stop [WITHSCORES] 返回有序集中指定区间内的成员,通过索引,分数从高到底

    redis 127.0.0.1:6379> ZRANGE salary 0 -1 WITHSCORES        # 递增排列
    1) "peter"
    2) "3500"
    3) "tom"
    4) "4000"
    5) "jack"
    6) "5000"
    
    redis 127.0.0.1:6379> ZREVRANGE salary 0 -1 WITHSCORES     # 递减排列
    1) "jack"
    2) "5000"
    3) "tom"
    4) "4000"
    5) "peter"
    6) "3500"
    
  16. ZREVRANGEBYSCORE key max min [WITHSCORES] 返回有序集中指定分数区间内的成员,分数从高到低排序

    redis 127.0.0.1:6379> ZADD salary 10086 jack
    (integer) 1
    redis > ZADD salary 5000 tom
    (integer) 1
    redis 127.0.0.1:6379> ZADD salary 7500 peter
    (integer) 1
    redis 127.0.0.1:6379> ZADD salary 3500 joe
    (integer) 1
    
    redis 127.0.0.1:6379> ZREVRANGEBYSCORE salary +inf -inf   # 逆序排列所有成员
    1) "jack"
    2) "peter"
    3) "tom"
    4) "joe"
    
    redis 127.0.0.1:6379> ZREVRANGEBYSCORE salary 10000 2000  # 逆序排列薪水介于 10000 和 2000 之间的成员
    1) "peter"
    2) "tom"
    3) "joe"
    
  17. ZREVRANK key member 返回有序集合中指定成员的排名,有序集成员按分数值递减(从大到小)排序

    redis 127.0.0.1:6379> ZRANGE salary 0 -1 WITHSCORES     # 测试数据
    1) "jack"
    2) "2000"
    3) "peter"
    4) "3500"
    5) "tom"
    6) "5000"
    
    redis 127.0.0.1:6379> ZREVRANK salary peter     # peter 的工资排第二
    (integer) 1
    
    redis 127.0.0.1:6379> ZREVRANK salary tom       # tom 的工资最高
    (integer) 0
    
  18. ZSCORE key member 返回有序集中,成员的分数值

    redis 127.0.0.1:6379> ZRANGE salary 0 -1 WITHSCORES    # 测试数据
    1) "tom"
    2) "2000"
    3) "peter"
    4) "3500"
    5) "jack"
    6) "5000"
    
    redis 127.0.0.1:6379> ZSCORE salary peter              # 注意返回值是字符串
    "3500"
    
  19. ZUNIONSTORE destination numkeys key [key ...] 计算给定的一个或多个有序集的并集,并存储在新的 key 中

    redis 127.0.0.1:6379> ZRANGE programmer 0 -1 WITHSCORES
    1) "peter"
    2) "2000"
    3) "jack"
    4) "3500"
    5) "tom"
    6) "5000"
    
    redis 127.0.0.1:6379> ZRANGE manager 0 -1 WITHSCORES
    1) "herry"
    2) "2000"
    3) "mary"
    4) "3500"
    5) "bob"
    6) "4000"
    
    redis 127.0.0.1:6379> ZUNIONSTORE salary 2 programmer manager WEIGHTS 1 3   # 公司决定加薪。。。除了程序员。。。
    (integer) 6
    
    redis 127.0.0.1:6379> ZRANGE salary 0 -1 WITHSCORES
    1) "peter"
    2) "2000"
    3) "jack"
    4) "3500"
    5) "tom"
    6) "5000"
    7) "herry"
    8) "6000"
    9) "mary"
    10) "10500"
    11) "bob"
    12) "12000"
    
  20. ZSCAN key cursor [MATCH pattern] [COUNT count] 迭代有序集合中的元素(包括元素成员和元素分值)?

    
    
posted @ 2019-06-25 00:01  Hello_Jack  阅读(668)  评论(0编辑  收藏  举报
# 页脚html代码 /*头部导航栏*/ #navigator { font-size:15px; border-bottom: 1px solid #ededed; border-top: 1px solid #ededed; height: 60px;/*导航栏高度,原始50*/ clear: both; margin-top: 25px; } /*导航栏设置,可以自定义导航栏的目录*/ #navList { min-height: 35px; float: left; } #navList li { /*每一个栏目节点*/ float: left; margin: 0 5px 0 0; /*这里原来是0 40px 0 0 */ } #navList a { /*栏目文字的格式*/ display: block; width: 5em; height: 22px; float: left; text-align: center; padding-top: 19px; }