Redis 常用命令
启动客户端
redis-cli
127.0.0.1:6379>
默认启动6379端口
退出
127.0.0.1:6379> exit
登录帮助
redis-cli -h
常用登录的命令
命令 | 作用 |
---|---|
-h | 具体哪台主机 |
-p | 哪一个端口 |
-a | 密码 |
-n | 哪一个数据库0-15库,默认有16个库 |
-raw | 格式化,远程连接工具格式为主 |
redis-cli -p 6380 ## 登录6380端口的redis
127.0.0.1:6380> set k380:1 hello ## 默认0库,设置k380:1 值为hello
OK
127.0.0.1:6380> get k380:1
"hello"
127.0.0.1:6380> SELECT 8 ## 切换到8库
OK
127.0.0.1:6380[8]> get k380:1
(nil) ##nil未查到
127.0.0.1:6380[8]>
遇到不会的怎么办,登录redis后,使用help命令
127.0.0.1:6380> help
redis-cli 6.2.4
To get help about Redis commands type:
"help @<group>" to get a list of commands in <group>
"help <command>" for help on <command>
"help <tab>" to get a list of possible help topics
"quit" to exit
To set redis-cli preferences:
":set hints" enable online hints
":set nohints" disable online hints
Set your preferences in ~/.redisclirc
127.0.0.1:6380>
通用组全局配置
help @generic
常用的命令
key
命令 | 作用 |
---|---|
DEL | 删除key |
EXISTS | 是否存在 |
EXPIRE | 定义时间的,key的存活时间(内存优化) |
KEYS | 接通配符,看下key有哪些 |
MOVE | 移动key |
OBJECT | 可以查询key的object定义 |
PERSIST | 将定义过期的key持久化下来 |
TYPE | 查看value类型 |
查看所有key
127.0.0.1:6379> keys *
1) "k3"
2) "k2"
3) "k1"
key里面有一个属性type,描述的是里面value的类型
127.0.0.1:6379> set k1 99
OK
127.0.0.1:6379> type k1
string
127.0.0.1:6379> set k2 hello
OK
127.0.0.1:6379> object encoding k2
"embstr"
127.0.0.1:6379> object encoding k1
"int"
127.0.0.1:6379> incr k1
(integer) 100
127.0.0.1:6379> get k1
"100"
127.0.0.1:6379> incrby k1 22
(integer) 122
127.0.0.1:6379> get k1
"122"
127.0.0.1:6379> decr k1
(integer) 121
127.0.0.1:6379> get k1
"121"
127.0.0.1:6379> decrby k1 22
(integer) 99
127.0.0.1:6379> incrbyfloat k1 0.5
"99.5"
127.0.0.1:6379> get k1
"99.5"
127.0.0.1:6379> set k3 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
OK
127.0.0.1:6379> object encoding k3
"embstr"
127.0.0.1:6379> append k3 jjjjj
(integer) 44
127.0.0.1:6379> object encoding k3
"raw"
127.0.0.1:6379>
清库命令
FLUSHDB
FLUSHALL
两个命令都可以清库
redis类型
类型 | 含义 |
---|---|
String(Byte) | 字符串 |
list | 数组 |
set | set集合 |
sorted_set | 有序set集合 |
hash | hash集合 |