redis --- ZSet类型的使用

目录

一、ZSet类型的使用

1.1、zadd  key score member [score member]

1.2、zrange key start stop [withscores] 

1.3、zrerange 

1.4、zrangebyscore key min max [withscores] [limit offset count]

1.5、zscore key member 

1.6、zcard key

1.7、zrem key 

1.8、zincrby key increment member

1.9、zcount key min max

1.10、zmpop 

1.11、zrank key values值 

1.12、zrevrank key values值 


一、ZSet类型的使用

1.1、zadd  key score member [score member]

 添加元素

127.0.0.1:6379> zadd zset1 1 a1 2 a2  3 a3 4 a4
(integer) 4
 

1.2、zrange key start stop [withscores] 

按照元素分数从小到大的顺序返回索引从start到stop之间的所有元素

127.0.0.1:6379> zrange zset1 0 -1
1) "a1"
2) "a2"
3) "a3"
4) "a4"
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "a1"
2) "1"
3) "a2"
4) "2"
5) "a3"
6) "3"
7) "a4"
8) "4"
 

1.3、zrerange 

127.0.0.1:6379> zrevrange zset1 0 -1
1) "a4"
2) "a3"
3) "a2"
4) "a1"
127.0.0.1:6379> zrevrange zset1 0 -1 withscores
1) "a4"
2) "4"
3) "a3"
4) "3"
5) "a2"
6) "2"
7) "a1"
8) "1"
 

1.4、zrangebyscore key min max [withscores] [limit offset count]

获取指定分数范围的元素 :①、withscores ②、( 不包含 ③、limit作用是限制返回

127.0.0.1:6379> zrangebyscore zset1 1 3
1) "a1"
2) "a2"
3) "a3"
127.0.0.1:6379> zrangebyscore zset1 1 3 withscores
1) "a1"
2) "1"
3) "a2"
4) "2"
5) "a3"
6) "3"
127.0.0.1:6379> zrangebyscore zset1 (1 3 withscores
1) "a2"
2) "2"
3) "a3"
4) "3"
127.0.0.1:6379> zrangebyscore zset1 (1 3 withscores limit 0 1
1) "a2"
2) "2"
 

1.5、zscore key member 

获取元素的分数

127.0.0.1:6379> zscore zset1 a1
"1"
 

1.6、zcard key

获取集合中元素的数量

127.0.0.1:6379> zcard zset1
(integer) 4
 

1.7、zrem key 

某个score下对应的value值,作用是删除元素 

127.0.0.1:6379> zrem zset1 a1
(integer) 1
127.0.0.1:6379> zrange zset1 0 -1
1) "a2"
2) "a3"
3) "a4" 

1.8、zincrby key increment member

增加某个元素的分数

127.0.0.1:6379> zincrby zset1 5 a2
"7"
 

1.9、zcount key min max

获取指定分数范围内的元素个数 

127.0.0.1:6379> zcount zset1 2 4
(integer) 2
 

1.10、zmpop 

从键名列表中的第一个非空排序集中弹出一个或多个元素,它们是成员分数对

redis7新加的

127.0.0.1:6379> zmpop  1 zset1  min count 1
1) "zset1"
2) 1) 1) "a3"
      2) "3"
 

1.11、zrank key values值 

作用获取下标值

127.0.0.1:6379> zrange zset1 0 -1
1) "a4"
2) "a2"
127.0.0.1:6379> zrank zset1  a4
(integer) 0
 

1.12、zrevrank key values值 

 作用逆序获取下标值

127.0.0.1:6379> zrevrank zset1  a4
(integer) 1

 

你可能感兴趣的:(redis,redis)