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