zset是有序存储的数据结构,它和set一样,不允许重复的值,下面我们总结一些常用的命令。
- zadd key 排序的数值 值(这里为了zset可以有序的存储,需要设定数值)
127.0.0.1:6379> zadd z1 5 java 3 redis 1 mysql 2 nginx 4 oracle
(integer) 5
- zcard key(返回key中的成员数)
127.0.0.1:6379> zcard z1
(integer) 5
- zrange key 开始的索引 结束的索引(取出key中,指定的开始索引和结束索引的值,0 -1的话,取出所有的值)
127.0.0.1:6379> zrange z1 0 -1
1) "mysql"
2) "nginx"
3) "redis"
4) "oracle"
5) "java"
这里返回的结果,是按照我们在赋值时,设定的排序数值从小到大的顺序,这里小伙伴会问,现在取出的值是从小到大的顺序返回的,如果从大到小的顺序,我们接着来学习zrevrange
- zrevrange key 开始的索引 结束的索引(从key中按照从大到小的顺序,取出zset中的值)
127.0.0.1:6379> zrevrange z1 0 -1
1) "java"
2) "oracle"
3) "redis"
4) "nginx"
5) "mysql"
- zcount key 开始的索引 结束的索引(返回在key中开始索引和结束索引中,元素的个数,开始索引<= 返回的值<= 结束索引)
127.0.0.1:6379> zrange z1 0 -1
1) "mysql"
2) "nginx"
3) "redis"
4) "oracle"
5) "java"
127.0.0.1:6379> zcount z1 2 4
(integer) 3
- zrank key 值(返回在key中指定值的索引,索引大家都知道是从0开始的)
127.0.0.1:6379> zrange z1 0 -1
1) "mysql"
2) "nginx"
3) "redis"
4) "oracle"
5) "java"
127.0.0.1:6379> zrank z1 redis
(integer) 2
- zrangebyscore key 开始score 结束score(我们往zset里赋值时,每个元素都设定了score,这里用score来取值)
127.0.0.1:6379> zadd z2 20 mysql 30 nginx 40 redis 50 oracle 60 java
(integer) 5
127.0.0.1:6379> zadd z2 10 tomcat
(integer) 1
127.0.0.1:6379> zrange z2 0 -1
1) "tomcat"
2) "mysql"
3) "nginx"
4) "redis"
5) "oracle"
6) "java"
127.0.0.1:6379> zrangebyscore z2 20 50
1) "mysql"
2) "nginx"
3) "redis"
4) "oracle"
- zrevrangebyscore key 开始score 结束score(返回score从大到小的顺序的结果集)
127.0.0.1:6379> zrange z2 0 -1
1) "tomcat"
2) "mysql"
3) "nginx"
4) "redis"
5) "oracle"
6) "java"127.0.0.1:6379> zrevrangebyscore z2 50 20
1) "oracle"
2) "redis"
3) "nginx"
4) "mysql"
- del key(删除zset中的指定key)
127.0.0.1:6379> keys *
1) "s1"
2) "z1"
3) "z2"
4) "s3"
5) "s2"
127.0.0.1:6379> del z1
(integer) 1
127.0.0.1:6379> keys *
1) "s1"
2) "z2"
3) "s3"
4) "s2"
- zrem key 指定的元素(删除key中指定的元素)
127.0.0.1:6379> zrange z2 0 -1
1) "tomcat"
2) "mysql"
3) "nginx"
4) "redis"
5) "oracle"
6) "java"
127.0.0.1:6379>
127.0.0.1:6379> zrem z2 tomcat
(integer) 1
127.0.0.1:6379> zrange z2 0 -1
1) "mysql"
2) "nginx"
3) "redis"
4) "oracle"
5) "java"
- zremrangebyrank key 指定开始位置 指定结束位置 (删除key中指定的开始位置到结束位置)
127.0.0.1:6379> zrange z2 0 -1
1) "mysql"
2) "nginx"
3) "redis"
4) "oracle"
5) "java"
127.0.0.1:6379> zremrangebyrank z2 2 3
(integer) 2
127.0.0.1:6379> zrange z2 0 -1
1) "mysql"
2) "nginx"
3) "java"
- zremrangebyscore key 指定开始score 指定结束score (删除key中指定的开始score到结束score)
127.0.0.1:6379> zadd z1 10 a 20 b 30 c 40 d 50 e
(integer) 5
127.0.0.1:6379> zrange z1 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> zremrangebyscore z1 20 40
(integer) 3
127.0.0.1:6379> zrange z1 0 -1
1) "a"
2) "e"
- zincrby key 加几 指定元素(给key中的指定元素的score加几)
127.0.0.1:6379> zadd z3 10 a 20 b 30 c 40 d 50 e
(integer) 5
127.0.0.1:6379> zrange z3 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> zincrby z3 50 a
"60"
127.0.0.1:6379> zrange z3 0 -1
1) "b"
2) "c"
3) "d"
4) "e"
5) "a"
这里看到设定初期score值时,a的score值最小,在给a的score加上50后,a的score值变成最大的了,所以输出的时候,a排在最后显示。
zset的常用命令介绍到这里,zset是有序的存储的数据结构,它被应用在排行榜,销量排行,积分排行等。