2019-04-03 13:49:47 +08:00
|
|
|
# Redis
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
## 安裝
|
2019-04-03 13:49:47 +08:00
|
|
|
|
|
|
|
```
|
|
|
|
composer require hyperf/redis
|
|
|
|
```
|
|
|
|
|
|
|
|
## 配置
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
| 配置項 | 型別 | 預設值 | 備註 |
|
2019-11-07 09:25:57 +08:00
|
|
|
|:--------------:|:-------:|:-----------:|:------------------------------:|
|
2019-11-12 19:46:54 +08:00
|
|
|
| host | string | 'localhost' | Redis 地址 |
|
2019-12-12 16:24:04 +08:00
|
|
|
| auth | string | 無 | 密碼 |
|
|
|
|
| port | integer | 6379 | 埠 |
|
2019-11-07 09:25:57 +08:00
|
|
|
| db | integer | 0 | DB |
|
2019-12-12 16:24:04 +08:00
|
|
|
| cluster.enable | boolean | false | 是否叢集模式 |
|
|
|
|
| cluster.name | string | null | 叢集名 |
|
|
|
|
| cluster.seeds | array | [] | 叢集連線地址陣列 ['host:port'] |
|
|
|
|
| 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),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## 使用
|
|
|
|
|
2020-03-26 11:28:26 +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\Utils\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-03-26 11:28:26 +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-12-12 16:24:04 +08:00
|
|
|
## 多庫配置
|
2019-04-03 13:49:47 +08:00
|
|
|
|
2019-12-12 16:24:04 +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-12-12 16:24:04 +08:00
|
|
|
// 增加一個名為 foo 的 Redis 連線池
|
2019-06-03 02:27:09 +08:00
|
|
|
'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-12-12 16:24:04 +08:00
|
|
|
### 通過代理類使用
|
2019-05-27 15:08:43 +08:00
|
|
|
|
2019-12-12 16:24:04 +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-12-12 16:24:04 +08:00
|
|
|
// 對應的 Pool 的 key 值
|
2019-06-03 02:27:09 +08:00
|
|
|
protected $poolName = 'foo';
|
2019-04-03 13:49:47 +08:00
|
|
|
}
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
// 通過 DI 容器獲取或直接注入當前類
|
2019-06-03 02:27:09 +08:00
|
|
|
$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-12-12 16:24:04 +08:00
|
|
|
### 使用工廠類
|
2019-05-27 15:08:43 +08:00
|
|
|
|
2019-12-12 16:24:04 +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;
|
2019-09-15 15:23:55 +08:00
|
|
|
use Hyperf\Utils\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-12-12 16:24:04 +08:00
|
|
|
// 通過 DI 容器獲取或直接注入 RedisFactory 類
|
2019-09-15 15:23:55 +08:00
|
|
|
$redis = $container->get(RedisFactory::class)->get('foo');
|
2019-05-27 15:08:43 +08:00
|
|
|
$result = $redis->keys('*');
|
|
|
|
```
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
## 叢集模式
|
2019-11-06 15:02:01 +08:00
|
|
|
|
|
|
|
### 使用 `name`
|
|
|
|
|
|
|
|
配置 `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 \
|
|
|
|
```
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
對應 PHP 配置如下
|
2019-11-06 15:02:01 +08:00
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
// 省略其他配置
|
|
|
|
return [
|
|
|
|
'default' => [
|
|
|
|
'cluster' => [
|
|
|
|
'enable' => true,
|
|
|
|
'name' => 'mycluster',
|
|
|
|
'seeds' => [],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
```
|
|
|
|
|
|
|
|
### 使用 seeds
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
當然不配置 name 直接使用 seeds 也是可以的。如下
|
2019-11-06 15:02:01 +08:00
|
|
|
|
|
|
|
```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
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
使用者可以修改 `options`,來設定 `Redis` 配置選項。
|
2019-11-08 15:43:19 +08:00
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
例如修改 `Redis` 序列化為 `PHP` 序列化。
|
2019-11-08 15:43:19 +08:00
|
|
|
|
|
|
|
```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,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
```
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
比如設定 `Redis` 永不超時
|
2019-11-08 15:43:19 +08:00
|
|
|
|
|
|
|
```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,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
```
|
|
|
|
|
2020-03-26 11:28:26 +08:00
|
|
|
> 有的 `phpredis` 擴充套件版本,`option` 的 `value` 必須是 `string` 型別。
|