您现在的位置: 爱51代码网 >> 范文 >> 文章正文
Redis存储篇sorted set主要操作函数小结 .

Redis存储篇sorted set主要操作函数小结 .
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


[plain] view plaincopyprint?
01.[root@xsf001 ~]# redis-cli  
02.redis 127.0.0.1:6379> ZADD score 230 zhangsan 
03.(integer) 1 
04.redis 127.0.0.1:6379> zadd score 240 lisi 250 wangwu 
05.(integer) 2 
06.redis 127.0.0.1:6379> zadd score 100 liuli 
07.redis 127.0.0.1:6379> zadd score 249 wangwu 
08.(integer) 0 
[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];


[plain] view plaincopyprint?
01.redis 127.0.0.1:6379> zrange score 0 -1  #所有元素 
02.1) "liuli" 
03.2) "zhangsan" 
04.3) "lisi" 
05.4) "wangwu" 
06.redis 127.0.0.1:6379> zrange score 0 -1 withscores  #所有member及score 
07.1) "liuli" 
08.2) "100" 
09.3) "zhangsan" 
10.4) "230" 
11.5) "lisi" 
12.6) "240" 
13.7) "wangwu" 
14.8) "249" 
15.redis 127.0.0.1:6379> zrange score 0 2 withscores  #前三个元素 
16.1) "liuli" 
17.2) "100" 
18.3) "zhangsan" 
19.4) "230" 
20.5) "lisi" 
21.6) "240" 
22.redis 127.0.0.1:6379> zrange score 0 -2  #第一个元素到 
23.1) "liuli" 
24.2) "zhangsan" 
25.3) "lisi" 
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个数


[plain] view plaincopyprint?
01.redis 127.0.0.1:6379> zrange score 0 -1 withscores 
02.1) "liuli" 
03.2) "100" 
04.3) "zhangsan" 
05.4) "230" 
06.5) "lisi" 
07.6) "240" 
08.7) "wangwu" 
09.8) "249" 
10.redis 127.0.0.1:6379> zcount score 230 240  # count(score >=230 && score <=240) 
11.(integer) 2 
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


[plain] view plaincopyprint?
01.redis 127.0.0.1:6379> zscore score liuli 
02."100" 
redis 127.0.0.1:6379> zscore score liuli
"100"
d)zrevrange


语法:zrevrange key start stop [withscores]

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


[plain] view plaincopyprint?
01.redis 127.0.0.1:6379> zrevrange score 0 -1 withscores 
02.1) "wangwu" 
03.2) "249" 
04.3) "lisi" 
05.4) "240" 
06.5) "zhangsan" 
07.6) "230" 
08.7) "liuli" 
09.8) "100" 
10.redis 127.0.0.1:6379> zrevrange score 1 -2 
11.1) "lisi" 
12.2) "zhangsan" 
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递增的次序排列。可选withscores决定返回结果集中仅仅返回member还是返回member和score;可选参数limit 指定返回结果的数量区间。


[plain] view plaincopyprint?
01.redis 127.0.0.1:6379> zrangebyscore score 100 10000 withscores 
02.1) "liuli" 
03.2) "100" 
04.3) "zhangsan" 
05.4) "230" 
06.5) "lisi" 
07.6) "240" 
08.7) "wangwu" 
09.8) "249" 
10.redis 127.0.0.1:6379> zrangebyscore score 100 10000  limit 1 3 
11.1) "zhangsan" 
12.2) "lisi" 
13.3) "wangwu" 
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从大到小顺序排列。可选参数withscores决定结果集中是否包含score,可选参数limit 指定返回结果集范围。


[plain] view plaincopyprint?
01.redis 127.0.0.1:6379> zrevrangebyscore score 0 10000 
02.(empty list or set) 
03.redis 127.0.0.1:6379> zrevrangebyscore score 10000 0 
04.1) "wangwu" 
05.2) "lisi" 
06.3) "zhangsan" 
07.4) "liuli" 
08.redis 127.0.0.1:6379> zrevrangebyscore score 10000 0 withscores limit 0, 2 
09.1) "wangwu" 
10.2) "249" 
11.3) "lisi" 
12.4) "240" 
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


[plain] view plaincopyprint?
01.redis 127.0.0.1:6379> zrange score 0 -1 
02.1) "liuli" 
03.2) "zhangsan" 
04.3) "lisi" 
05.4) "wangwu" 
06.redis 127.0.0.1:6379> zrank score liuli 
07.(integer) 0 
08.redis 127.0.0.1:6379> zrank score wangwu 
09.(integer) 3 
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


[plain] view plaincopyprint?
01.redis 127.0.0.1:6379> zrange score 0 -1 
02.1) "liuli" 
03.2) "zhangsan" 
04.3) "lisi" 
05.4) "wangwu" 
06.redis 127.0.0.1:6379> zrevrank score liuli 
07.(integer) 3 
08.redis 127.0.0.1:6379> zrevrank score wangwu 
09.(integer) 0 
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的基数


[plain] view plaincopyprint?
01.redis 127.0.0.1:6379> zcard score 
02.(integer) 4 
03.redis 127.0.0.1:6379> zcard stdu  #有续集不存在返回0 
04.(integer) 0 
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.毕业论文 


[plain] view plaincopyprint?
01.redis 127.0.0.1:6379> zscore score liuli 
02."100" 
03.redis 127.0.0.1:6379> zincrby score 300 liuli 
04."400" 
05.redis 127.0.0.1:6379> zscore score liuli 
06."400" 
07.redis 127.0.0.1:6379> <SPAN style="FONT-FAMILY: Arial, Helvetica, sans-serif">zincrby </SPAN>score 500 xie 
08."500" 
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> zincrby score 500 xie
"500"
注意:如果member在key中不存在,则新增个member


4)delete

a)zrem

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

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


[plain] view plaincopyprint?
01.redis 127.0.0.1:6379> zrange score 0 -1 
02.1) "zhangsan" 
03.2) "lisi" 
04.3) "wangwu" 
05.4) "liuli" 
06.5) "xie" 
07.redis 127.0.0.1:6379> zrem score xie wwww  #www不存在 
08.(integer) 1 
09.redis 127.0.0.1:6379> zrange score 0 -1 
10.1) "zhangsan" 
11.2) "lisi" 
12.3) "wangwu" 
13.4) "liuli" 
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

[1] [2] 下一页

  • 上一篇文章:

  • 下一篇文章: 没有了
  • 最新文章 热点文章 相关文章
    mysql主从同步延迟方案解决的学习
    青岛科学六年级下册教材分析
    生日旅行总结
    中小板生日快乐随感
    送生日快乐桑葚乳酪小蛋糕
    写给女儿的生日快乐
    总分公司财务核算
    恢复使用繁体字可行性研究报告
    青少年吸烟心理探析
    保险受益人制度相关问题的探讨
    mysql主从同步延迟方案解决的学习
    生日旅行总结
    中小板生日快乐随感
    送生日快乐桑葚乳酪小蛋糕
    写给女儿的生日快乐
    总分公司财务核算
    恢复使用繁体字可行性研究报告
    保险受益人制度相关问题的探讨
    初中生地理读图能力培养的研究
    搞笑生日祝福
    把oracle rac 转化为单机数据
    sqlplus spool 到动态日志文
    ios开发之UIButton 点击两张
    android蓝牙串口连接代码
    Preview of Qt 5 for Androi
    VS2010 cocos2d-x 工程配置 
    windows正在配置Windows Ins
    班主任的工作技能
    暖通空调施工图存在的问题及
    多媒体在政治教学中的应用
     



    设为首页 | 加入收藏 | 网站地图 | 友情链接 |