hyperf/docs/en/redis.md

264 lines
7.1 KiB
Markdown
Raw Normal View History

2019-12-12 16:24:04 +08:00
# Redis
## Installation
```
composer require hyperf/redis
```
2020-08-11 00:28:32 +08:00
## Configuration
2019-12-12 16:24:04 +08:00
2020-08-11 00:28:32 +08:00
| Config | Type | Default Value | Comment |
2019-12-12 16:24:04 +08:00
|:------:|:-------:|:-----------:|:---------:|
2020-08-11 00:28:32 +08:00
| host | string | 'localhost' | The host of Redis Server |
| auth | string | null | The password of Redis Server |
| port | integer | 6379 | The port of Redis Server |
| db | integer | 0 | The DB of Redis Server |
| cluster.enable | boolean | false | Is it cluster mode ? |
| cluster.name | string | null | The cluster name |
| cluster.seeds | array | [] | The seeds of cluster, format: ['host:port'] |
| pool | object | {} | The connection pool |
| options | object | {} | The options of Redis Client |
2019-12-12 16:24:04 +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),
2020-08-11 00:28:32 +08:00
'cluster' => [
'enable' => (bool) env('REDIS_CLUSTER_ENABLE', false),
'name' => null,
'seeds' => [],
],
2019-12-12 16:24:04 +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-08-11 00:28:32 +08:00
## Usage
2019-12-12 16:24:04 +08:00
2020-08-11 00:28:32 +08:00
`hyperf/redis` implemented the proxy of `ext-redis` and the connection pool, you could use `\Redis` class directly.
2019-12-12 16:24:04 +08:00
```php
<?php
$redis = $this->container->get(\Redis::class);
$result = $redis->keys('*');
```
2020-08-11 00:28:32 +08:00
## Multi-resource configuration
2019-12-12 16:24:04 +08:00
2020-08-11 00:28:32 +08:00
Sometimes, a single `Redis` resource can not meet the needs, and a project often needs to configure multiple resources. At this time, we could modify the configuration file `redis.php` as follows:
2019-12-12 16:24:04 +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),
2020-08-11 00:28:32 +08:00
'cluster' => [
'enable' => (bool) env('REDIS_CLUSTER_ENABLE', false),
'name' => null,
'seeds' => [],
],
2019-12-12 16:24:04 +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-08-11 00:28:32 +08:00
// Added a named `foo` redis connection pool
2019-12-12 16:24:04 +08:00
'foo' => [
'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),
],
],
];
```
2020-08-11 00:28:32 +08:00
### Use through proxy class
2019-12-12 16:24:04 +08:00
2020-08-11 00:28:32 +08:00
We could rewrite a `FooRedis` class and inherit the `Hyperf\Redis\Redis` class, and modify the `poolName` property to the above `foo`, to complete the switch of the connection pool, for example:
2019-12-12 16:24:04 +08:00
```php
<?php
use Hyperf\Redis\Redis;
class FooRedis extends Redis
{
2020-08-11 00:28:32 +08:00
// The key value of the corresponding Pool
2019-12-12 16:24:04 +08:00
protected $poolName = 'foo';
}
2020-08-11 00:28:32 +08:00
// Obtain or directly inject the current class through the DI container
2019-12-12 16:24:04 +08:00
$redis = $this->container->get(FooRedis::class);
$result = $redis->keys('*');
```
2020-08-11 00:28:32 +08:00
### Use through factory
2019-12-12 16:24:04 +08:00
2020-08-11 00:28:32 +08:00
When each resource corresponds to a static scene, the proxy class is a good way to distinguish the resources, but sometimes the demand may be more dynamic. At this time, we could use the `Hyperf\Redis\RedisFactory` factory class to dynamically pass `poolName` argument to retrieve the client of the corresponding connection pool without creating a proxy class for each resource, for example:
2019-12-12 16:24:04 +08:00
```php
<?php
use Hyperf\Redis\RedisFactory;
2020-08-11 00:28:32 +08:00
// Obtain or directly inject the RedisFactory class through the DI container
2019-12-12 16:24:04 +08:00
$redis = $this->container->get(RedisFactory::class)->get('foo');
$result = $redis->keys('*');
```
2020-08-11 00:28:32 +08:00
## Cluster mode
### Cluster name
Configure `cluster`, modify `redis.ini`, or modify `Dockerfile`, as follows:
```
# - 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 \
```
The corresponding PHP configuration is as follows
```php
<?php
// ./config/autoload/redis.php
// Ignore the other irrelevant configurations
return [
'default' => [
'cluster' => [
'enable' => true,
'name' => 'mycluster',
'seeds' => [],
],
],
];
```
### Seeds
Of course, it is also available to use `seeds` directly without configuring the `name`, as follows:
```php
<?php
// Ignore the other irrelevant configurations
return [
'default' => [
'cluster' => [
'enable' => true,
'name' => null,
'seeds' => [
'192.168.1.110:6379',
'192.168.1.111:6379',
],
],
],
];
```
## Options
You could define `options` configuration to set the options of Redis Client.
For example, use PHP Serializer to serialize the result:
```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,
],
],
];
```
For example, set the redis client never timeout:
```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,
],
],
];
```
> Notice that, in some versions of `phpredis` extension, the value type of `options` has to `string`.