您现在的位置: 爱51代码网 >> 范文 >> 文章正文
Redis存储篇sorted set主要操作函数小结 .
解释:移除有续集中指定排名范围【start stop】的元素,返回移除元素个数


[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.redis 127.0.0.1:6379> zremrangebyrank score 0 1 
07.(integer) 2 
08.redis 127.0.0.1:6379> zrange score 0 -1 
09.1) "wangwu" 
10.2) "liuli" 
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;返回移除元素个数


[plain] view plaincopyprint?
01.redis 127.0.0.1:6379> zrange score 0 -1 withscores 
02.1) "wangwu" 
03.2) "249" 
04.3) "liuli" 
05.4) "400" 
06.redis 127.0.0.1:6379> zremrangebyscore score 248 250 
07.(integer) 1 
08.redis 127.0.0.1:6379> zrange score 0 -1 withscores 
09.1) "liuli" 
10.2) "400" 
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,求和。


[plain] view plaincopyprint?
01.redis 127.0.0.1:6379> zrange score 0 -1 withscores 
02.1) "lisi" 
03.2) "20" 
04.3) "liuli" 
05.4) "400" 
06.redis 127.0.0.1:6379> zrange score1 0 -1 withscores 
07.1) "wangwu" 
08.2) "20" 
09.3) "liuli" 
10.4) "30" 
11.redis 127.0.0.1:6379> zinterstore dest 2 score score1 #交集求和 sum(score.member.score * 1 + score1.member.score * 1) 
12.(integer) 1 
13.redis 127.0.0.1:6379> zrange dest 0 -1 withscores 
14.1) "liuli" 
15.2) "430" 
16.redis 127.0.0.1:6379> zinterstore dest 2 score score1 weights 1 2 #交集求和,sum(score.member.score * 1 + score1.member.score * 2) 
17.(integer) 1 
18.redis 127.0.0.1:6379> zrange dest 0 -1 withscores 
19.1) "liuli" 
20.2) "460" 
21.redis 127.0.0.1:6379> zinterstore dest 2 score score1 aggregate min #交集求最小值,min(score.member.score * 1 , score1.member.score * 1) 
22.(integer) 1 
23.redis 127.0.0.1:6379> zrange dest 0 -1 withscores 
24.1) "liuli" 
25.2) "30" 
26.redis 127.0.0.1:6379> zinterstore dest 2 score score1 aggregate max #交集求最大值 max(score.member.score * 1 , score1.member.score * 1) 
27.(integer) 1 
28.redis 127.0.0.1:6379> zrange dest 0 -1 withscores 
29.1) "liuli" 
30.2) "400" 
31.redis 127.0.0.1:6379> zinterstore dest 2 score score1 aggregate sum  #交集求和,sum(score.member.score * 1 , score1.member.score * 1) 
32.(integer) 1 
33.redis 127.0.0.1:6379> zrange dest 0 -1 withscores 
34.1) "liuli" 
35.2) "430" 
36.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) 
37.(integer) 1 
38.redis 127.0.0.1:6379> zrange dest 0 -1 withscores 
39.1) "liuli" 
40.2) "60" 
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 中介绍


[plain] view plaincopyprint?
01.redis 127.0.0.1:6379> zrange score 0 -1 withscores 
02.1) "lisi" 
03.2) "20" 
04.3) "liuli" 
05.4) "400" 
06.redis 127.0.0.1:6379> zrange score1 0 -1 withscores 
07.1) "wangwu" 
08.2) "20" 
09.3) "liuli" 
10.4) "30" 
11.redis 127.0.0.1:6379> zunionstore dest 2 score score1 #交集求和 sum(score.member.score * 1 , score1.member.score * 1) 
12.(integer) 3 
13.redis 127.0.0.1:6379> zrange dest 0 -1 withscores 
14.1) "lisi" 
15.2) "20" 
16.3) "wangwu" 
17.4) "20" 
18.5) "liuli" 
19.6) "430" 
20.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) 
21.(integer) 3 
22.redis 127.0.0.1:6379> zrange dest 0 -1 withscores 
23.1) "lisi" 
24.2) "20" 
25.3) "wangwu" 
26.4) "40" 
27.5) "liuli" 
28.6) "460" 
29.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) 
30.(integer) 3 
31.redis 127.0.0.1:6379> zrange dest 0 -1 withscores 
32.1) "lisi" 
33.2) "20" 
34.3) "wangwu" 
35.4) "40" 
36.5) "liuli" 
37.6) "400" 
38.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) 
39.(integer) 3 
40.redis 127.0.0.1:6379> zrange dest 0 -1 withscores 
41.1) "lisi" 
42.2) "40" 
43.3) "wangwu" 
44.4) "80" 
45.5) "liuli" 
46.6) "120" 
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的所有函数,请用下面方式查看


[plain] view plaincopyprint?
01.[root@xsf001 ~]# redis-cli  
02.redis 127.0.0.1:6379> help @sorted_set 

上一页  [1] [2] 

  • 上一篇文章:

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



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