有序集合

发布时间:2020-08-08 20:27:03 作者:梁十八
来源:网络 阅读:268

order set 有序集合

zadd key score1 value1 score2 value2 ..

添加元素
redis 127.0.0.1:6379> zadd stu 18 lily 19 hmm 20 lilei 21 lilei
(integer) 3 (由此看,也是唯一性的)
(排序的依据是score...)

zrem key value1 value2 ..

作用: 删除集合中的元素

有序集合

zremrangebyscore key min max

作用: 按照socre来删除元素,删除score在[min,max]之间的
redis 127.0.0.1:6379> zremrangebyscore stu 4 10
(integer) 2
redis 127.0.0.1:6379> zrange stu 0 -1
1) "f"

有序集合

zremrangebyrank key start end

作用: 按排名删除元素,删除名次在[start,end]之间的
redis 127.0.0.1:6379> zremrangebyrank stu 0 1
(integer) 2
redis 127.0.0.1:6379> zrange stu 0 -1
1) "c"
2) "e"
3) "f"
4) "g"

zrank key member

查询member的排名(升续 0名开始)

有序集合

zrevrank key memeber

查询 member的排名(降续 0名开始)

ZRANGE key start stop [WITHSCORES]

把集合排序后,返回名次[start,stop]的元素(从0开始)
默认是升续排列
Withscores 是把score也打印出来
有序集合
有序集合

zrevrange key start stop

作用:把集合降序排列,取名字[start,stop]之间的元素

zrangebyscore key min max [withscores] limit offset N

作用: 集合(升续)排序后,取score在[min,max]内的元素,
并跳过 offset个, 取出N个
redis 127.0.0.1:6379> zadd stu 1 a 3 b 4 c 9 e 12 f 15 g
(integer) 6
redis 127.0.0.1:6379> zrangebyscore stu 3 12 limit 1 2 withscores
1) "c"
2) "4"
3) "e"
4) "9"

有序集合

有序集合

zcard key

返回元素个数

有序集合

zcount key min max

返回[min,max] 区间内元素的数量

zinterstore destination numkeys key1 [key2 ...]
[WEIGHTS weight [weight ...]]
[AGGREGATE SUM|MIN|MAX]

求key1,key2的交集,key1,key2的权重分别是 weight1,weight2
聚合方法用: sum |min|max
聚合的结果,保存在dest集合内

注意: weights ,aggregate如何理解?
答: 如果有交集, 交集元素又有socre,score怎么处理?
Aggregate sum->score相加 , min 求最小score, max 最大score

另: 可以通过weigth设置不同key的权重, 交集时,socre * weights

详见下例
redis 127.0.0.1:6379> zadd z1 2 a 3 b 4 c
(integer) 3
redis 127.0.0.1:6379> zadd z2 2.5 a 1 b 8 d
(integer) 3
redis 127.0.0.1:6379> zinterstore tmp 2 z1 z2
(integer) 2
redis 127.0.0.1:6379> zrange tmp 0 -1
1) "b"
2) "a"
redis 127.0.0.1:6379> zrange tmp 0 -1 withscores
1) "b"
2) "4"
3) "a"
4) "4.5"
redis 127.0.0.1:6379> zinterstore tmp 2 z1 z2 aggregate sum
(integer) 2
redis 127.0.0.1:6379> zrange tmp 0 -1 withscores
1) "b"
2) "4"
3) "a"
4) "4.5"
redis 127.0.0.1:6379> zinterstore tmp 2 z1 z2 aggregate min
(integer) 2
redis 127.0.0.1:6379> zrange tmp 0 -1 withscores
1) "b"
2) "1"
3) "a"
4) "2"
redis 127.0.0.1:6379> zinterstore tmp 2 z1 z2 weights 1 2
(integer) 2
redis 127.0.0.1:6379> zrange tmp 0 -1 withscores
1) "b"
2) "5"
3) "a"
4) "7"

有序集合

有序集合

(以卖的最少的那个人的数据为准)

有序集合

(以卖的最多的那个人的数据为准)

推荐阅读:
  1. Redis Sorted Set有序集合 存储操作方法
  2. 07 redis数据类型:有序集合 sort set

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

有序 集合

上一篇:老生常谈的Python基础知识学习笔记,老师又敲黑板了

下一篇:有关进行单元测试的时候,不去调本地代码去掉dubbo上的服务

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》