redis从入门到精通(七)———order set结构及命令讲解

Order set结构及命令详解

 

1、zadd key score1 value1 score2 key2 …

添加元素

127.0.0.1:6379> zadd class 12 lily 13 lucy 18 lilei 6 poly

(integer) 4

 

2、zrem key value1 value2…

删除集合中的元素

127.0.0.1:6379> zrem class lily

(integer) 1

 

3、zremrangebyscore key min max

按照score来删除元素,删除scare在【min,max】之间的

 

 

4、zrange key start stop [withscores]

把集合排序后返回名次[start,stop],默认升序

127.0.0.1:6379> zrange class 0 3

1) "poly"

2) "lucy"

3) "lilei"

 

127.0.0.1:6379> zrange class 1 3 withscores

1) "lucy"

2) "13"

3) "lilei"

4) "18"

 

5、zrangebyscore key min max [withscore] limit offset N

集合(升序)排列后,取score在【main,max】内的元素,并跳过offset个,取n个

127.0.0.1:6379> zrangebyscore class 13 18

1) "lucy"

2) "lilei"

 

127.0.0.1:6379> zrangebyscore class 1 20 limit 1 2

1) "lucy"

2) "lilei"

 

127.0.0.1:6379> zrangebyscore class 1 20 withscores limit 1 2

1) "lucy"

2) "13"

3) "lilei"

4) "18"

 

6、zrank key member

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

127.0.0.1:6379> zrank class lily

(nil)

127.0.0.1:6379> zrank class lucy

(integer) 1

127.0.0.1:6379> zrank class lilei

(integer) 2

 

7、zrevrank key member

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

127.0.0.1:6379> zrank class lilei

(integer) 2

127.0.0.1:6379> zrevrank class lucy

(integer) 1

127.0.0.1:6379> zrevrank class lilei

(integer) 0

 

8、zrevrange key start stop

把集合降序排雷,取score在【max,min】之间的元素

127.0.0.1:6379> zrevrange class 0 2

1) "lilei"

2) "lucy"

3) "poly"

127.0.0.1:6379> zrevrange class 0 2 withscores

1) "lilei"

2) "18"

3) "lucy"

4) "13"

5) "poly"

6) "6"

 

9、zrevrangebyscore key min max [withsscores] limit offset n

把集合降序排雷,取名字【start,stop】之间的元素

 

127.0.0.1:6379> zrevrangebyscore class 20 0 withscores limit 1 2

1) "lucy"

2) "13"

3) "poly"

4) "6"

 

10、zremrangebyscore key min max

按排名删除元素,删除score在【min,max】之间的

返回删除个数

127.0.0.1:6379> zremrangebyscore class 6 10

(integer) 1

 

11、zremrangebyrank key start end

按排名删除元素,删除名次在【start,end】之间的

127.0.0.1:6379> zremrangebyrank class 0 1

(integer) 2

 

12、zcount key min max

返回【min,max】区间内元素的数量

127.0.0.1:6379> zadd mcsca 24 ll 26 sd 27 dsa 35 ps

(integer) 4

127.0.0.1:6379> zcard mcsca

(integer) 4

127.0.0.1:6379> zcount mcsca 20 30

(integer) 3

 

13、zinterstore destination numbers key1 [key2…] [weights weight1 [weight2 …]] [aggregate sum|min|max]

求key1 key2的交集,key1 key2的权重分别是weight1 weight2

聚合方法用sum min max

聚合结果保存在dest中

numkeys 提供key个数

 

注意:如果有交集,交集元素又有score , score怎么处理?

aggregate  sum->score相加, min求最小score, max求最大score

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

 

127.0.0.1:6379> zrange mcsca 0 -1

1) "ll"

2) "sd"

3) "dsa"

4) "ps"

127.0.0.1:6379> zrange dlyd 0 -1

1) "ss"

2) "ds"

3) "52"

127.0.0.1:6379> zadd dlyd 32 ll

(integer) 1

127.0.0.1:6379> zinterstore all 2 mcsca dlyd aggregate sum

(integer) 1

127.0.0.1:6379> zrange all 0 -1 withscores

1) "ll"

2) "56"

 

127.0.0.1:6379> zinterstore all 2 mcsca dlyd weights 1 2 aggregate sum

(integer) 1

127.0.0.1:6379> zrange all 0 -1 withscores

1) "ll"

2) "88"

 

读完请点赞,笔者花时间写博客不容易

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值