Redis之zset类型及操作

发布时间:2020-08-13 14:44:17 作者:stonebox1122
来源:ITPUB博客 阅读:159
zset是string类型的有序集合

zadd:向名称为key的zset中添加元素member,score用于排序。如果该元素存在,则更新其顺序。
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:删除名称为key的zset中的元素member。
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 two
(integer) 1
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "1"

zincrby:如果在名称为key的zset中已经存在元素member,则该元素的score增加increment,否则向该集合中添加该元素,其score的值为increment。
127.0.0.1:6379> zadd myzset2 1 one
(integer) 1
127.0.0.1:6379> zadd myzset2 2 two
(integer) 1
127.0.0.1:6379> zrange myzset2 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
127.0.0.1:6379> zincrby myzset2 2 one
"3"
127.0.0.1:6379> zrange myzset2 0 -1 withscores
1) "two"
2) "2"
3) "one"
4) "3"

zrank:返回名称为key的zset中member元素的排名(按score从小到大排序)即下标。
127.0.0.1:6379> zadd myzset3 1 one
(integer) 1
127.0.0.1:6379> zadd myzset3 2 two
(integer) 1
127.0.0.1:6379> zadd myzset3 3 three
(integer) 1
127.0.0.1:6379> zadd myzset3 5 five
(integer) 1
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "five"
8) "5"
127.0.0.1:6379> zrank myzset3 two
(integer) 1
127.0.0.1:6379> zrank myzset3 three
(integer) 2

zrevrank:返回名称为key的zset中member元素的排名(按score从大到小排序)即下标
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "five"
8) "5"
127.0.0.1:6379> zrevrank myzset3 five
(integer) 0

zrevrange:
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "five"
8) "5"
127.0.0.1:6379> zrevrange myzset3 0 -1 withscores
1) "five"
2) "5"
3) "three"
4) "3"
5) "two"
6) "2"
7) "one"
8) "1"


zrangebyscore:
127.0.0.1:6379> zrangebyscore myzset3 2 3 withscores
1) "two"
2) "2"
3) "three"
4) "3"

zcount:
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "five"
8) "5"
127.0.0.1:6379> zcount myzset3 2 3
(integer) 2

zcard:
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "five"
8) "5"
127.0.0.1:6379> zcard myzset3
(integer) 4

zremrangebyrank:删除集合中排名在给定区间的元素。
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "five"
8) "5"
127.0.0.1:6379> zremrangebyrank myzset3 1 1
(integer) 1
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "three"
4) "3"
5) "five"
6) "5"


zremrangbyscore:
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "three"
4) "3"
5) "five"
6) "5"
127.0.0.1:6379> zremrangebyscore myzset3 4 5
(integer) 1
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "three"
4) "3"
推荐阅读:
  1. redis之sorted sets类型及操作
  2. redis之sets类型及操作

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

redis 操作 zset

上一篇:python使用库的方法

下一篇:MySQL 8.0 | CATS调度算法的性能提升

相关阅读

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

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