# redis-cli,Redis命令行界面

redis-cli是Redis命令行界面，一个简单的程序，允许向Redis发送命令，并直接从终端读取服务器发送的回复。

# 命令行用法

‘()’内为返回结果的类型.当我们需要获取返回结果作为下一个命令的输入,或者希望将结果重定向到文件中时,我们可能不需要显示类型信息.

## Host, port, password and database

redis-cli默认会连接127.0.0.1,6379端口.可以使用-h-p选项更改host和port

Some or all of this information can also be provided by using the -u option and a valid URI:

3.2.9没这个

## 持续运行相同的命令

-r

Execute specified command N times.

-i

When -r is used, waits seconds per command.
It is possible to specify sub-second times like -i 0.1.

## CSV格式输出

--csv

Output in CSV format.

## 执行lua脚本

redis-cli对使用Lua脚本的新Lua调试工具提供了广泛的支持，从Redis 3.2开始。 有关此功能，请参阅Redis Lua调试器文档。

# Interactive mode 交互模式

127.0.0.1:6379>是提示符,提示你连接server和database. 当改变server或选择其他非0database时提示符会改变

## 处理连接和重新连接

connect命令,指定hostname和port即可连接另一个instance

## Editing, history and completion

CLI也可以通过按TAB键执行命令补全，如下例所示：

## Showing help about Redis commands

Redis has a number of commands and sometimes, as you test things, you may not remember the exact order of arguments. redis-cli provides online help for most Redis commands, using the help command. The command can be used in two forms:

help @ shows all the commands about a given category. The categories are: @generic, @list, @set, @sorted_set, @hash, @pubsub, @transactions, @connection, @server, @scripting, @hyperloglog.
help shows specific help for the command given as argument.
For example in order to show help for the PFADD command, use:

PFADD key element [element …] summary: Adds the specified elements to the specified HyperLogLog. since: 2.8.9

Note that help supports TAB completion as well.

# 特殊的操作模式

• Redis命令的命令行执行。
• 交互式的“类似REPL”的用法。

However the CLI performs other auxiliary tasks related to Redis that are explained in the next sections:

• Monitoring tool to show continuous stats about a Redis server. 监控工具
• Scanning a Redis database for very large keys. 扫描database找出very large key
• Key space scanner with pattern matching. 模式匹配扫描
• Acting as a Pub/Sub client to subscribe to channels. 作为Pub/Sub client订阅频道
• Monitoring the commands executed into a Redis instance. 监视执行到Redis实例中的命令
• Checking the latency of a Redis server in different ways. 以不同的方式检查Redis服务器的延迟
• Checking the scheduler latency of the local computer. 检查本地计算机的调度程序延迟
• Transferring RDB backups from a remote Redis server locally. 从远程Redis服务器传输RDB备份到本地
• Acting as a Redis slave for showing what a slave receives. 扮演Redis的slave,展示slave所接受的东西
• A client for the Lua debugger.

## Getting a list of keys

--pattern模式匹配

Piping the output through the wc command can be used to count specific kind of objects, by key name:

## Pub/sub mode

The CLI is able to publish messages in Redis Pub/Sub channels just using the PUBLISH command. This is expected since the PUBLISH command is very similar to any other command. Subscribing to channels in order to receive messages is different - in this case we need to block and wait for messages, so this is implemented as a special mode in redis-cli. Unlike other special modes this mode is not enabled by using a special option, but simply by using the SUBSCRIBE or PSUBSCRIBE command, both in interactive or non interactive mode:

This is very useful for debugging Pub/Sub issues. To exit the Pub/Sub mode just process CTRL-C.

## Monitoring commands executed in Redis

Similarly to the Pub/Sub mode, the monitoring mode is entered automatically once you use the MONITOR mode. It will print all the commands received by a Redis instance:

## Monitoring the latency of Redis instances

Redis经常用在延迟非常关键的环境中。 延迟涉及应用程序中的多个移动部分，从客户端库到网络堆栈，直到Redis实例本身。

CLI具有多个工具来研究Redis实例的延迟，并了解延迟的最大值，平均值和分布。

## Slave mode

CLI的slave模式是用于Redis开发人员和调试操作的高级功能

## Performing an LRU simulation

Redis通常用作LRU驱逐的缓存。根据keys的数量和为缓存分配的内存量（通过maxmemory指令指定），缓存命中和未命中的数量将会改变。有时，模拟命中率对正确设置缓存非常有用。

CLI有一个特殊的模式，它执行GET和SET操作的模拟，在请求模式中使用80-20％的幂律分布。这意味着20％的keys将被请求80％的时间，这是缓存场景中的普遍分布。

#