redis的数据类型-zset

sorted sets是有序集合,它在set的基础上增加了顺序属性,可理解为有两列的mysql表。

  • zadd
    添加元素score,member(如果值相同则更新排序号)
127.0.0.1:6379> srandmember myset3
"five"
127.0.0.1:6379> srandmember myset3
"five"
127.0.0.1:6379> srandmember myset3
"four"
127.0.0.1:6379> srandmember myset3
"five"
127.0.0.1:6379> zadd myzset 1 one
(integer) 1
127.0.0.1:6379> zadd myzset 2 two
(integer) 1
127.0.0.1:6379> zadd myzset 3 two
(integer) 0
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "3"
  • zrem
    删除
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "3"
127.0.0.1:6379> zrem myzset one
(integer) 1
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "two"
2) "3"
  • zincrby
    对顺序号进行增加(存在元素则增加顺序号,不存在则创建元素)
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "two"
2) "3"
127.0.0.1:6379> zincrby myzset 2 two
"5"
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "two"
2) "5"
127.0.0.1:6379> zincrby myzset 2 one
"2"
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "2"
3) "two"
4) "5"
  • zrank
    按照元素的score从小到大进行排序,排序后返回查找元素的索引值
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "2"
3) "two"
4) "5"
127.0.0.1:6379> zadd myzset 4 three
(integer) 1
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "2"
3) "three"
4) "4"
5) "two"
6) "5"
127.0.0.1:6379> zrank myzset three
(integer) 1
  • zrevrank(
    (与zrank相反,降序)
  • zrevrange
    降序排序
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "2"
3) "three"
4) "4"
5) "two"
6) "5"
127.0.0.1:6379> zrank myzset three
(integer) 1
127.0.0.1:6379> zrevrange myzset 0 -1 withscores
1) "two"
2) "5"
3) "three"
4) "4"
5) "one"
6) "2"
  • zrangebyscore
    排序后返回score在给定范围内的元素
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "2"
3) "three"
4) "4"
5) "two"
6) "5"
127.0.0.1:6379> zrangebyscore myzset 2 4 withscores
1) "one"
2) "2"
3) "three"
4) "4"
  • zcount
    score在给定范围内的数量
127.0.0.1:6379> zrange myzset 0 -1 withscores
 1) "one"
 2) "1"
 3) "two"
 4) "2"
 5) "three"
 6) "3"
 7) "four"
 8) "4"
 9) "five"
10) "5"
127.0.0.1:6379> zcount myzset 2 3
(integer) 2
  • zcard
    返回所有元素个数
127.0.0.1:6379> zrange myzset 0 -1 withscores
 1) "one"
 2) "1"
 3) "two"
 4) "2"
 5) "three"
 6) "3"
 7) "four"
 8) "4"
 9) "five"
10) "5"
127.0.0.1:6379> zcount myzset 2 3
(integer) 2
127.0.0.1:6379> zcard myzset
(integer) 5
  • zremrangebyrank
    删除给定区间的元素(区间指索引的区间)
    不要与zremrangebyscore方法混淆(它的区间指score下标的区间)
127.0.0.1:6379> zrange myzset 0 -1 withscores
 1) "one"
 2) "1"
 3) "two"
 4) "2"
 5) "three"
 6) "3"
 7) "four"
 8) "4"
 9) "five"
10) "5"
127.0.0.1:6379> zremrangebyrank myzset 2 3
(integer) 2
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "five"
6) "5"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值