目录
常用命令
启动Redis
bash
[root@localhost /]# redis-cli
127.0.0.1:6379>
查看状态
bash
ping
bash
127.0.0.1:6379> ping
PONG
返回PONG说明服务运行正常
查看当前数据库key的数目
bash
dbsize
bash
127.0.0.1:6379> dbsize
(integer) 1
切换库
Redis默认使用16个库,索引为0到15
bash
select index
bash
127.0.0.1:6379> select 5
OK
127.0.0.1:6379[5]> dbsize
(integer) 0
删除当前数据库的数据
bash
dbsize
bash
127.0.0.1:6379> dbsize
(integer) 1
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> dbsize
(integer) 0
退出连接
bash
quit
bash
exit
key操作
查找所有符合条件的key
bash
keys pattern
*表示多个字符
?表示单个字符
bash
127.0.0.1:6379> keys *
1) "aw"
2) "a"
3) "kl"
bash
127.0.0.1:6379> keys ?l
1) "kl"
查看key是否存在
bash
exists key
存在返回1
不存在返回0
bash
127.0.0.1:6379> exists a
(integer) 1
127.0.0.1:6379> exists xiaoming
(integer) 0
设置key的生存时间
bash
expire key second
超过时间会自动删除,单位是s
设置成功返回1
不成功返回0
bash
127.0.0.1:6379> expire a 3
(integer) 1
127.0.0.1:6379> keys *
1) "aw"
2) "a"
3) "kl"
127.0.0.1:6379> keys *
1) "aw"
2) "kl"
返回key的剩余存在时间
bash
ttl key
没有设置生存时间,key永不过期则返回-1
key不存在则返回-2
否则返回剩余存在时间
查看key的数据类型
bash
type key
bash
127.0.0.1:6379> type aw
string
如果key不存在会返回none
bash
127.0.0.1:6379> type lll
none
删除指定的key
bash
127.0.0.1:6379> keys *
1) "aw"
2) "kl"
127.0.0.1:6379> del aw
(integer) 1
127.0.0.1:6379> keys *
1) "kl"
string操作
给key设置字符串类型
bash
set key value
bash
127.0.0.1:6379> set a abcd
OK
127.0.0.1:6379> get a
"abcd"
获取key的字符串值
bash
127.0.0.1:6379> get a
"abcd"
对key进行加/减操作
如果 key 不存在,则 key 的值先被初始化为 0 再执行incr操作(只能对数字类型的数据操作)
key加1
bash
incr key
bash
127.0.0.1:6379> set idx 1
OK
127.0.0.1:6379> incr idx
(integer) 2
key减1
bash
decr key
bash
127.0.0.1:6379> decr idx
(integer) 1
key加上指定数
bash
incrby key n
bash
127.0.0.1:6379> set num 1
OK
127.0.0.1:6379> incrby num 5
(integer) 6
key减去指定数
bash
decrby key n
bash
127.0.0.1:6379> decrby num 5
(integer) 1
追加内容
bash
append key value
如果key存在,则将value追加到key末尾
如果key不存在,则将key设置值为value
返回值 追加字符串之后的总长度
bash
127.0.0.1:6379> get aw
"acs"
127.0.0.1:6379> append aw 1234
(integer) 7
127.0.0.1:6379> get aw
"acs1234"
返回key的长度
bash
strlen key
返回key存储的字符串的长度
key不存在则返回0
bash
127.0.0.1:6379> strlen aw
(integer) 7
截取字符串
bash
getrange key start end
bash
127.0.0.1:6379> get aw
"acs1234"
127.0.0.1:6379> getrange aw 1 3
"cs1"
从尾部截取
bash
127.0.0.1:6379> get aw
"acs1234"
从offset开始用value覆盖key
bash
setrange key offset value
bash
127.0.0.1:6379> get aw
"acs1234"
127.0.0.1:6379> setrange aw 3 78910
(integer) 8
127.0.0.1:6379> get aw
"acs78910"
同时设置多个key-value
bash
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3
OK
同时获取多个key存储的值
bash
mget key1 [key2] ...
bash
127.0.0.1:6379> mget k1 k2 k3
1) "v1"
2) "v2"
3) "v3"
list操作
从列表的左边添加元素
bash
lpush list value1 [value2...]
bash
127.0.0.1:6379> lpush list a b c
(integer) 3
查询列表中的全部元素
bash
lrange list 0 -1
bash
127.0.0.1:6379> lrange list 0 -1
1) "c"
2) "b"
3) "a"
从列表的右边添加元素
bash
rpush list value1 [value2...]
bash
127.0.0.1:6379> rpush list d e f
(integer) 6
127.0.0.1:6379> lrange list 0 -1
1) "c"
2) "b"
3) "a"
4) "d"
5) "e"
6) "f"
从左边弹出一个元素并返回
bash
lpop list
bash
127.0.0.1:6379> lpop list
"c"
从右边弹出一个元素并返回
bash
rpop list
bash
127.0.0.1:6379> rpop list
"f"
根据指定下标获取元素
bash
lindex list 下标
bash
127.0.0.1:6379> lrange list 0 -1
1) "b"
2) "a"
3) "d"
4) "e"
127.0.0.1:6379> lindex list 2
"d"
获取列表的长度
bash
llen list
bash
127.0.0.1:6379> llen list
(integer) 4
从左边删除指定个数的元素
bash
lrem list n value
bash
127.0.0.1:6379> lrange list 0 -1
1) "a"
2) "a"
3) "c"
4) "b"
5) "a"
6) "b"
7) "a"
8) "d"
9) "e"
127.0.0.1:6379> lrem list 3 a
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "c"
2) "b"
3) "b"
4) "a"
5) "d"
6) "e"
set操作
添加元素
bash
sadd set value1 [value2]...
bash
127.0.0.1:6379> sadd s1 v1 v2 v3
(integer) 3
查询集合所有元素
bash
smembers set
bash
127.0.0.1:6379> smembers s1
1) "v3"
2) "v1"
3) "v2"
判断集合是否存在某个元素
bash
sismember set value
存在返回1
不存在返回0
bash
127.0.0.1:6379> sismember s1 v1
(integer) 1
bash
127.0.0.1:6379> sismember s1 v4
(integer) 0
返回集合中元素的个数
bash
scard set
bash
127.0.0.1:6379> scard s1
(integer) 3
删除集合中指定的元素
bash
srem set value
bash
127.0.0.1:6379> srem s1 v1
(integer) 1
127.0.0.1:6379> scard s1
(integer) 2
随机弹出集合中某个元素
bash
spop set
bash
127.0.0.1:6379> spop s1
"v2"
随机弹出指定个数的元素,但不会从集合中删除
bash
srandmember set n
bash
127.0.0.1:6379> smembers s1
1) "v4"
2) "v2"
3) "v1"
4) "v5"
5) "v6"
6) "v3"
127.0.0.1:6379> srandmember s1 2
1) "v1"
2) "v4"
127.0.0.1:6379> smembers s1
1) "v1"
2) "v5"
3) "v6"
4) "v2"
5) "v3"
6) "v4"
取交集
bash
sinter set1 set2
bash
127.0.0.1:6379> smembers s1
1) "v1"
2) "v5"
3) "v6"
4) "v2"
5) "v3"
6) "v4"
127.0.0.1:6379> smembers s2
1) "a"
2) "v2"
3) "v1"
4) "b"
5) "c"
6) "d"
127.0.0.1:6379> sinter s1 s2
1) "v1"
2) "v2"
取并集
bash
sunion set1 set2
bash
127.0.0.1:6379> sunion s1 s2
1) "v6"
2) "v2"
3) "v3"
4) "v4"
5) "c"
6) "v5"
7) "a"
8) "v1"
9) "b"
10) "d"
取补集
bash
sdiff set1 set2
bash
127.0.0.1:6379> sdiff s1 s2
1) "v3"
2) "v4"
3) "v5"
4) "v6"
zset操作
添加元素
bash
zadd zset score1 value1 [score2 value2]...
bash
127.0.0.1:6379> zadd stu 99 aw 88 xh 77 xm
(integer) 3
显示集合中的所有值
按照分数升序排列
bash
zrange zset 0 -1
bash
127.0.0.1:6379> zrange stu 0 -1
1) "xm"
2) "xh"
3) "aw"
显示集合中值以及其对应的分数
bash
127.0.0.1:6379> zrange stu 0 -1 withscores
1) "xm"
2) "77"
3) "xh"
4) "88"
5) "aw"
6) "99"
取指定区间的元素
bash
zrangebyscore zset score1 score2
bash
127.0.0.1:6379> zrangebyscore stu 1 100
1) "xm"
2) "xh"
3) "aw"
bash
127.0.0.1:6379> zrangebyscore stu 1 100 withscores
1) "xm"
2) "77"
3) "xh"
4) "88"
5) "aw"
6) "99"
按照分数降序排列
bash
zrevrangebyscore zset score1 score2
bash
127.0.0.1:6379> zrevrangebyscore stu 100 1
1) "aw"
2) "xh"
3) "xm"
给指定的元素添加分数
bash
zincrby stu score value
bash
127.0.0.1:6379> zrange stu 0 -1 withscores
1) "xm"
2) "77"
3) "xh"
4) "88"
5) "aw"
6) "99"
7) "kl"
8) "110"
127.0.0.1:6379> zincrby stu 1 aw
"100"
删除指定的元素
bash
zrem zset value
bash
127.0.0.1:6379> zrem stu xm
(integer) 1
127.0.0.1:6379> zrange stu 0 -1 withscores
1) "xh"
2) "88"
3) "aw"
4) "100"
5) "kl"
6) "110"
统计指定分数区间元素的个数
bash
zcount zset score1 score2
bash
127.0.0.1:6379> zcount stu 1 100
(integer) 2
查询指定元素的排名
bash
zrank zset 元素
bash
127.0.0.1:6379> zrank stu xh
(integer) 0
127.0.0.1:6379> zrank stu aw
(integer) 1
hash操作
向hash中存储数据
bash
hset key field value
bash
127.0.0.1:6379> hset user id 10001
(integer) 1
获取指定的数据
bash
hget key field
bash
127.0.0.1:6379> hget user id
"10001"
批量向hash中添加数据
bash
hmset key field value field value
bash
127.0.0.1:6379> hmset user name kl age 19
OK
批量获取数据
bash
127.0.0.1:6379> hmget user name age
1) "kl"
2) "19"
判断key中是否存在指定的field
存在返回1
不存在返回0
bash
127.0.0.1:6379> hexists user name
(integer) 1
获取所有的field
bash
hkeys key
bash
127.0.0.1:6379> hkeys user
1) "id"
2) "name"
3) "age"
获取所有的value
bash
hvals key
bash
127.0.0.1:6379> hvals user
1) "10001"
2) "kl"
3) "19"
删除指定的field及其对应的value
bash
hdel key field
bash
127.0.0.1:6379> hdel user id
(integer) 1
127.0.0.1:6379> hget user id
(nil)
对指定的value的值自增/自减
bash
hincrby key value 1
hincrby key value -1
bash
127.0.0.1:6379> hget user id
"99"
127.0.0.1:6379> hincrby user id 1
(integer) 100
127.0.0.1:6379> hincrby user id -1
(integer) 99
设置指定field的值
bash
hsetnx key field value
如果field存在则不成功,返回0
如果field不存在则成功,返回1
bash
127.0.0.1:6379> hsetnx user time 6:30
(integer) 1
127.0.0.1:6379> hsetnx user time 8:00
(integer) 0