Redis之zset类型及操作-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

Redis之zset类型及操作

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"
当前文章:Redis之zset类型及操作
文章路径:http://kswsj.cn/article/ipoece.html

其他资讯