1、查看redis是否启动
[root@localhost ~]
root 4270 1 0 05:51 ? 00:00:33 /usr/local/redis/bin/redis-server *:6379
root 8671 6858 0 11:56 pts/0 00:00:00 grep --color=auto redis
[root@localhost ~]
2、通过客户端连接redis
[root@localhost ~]
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
3、切换到db4数据库
127.0.0.1:6379> select 4
OK
127.0.0.1:6379[4]> keys *
(empty array)
127.0.0.1:6379[4]>
4、将一个或多个member元素及其score值加入到有序集key当中
127.0.0.1:6379[4]> zadd scoreInfo 75 chinese 85 math 80 english
(integer) 3
127.0.0.1:6379[4]>
5、升序返回有序集key
127.0.0.1:6379[4]> zrange scoreInfo 0 -1
1) "chinese"
2) "english"
3) "math"
127.0.0.1:6379[4]>
6、升序返回有序集key,让分数一起和值返回的结果集
127.0.0.1:6379[4]> zrange scoreInfo 0 -1 withscores
1) "chinese"
2) "75"
3) "english"
4) "80"
5) "math"
6) "85"
127.0.0.1:6379[4]>
7、降序返回有序集key,让分数一起和值返回到结果集(reverse)
在Redis中,zrevrange 的英文全称是 ZSET Reverse Range。它是Redis中有序集合(ZSET)的一个操作命令,用于从大到小获取有序集合中指定排名范围的元素。
127.0.0.1:6379[4]> zrevrange scoreInfo 0 -1 withscores
1) "math"
2) "85"
3) "english"
4) "80"
5) "chinese"
6) "75"
127.0.0.1:6379[4]>
8、返回有序集key,所有score值介于min和max之间,默认递增排序
127.0.0.1:6379[4]> zrangebyscore scoreInfo 70 80 withscores
1) "chinese"
2) "75"
3) "english"
4) "80"
127.0.0.1:6379[4]>
9、从大到小获取有序集合(sorted set)中指定分数范围内的元素
127.0.0.1:6379[4]> zrevrangebyscore scoreInfo 90 85 withscores
1) "math"
2) "85"
127.0.0.1:6379[4]>
10、为元素的score加上增量
127.0.0.1:6379[4]> zrange scoreInfo 0 -1 withscores
1) "chinese"
2) "75"
3) "english"
4) "80"
5) "math"
6) "85"
127.0.0.1:6379[4]> zincrby scoreInfo 8 chinese
"83"
127.0.0.1:6379[4]> zrange scoreInfo 0 -1 withscores
1) "english"
2) "80"
3) "chinese"
4) "83"
5) "math"
6) "85"
127.0.0.1:6379[4]>
11、删除该集合下,指定值的元素
127.0.0.1:6379[4]> zrem scoreInfo english
(integer) 1
127.0.0.1:6379[4]> zrange scoreInfo 0 -1 withscores
1) "chinese"
2) "83"
3) "math"
4) "85"
127.0.0.1:6379[4]>
12、统计该集合,分数区间内的元素个数
127.0.0.1:6379[4]> zrange scoreInfo 0 -1 withscores
1) "chinese"
2) "83"
3) "math"
4) "85"
127.0.0.1:6379[4]> zcount scoreInfo 0 83
(integer) 1
127.0.0.1:6379[4]> zcount scoreInfo 83 85
(integer) 2
127.0.0.1:6379[4]>
13、返回该值在集合中的排名,从0开始
127.0.0.1:6379[4]> zrange scoreInfo 0 -1 withscores
1) "chinese"
2) "83"
3) "math"
4) "85"
127.0.0.1:6379[4]> zrank scoreInfo chinese
(integer) 0
127.0.0.1:6379[4]> zrank scoreInfo math
(integer) 1
127.0.0.1:6379[4]>