• 苹果股票价格累计下跌7.1% 市值蒸发639亿美元 2018-04-27
  • 《夏目的美丽日记》绿色度测评报告 2018-04-27
  • 景区应从门票经济转向产业经济 2018-04-27
  • 问政时刻 | 谁在“起跑线”使绊子 2018-04-27
  • 开创生态文明新时代 绘出美丽中国新图景 2018-04-26
  • 贵州桐梓:困难群众打工差点错过“移民搬迁”之后 2018-04-26
  • 国乒男女队出征第54届世乒赛“盼天晴” 2018-04-26
  • “鲶鱼”初显 “自主三强”以合作应“开放”? 2018-04-25
  • 第二批慈善组织互联网募捐信息平台遴选结果公示 2018-04-25
  • 国乒男女队出征第54届世乒赛“盼天晴” 2018-04-24
  • 崔世安会见吴政隆 双方就推进苏澳合作深入交谈 2018-04-24
  • 音乐剧《朝暮有情人》演绎港穗两地三代情 2018-04-24
  • 3月70城房价环比涨幅扩大 2018-04-24
  • 国际油价4月19日涨跌互现 2018-04-23
  • 南平市人民防空办公室成立武夷新区人防办事处 2018-04-23
  • Redis系列-存储篇sorted set主要操作函数小结

    栏目: redis 发布于: 2014-01-19 14:42:16

    redis支持有序集合,即sorted set。sorted set在set的基础上,增加了排序属性,是set的升级版。这里简要谈谈sorted set的常用函数:

    1)insert

    a)  zadd

    语法:zadd key score member [[score member] [score member] ...]

    解释:增加一个或多个member【根据score排序】到有序集key中,如果member已经存在,只更新score。返回增加member个数,不包含已经存在的member

    [root@xsf001 ~]# redis-cli 
    redis 127.0.0.1:6379> ZADD score 230 zhangsan
    (integer) 1
    redis 127.0.0.1:6379> zadd score 240 lisi 250 wangwu
    (integer) 2
    redis 127.0.0.1:6379> zadd score 100 liuli
    redis 127.0.0.1:6379> zadd score 249 wangwu
    (integer) 0
    注意:有些redis版本,只能一次添加一个member

    2)select

    a)zrange

    语法:zrange key start stop [withscores]

    解释:返回有续集key中指定范围【通过索引 start stop】的member[及其score];

    redis 127.0.0.1:6379> zrange score 0 -1  #所有元素
    1) "liuli"
    2) "zhangsan"
    3) "lisi"
    4) "wangwu"
    redis 127.0.0.1:6379> zrange score 0 -1 withscores  #所有member及score
    1) "liuli"
    2) "100"
    3) "zhangsan"
    4) "230"
    5) "lisi"
    6) "240"
    7) "wangwu"
    8) "249"
    redis 127.0.0.1:6379> zrange score 0 2 withscores  #前三个元素
    1) "liuli"
    2) "100"
    3) "zhangsan"
    4) "230"
    5) "lisi"
    6) "240"
    redis 127.0.0.1:6379> zrange score 0 -2  #第一个元素到
    1) "liuli"
    2) "zhangsan"
    3) "lisi"
    注意:0,表示第一个元素,-1 表示最后一个元素,-2 表示倒数第二个元素

    b)zcount

    语法:zcount key min max

    解释:统计key中score值介于min 和max之间的member个数

    redis 127.0.0.1:6379> zrange score 0 -1 withscores
    1) "liuli"
    2) "100"
    3) "zhangsan"
    4) "230"
    5) "lisi"
    6) "240"
    7) "wangwu"
    8) "249"
    redis 127.0.0.1:6379> zcount score 230 240  # count(score >=230 && score <=240)
    (integer) 2
    c) zscore

    语法:zscore key member

    解释:返回有续集key中member的score

    redis 127.0.0.1:6379> zscore score liuli
    "100"
    d)zrevrange

    语法:zrevrange key start stop [withscores]

    解释:返回有续集key中指定范围[通过索引start stop]的member[及score],返回member根据score按降序排列

    redis 127.0.0.1:6379> zrevrange score 0 -1 withscores
    1) "wangwu"
    2) "249"
    3) "lisi"
    4) "240"
    5) "zhangsan"
    6) "230"
    7) "liuli"
    8) "100"
    redis 127.0.0.1:6379> zrevrange score 1 -2
    1) "lisi"
    2) "zhangsan"
    注意:索引0 表示第一个元素,-1 最后一个元素,-2 倒数第二个元素;依次类推

    e)zrangebyscore

    语法:zrangebyscore key min max [withscores] [limit offset count]

    解释:返回有续集key中,score大于等于min并且小于等于max的member。返回结果按照score递增的次序排列??裳ithscores决定返回结果集中仅仅返回member还是返回member和score;可选参数limit 指定返回结果的数量区间。

    redis 127.0.0.1:6379> zrangebyscore score 100 10000 withscores
    1) "liuli"
    2) "100"
    3) "zhangsan"
    4) "230"
    5) "lisi"
    6) "240"
    7) "wangwu"
    8) "249"
    redis 127.0.0.1:6379> zrangebyscore score 100 10000  limit 1 3
    1) "zhangsan"
    2) "lisi"
    3) "wangwu"
    f)zrevrangebyscore

    语法:zrevrangescore key max min [withscores] [limit offset count]

    解释:返回有续集key中score<=max并且score>=min 的元素,返回结果根据score从大到小顺序排列??裳〔问齱ithscores决定结果集中是否包含score,可选参数limit 指定返回结果集范围。

    redis 127.0.0.1:6379> zrevrangebyscore score 0 10000
    (empty list or set)
    redis 127.0.0.1:6379> zrevrangebyscore score 10000 0
    1) "wangwu"
    2) "lisi"
    3) "zhangsan"
    4) "liuli"
    redis 127.0.0.1:6379> zrevrangebyscore score 10000 0 withscores limit 0, 2
    1) "wangwu"
    2) "249"
    3) "lisi"
    4) "240"
    注意:max在min之前

    g)zrank

    语法:zrank key member

    解释:根据score从低到高,返回member在有续集中的index

    redis 127.0.0.1:6379> zrange score 0 -1
    1) "liuli"
    2) "zhangsan"
    3) "lisi"
    4) "wangwu"
    redis 127.0.0.1:6379> zrank score liuli
    (integer) 0
    redis 127.0.0.1:6379> zrank score wangwu
    (integer) 3
    
    h)zrevrank

    语法:zrevrank key member

    解释:根据score从高到低排序,返回member在有序集key中的index

    redis 127.0.0.1:6379> zrange score 0 -1
    1) "liuli"
    2) "zhangsan"
    3) "lisi"
    4) "wangwu"
    redis 127.0.0.1:6379> zrevrank score liuli
    (integer) 3
    redis 127.0.0.1:6379> zrevrank score wangwu
    (integer) 0
     i)zcard

    语法:zcard key

    解释:返回有续集key的基数

    redis 127.0.0.1:6379> zcard score
    (integer) 4
    redis 127.0.0.1:6379> zcard stdu  #有续集不存在返回0
    (integer) 0
    3)update

    a)zincrby

    语法:zincrby key increment member

    解释:有续集key的member增加增量increment,返回增加后的score

    redis 127.0.0.1:6379> zscore score liuli
    "100"
    redis 127.0.0.1:6379> zincrby score 300 liuli
    "400"
    redis 127.0.0.1:6379> zscore score liuli
    "400"
    redis 127.0.0.1:6379> <span style="font-family: Arial, Helvetica, sans-serif;">zincrby </span>score 500 xie
    "500"
    
    注意:如果member在key中不存在,则新增个member

    4)delete

    a)zrem

    语法:zrem key member [member ...]

    解释:移除有续集中的一个或多个member,返回移除member的个数

    redis 127.0.0.1:6379> zrange score 0 -1
    1) "zhangsan"
    2) "lisi"
    3) "wangwu"
    4) "liuli"
    5) "xie"
    redis 127.0.0.1:6379> zrem score xie wwww  #www不存在
    (integer) 1
    redis 127.0.0.1:6379> zrange score 0 -1
    1) "zhangsan"
    2) "lisi"
    3) "wangwu"
    4) "liuli"
    注意:如果member不存在,则忽略

    b)zremrangebyrank

    语法:zremrangebyrank key start stop

    解释:移除有续集中指定排名范围【start stop】的元素,返回移除元素个数

    redis 127.0.0.1:6379> zrange score 0 -1
    1) "zhangsan"
    2) "lisi"
    3) "wangwu"
    4) "liuli"
    redis 127.0.0.1:6379> zremrangebyrank score 0 1
    (integer) 2
    redis 127.0.0.1:6379> zrange score 0 -1
    1) "wangwu"
    2) "liuli"
    注意:如果stop在start之前,则移除0个

    c)zremrangebyscore

    语法:zremrangebyscore key min max

    解释:移除有续集中的member,移除member的score大于等于min小于等于max;返回移除元素个数

    redis 127.0.0.1:6379> zrange score 0 -1 withscores
    1) "wangwu"
    2) "249"
    3) "liuli"
    4) "400"
    redis 127.0.0.1:6379> zremrangebyscore score 248 250
    (integer) 1
    redis 127.0.0.1:6379> zrange score 0 -1 withscores
    1) "liuli"
    2) "400"
    5)其他

    a)zinterstore

    语法:zinterstore destination numkeys  key[key...] [weights weight] [aggregate sum|min|max]

    解释:统计多个有续集的交集,其中有续集key的个数必须以numkeys参数指定,并将统计结果存储到destination。默认情况下,destination中元素的score是各个有续集key中元素的score之和。使用weights为每个有续集指定个乘法因子,每个有续集的score在传递给集合函数(aggregate)之前,先乘以乘法因子。如果没指定乘法因子weight,默认是1;使用aggregate选项指定交集的聚合方式。min,最小score,max,最大score,sum,求和。

    redis 127.0.0.1:6379> zrange score 0 -1 withscores
    1) "lisi"
    2) "20"
    3) "liuli"
    4) "400"
    redis 127.0.0.1:6379> zrange score1 0 -1 withscores
    1) "wangwu"
    2) "20"
    3) "liuli"
    4) "30"
    redis 127.0.0.1:6379> zinterstore dest 2 score score1 #交集求和 sum(score.member.score * 1 + score1.member.score * 1)
    (integer) 1
    redis 127.0.0.1:6379> zrange dest 0 -1 withscores
    1) "liuli"
    2) "430"
    redis 127.0.0.1:6379> zinterstore dest 2 score score1 weights 1 2 #交集求和,sum(score.member.score * 1 + score1.member.score * 2)
    (integer) 1
    redis 127.0.0.1:6379> zrange dest 0 -1 withscores
    1) "liuli"
    2) "460"
    redis 127.0.0.1:6379> zinterstore dest 2 score score1 aggregate min #交集求最小值,min(score.member.score * 1 , score1.member.score * 1)
    (integer) 1
    redis 127.0.0.1:6379> zrange dest 0 -1 withscores
    1) "liuli"
    2) "30"
    redis 127.0.0.1:6379> zinterstore dest 2 score score1 aggregate max #交集求最大值 max(score.member.score * 1 , score1.member.score * 1)
    (integer) 1
    redis 127.0.0.1:6379> zrange dest 0 -1 withscores
    1) "liuli"
    2) "400"
    redis 127.0.0.1:6379> zinterstore dest 2 score score1 aggregate sum  #交集求和,sum(score.member.score * 1 , score1.member.score * 1)
    (integer) 1
    redis 127.0.0.1:6379> zrange dest 0 -1 withscores
    1) "liuli"
    2) "430"
    redis 127.0.0.1:6379> zinterstore dest 2 score score1 weights 1 2 aggregate min #交集求最小值 min(score.member.score * 1 , score1.member.score * 2)
    (integer) 1
    redis 127.0.0.1:6379> zrange dest 0 -1 withscores
    1) "liuli"
    2) "60"
    
    b)zunionstore

    语法:zunionstore destination numkeys  key[key...] [weights weight] [aggregate sum|min|max]

    解释:统计多个有续集的并集,其中有续集key的个数必须以numkeys参数指定,并将统计结果存储到destination。。默认情况下,destination中元素的score是各个有续集key中元素的score之和。weights指定乘法因子,aggregate指定聚合方式,详情,请参阅 zinterstore 中介绍

    redis 127.0.0.1:6379> zrange score 0 -1 withscores
    1) "lisi"
    2) "20"
    3) "liuli"
    4) "400"
    redis 127.0.0.1:6379> zrange score1 0 -1 withscores
    1) "wangwu"
    2) "20"
    3) "liuli"
    4) "30"
    redis 127.0.0.1:6379> zunionstore dest 2 score score1 #交集求和 sum(score.member.score * 1 , score1.member.score * 1)
    (integer) 3
    redis 127.0.0.1:6379> zrange dest 0 -1 withscores
    1) "lisi"
    2) "20"
    3) "wangwu"
    4) "20"
    5) "liuli"
    6) "430"
    redis 127.0.0.1:6379> zunionstore dest 2 score score1  weights 1 2 aggregate sum #交集求和 sum(score.member.score * 1 , score1.member.score * 2)
    (integer) 3
    redis 127.0.0.1:6379> zrange dest 0 -1 withscores
    1) "lisi"
    2) "20"
    3) "wangwu"
    4) "40"
    5) "liuli"
    6) "460"
    redis 127.0.0.1:6379> zunionstore dest 2 score score1  weights 1 2 aggregate max #交集最大值 max(score.member.score * 1 , score1.member.score * 2)
    (integer) 3
    redis 127.0.0.1:6379> zrange dest 0 -1 withscores
    1) "lisi"
    2) "20"
    3) "wangwu"
    4) "40"
    5) "liuli"
    6) "400"
    redis 127.0.0.1:6379> zunionstore dest 2 score score1  weights 2 4 aggregate min #交集最小值 min(score.member.score * 2 , score1.member.score * 4)
    (integer) 3
    redis 127.0.0.1:6379> zrange dest 0 -1 withscores
    1) "lisi"
    2) "40"
    3) "wangwu"
    4) "80"
    5) "liuli"
    6) "120"
    提醒:sorted set的所有函数,请用下面方式查看

    [root@xsf001 ~]# redis-cli 
    redis 127.0.0.1:6379> help @sorted_set


    本站文章除注明转载外,均为本站原创或编译?;队魏涡问降淖?,但请务必注明出处。
    转载请注明:文章转载自 七星彩票平台
    本文标题:Redis系列-存储篇sorted set主要操作函数小结
    IT技术书籍推荐:
    深入理解MySQL核心技术
    深入理解MySQL核心技术
    Sasba Pacbev (作者), 李芳 (译者), 于红芸 (译者), 邵健 (译者)
    《深入理解MySQL核心技术》:从公共可用性的意义上讲,MySQL源代码是开放源代码,但如果对其不了解,则实质上,它对于您来说是封闭的。MysQL开发团队的前成员Sasha Pachev通过《深入理解MySQL核心技术》给出了MySQL 5的全面指南,揭示了这一强大数据库的内部运作。您将直奔MySQL核心技术,了解各种数据结构和各种方便的功能的运作情况,了解如何添加新的存储引擎和配置选项等。 《深入理解MySQL核心技术》从结构概况讲起,在这一部分解释了MysQL的不同组件是如何协同工作的。接着将学习设置有效的可编译代码副本的步骤,然后使用基本架构添加自己的配置变量和存储引擎。