hyperf/docs/zh-cn/redis.md

315 lines
8.5 KiB
Markdown
Raw Normal View History

2019-04-03 13:49:47 +08:00
# Redis
## 安装
```
composer require hyperf/redis
```
## 配置
2019-11-07 09:25:57 +08:00
| 配置项 | 类型 | 默认值 | 备注 |
|:--------------:|:-------:|:-----------:|:------------------------------:|
2019-11-12 19:46:54 +08:00
| host | string | 'localhost' | Redis 地址 |
2019-11-07 09:25:57 +08:00
| auth | string | 无 | 密码 |
| port | integer | 6379 | 端口 |
| db | integer | 0 | DB |
| cluster.enable | boolean | false | 是否集群模式 |
| cluster.name | string | null | 集群名 |
| cluster.seeds | array | [] | 集群连接地址数组 ['host:port'] |
2019-11-08 15:43:19 +08:00
| pool | object | {} | 连接池配置 |
| options | object | {} | Redis 配置选项 |
2019-04-03 13:49:47 +08:00
```php
<?php
return [
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'auth' => env('REDIS_AUTH', ''),
'port' => (int) env('REDIS_PORT', 6379),
'db' => (int) env('REDIS_DB', 0),
2019-11-06 14:49:01 +08:00
'cluster' => [
2019-11-07 09:25:57 +08:00
'enable' => (bool) env('REDIS_CLUSTER_ENABLE', false),
2019-11-06 14:49:01 +08:00
'name' => null,
'seeds' => [],
],
2019-04-03 13:49:47 +08:00
'pool' => [
'min_connections' => 1,
'max_connections' => 10,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => (float) env('REDIS_MAX_IDLE_TIME', 60),
],
],
];
```
`publish`完整配置文件使用命令
```shell
php bin/hyperf.php vendor:publish hyperf/redis
```
2019-04-03 13:49:47 +08:00
## 使用
`hyperf/redis` 实现了 `ext-redis` 代理和连接池,用户可以直接通过依赖注入容器注入 `\Hyperf\Redis\Redis` 来使用 Redis 客户端,实际获得的是 `\Redis` 的一个代理对象。
2019-04-03 13:49:47 +08:00
```php
2019-09-14 19:20:25 +08:00
<?php
use Hyperf\Context\ApplicationContext;
2019-09-12 21:56:14 +08:00
2019-09-14 19:20:25 +08:00
$container = ApplicationContext::getContainer();
2019-04-03 13:49:47 +08:00
2020-09-16 14:07:18 +08:00
$redis = $container->get(\Hyperf\Redis\Redis::class);
2019-09-14 19:20:25 +08:00
$result = $redis->keys('*');
2019-04-03 13:49:47 +08:00
```
## 多库配置
2019-06-03 02:27:09 +08:00
有时候在实际使用中,一个 `Redis` 库并不满足需求,一个项目往往需要配置多个库,这个时候,我们就需要修改一下配置文件 `redis.php`,如下:
2019-04-03 13:49:47 +08:00
```php
<?php
return [
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'auth' => env('REDIS_AUTH', ''),
'port' => (int) env('REDIS_PORT', 6379),
'db' => (int) env('REDIS_DB', 0),
2019-11-06 14:49:01 +08:00
'cluster' => [
2019-11-07 09:25:57 +08:00
'enable' => (bool) env('REDIS_CLUSTER_ENABLE', false),
2019-11-06 14:49:01 +08:00
'name' => null,
'seeds' => [],
],
2019-04-03 13:49:47 +08:00
'pool' => [
'min_connections' => 1,
'max_connections' => 10,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => (float) env('REDIS_MAX_IDLE_TIME', 60),
],
],
2019-06-03 02:27:09 +08:00
// 增加一个名为 foo 的 Redis 连接池
'foo' => [
2019-04-03 13:49:47 +08:00
'host' => env('REDIS_HOST', 'localhost'),
'auth' => env('REDIS_AUTH', ''),
'port' => (int) env('REDIS_PORT', 6379),
'db' => 1,
'pool' => [
'min_connections' => 1,
'max_connections' => 10,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => (float) env('REDIS_MAX_IDLE_TIME', 60),
],
],
];
```
2019-06-03 02:27:09 +08:00
### 通过代理类使用
2019-05-27 15:08:43 +08:00
2019-06-03 02:27:09 +08:00
我们可以重写一个 `FooRedis` 类并继承 `Hyperf\Redis\Redis` 类,修改 `poolName` 为上述的 `foo`,即可完成对连接池的切换,示例:
2019-04-03 13:49:47 +08:00
```php
2019-06-03 02:27:09 +08:00
<?php
2019-04-03 13:49:47 +08:00
use Hyperf\Redis\Redis;
2019-06-03 02:27:09 +08:00
class FooRedis extends Redis
2019-04-03 13:49:47 +08:00
{
2019-06-03 02:27:09 +08:00
// 对应的 Pool 的 key 值
protected $poolName = 'foo';
2019-04-03 13:49:47 +08:00
}
2019-06-03 02:27:09 +08:00
// 通过 DI 容器获取或直接注入当前类
$redis = $this->container->get(FooRedis::class);
2019-04-03 13:49:47 +08:00
$result = $redis->keys('*');
2019-05-27 15:08:43 +08:00
```
### 使用工厂类
2019-08-18 17:11:40 +08:00
在每个库对应一个固定的使用场景时,通过代理类是一种很好的区分的方法,但有时候需求可能会更加的动态,这时候我们可以通过 `Hyperf\Redis\RedisFactory` 工厂类来动态的传递 `poolName` 来获得对应的连接池的客户端,而无需为每个库创建代理类,示例如下:
2019-05-27 15:08:43 +08:00
```php
2019-06-03 02:27:09 +08:00
<?php
2019-05-27 15:08:43 +08:00
use Hyperf\Redis\RedisFactory;
use Hyperf\Context\ApplicationContext;
2019-05-27 15:08:43 +08:00
2019-09-15 15:23:55 +08:00
$container = ApplicationContext::getContainer();
2019-05-27 15:08:43 +08:00
2019-09-15 15:23:55 +08:00
// 通过 DI 容器获取或直接注入 RedisFactory 类
$redis = $container->get(RedisFactory::class)->get('foo');
2019-05-27 15:08:43 +08:00
$result = $redis->keys('*');
```
## 哨兵模式
开启哨兵模式可以在`.env`或 `redis.php` 配置文件中修改如下
多个哨兵节点使用`;`分割
```
REDIS_HOST=
REDIS_AUTH=Redis实例密码
REDIS_PORT=
REDIS_DB=
REDIS_SENTINEL_ENABLE=true
REDIS_SENTINEL_PASSWORD=Redis哨兵密码
REDIS_SENTINEL_NODE=192.168.89.129:26381;192.168.89.129:26380;
```
```
return [
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'auth' => env('REDIS_AUTH', null),
'port' => (int) env('REDIS_PORT', 6379),
'db' => (int) env('REDIS_DB', 0),
'timeout' => 30.0,
'reserved' => null,
'retry_interval' => 0,
'sentinel' => [
'enable' => (bool) env('REDIS_SENTINEL_ENABLE', false),
'master_name' => env('REDIS_MASTER_NAME', 'mymaster'),
'nodes' => explode(';', env('REDIS_SENTINEL_NODE', '')),
'persistent' => false,
'read_timeout' => 30.0,
'auth' => env('REDIS_SENTINEL_PASSWORD', ''),
],
'pool' => [
'min_connections' => 1,
'max_connections' => 10,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => (float) env('REDIS_MAX_IDLE_TIME', 60),
],
],
];
```
2019-11-06 15:02:01 +08:00
## 集群模式
### 使用 `name`
2019-11-06 15:02:01 +08:00
配置 `cluster`,修改修改 `redis.ini`,也可以修改 `Dockerfile` 如下
```
# - config PHP
&& { \
echo "upload_max_filesize=100M"; \
echo "post_max_size=108M"; \
echo "memory_limit=1024M"; \
echo "date.timezone=${TIMEZONE}"; \
echo "redis.clusters.seeds = \"mycluster[]=localhost:7000&mycluster[]=localhost:7001\""; \
echo "redis.clusters.timeout = \"mycluster=5\""; \
echo "redis.clusters.read_timeout = \"mycluster=10\""; \
echo "redis.clusters.auth = \"mycluster=password\"";
} | tee conf.d/99-overrides.ini \
```
对应 PHP 配置如下
```php
<?php
// 省略其他配置
return [
'default' => [
'cluster' => [
'enable' => true,
'name' => 'mycluster',
'seeds' => [],
],
],
];
```
### 使用 seeds
当然不配置 name 直接使用 seeds 也是可以的。如下
```php
<?php
// 省略其他配置
return [
'default' => [
'cluster' => [
'enable' => true,
'name' => null,
'seeds' => [
'192.168.1.110:6379',
'192.168.1.111:6379',
],
],
],
];
2019-11-08 15:43:19 +08:00
```
## Options
用户可以修改 `options`,来设置 `Redis` 配置选项。
例如修改 `Redis` 序列化为 `PHP` 序列化。
```php
<?php
declare(strict_types=1);
return [
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'auth' => env('REDIS_AUTH', null),
'port' => (int) env('REDIS_PORT', 6379),
'db' => (int) env('REDIS_DB', 0),
'pool' => [
'min_connections' => 1,
'max_connections' => 10,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => (float) env('REDIS_MAX_IDLE_TIME', 60),
],
'options' => [
Redis::OPT_SERIALIZER => Redis::SERIALIZER_PHP,
],
],
];
```
比如设置 `Redis` 永不超时
```php
<?php
declare(strict_types=1);
return [
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'auth' => env('REDIS_AUTH', null),
'port' => (int) env('REDIS_PORT', 6379),
'db' => (int) env('REDIS_DB', 0),
'pool' => [
'min_connections' => 1,
'max_connections' => 10,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => (float) env('REDIS_MAX_IDLE_TIME', 60),
],
'options' => [
Redis::OPT_READ_TIMEOUT => -1,
],
],
];
```
> 有的 `phpredis` 扩展版本,`option` 的 `value` 必须是 `string` 类型。