hyperf/docs/en/redis.md

324 lines
9.1 KiB
Markdown
Raw Normal View History

2019-12-12 16:24:04 +08:00
# Redis
## Installation
2023-10-09 10:06:25 +08:00
```shell
2019-12-12 16:24:04 +08:00
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 |
2022-11-15 22:48:42 +08:00
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),
],
2023-10-09 10:06:25 +08:00
'options' => [ // Options of Redis Client, see https://github.com/phpredis/phpredis#setoption
\Redis::OPT_PREFIX => env('REDIS_PREFIX', ''),
// or 'prefix' => env('REDIS_PREFIX', ''), v3.0.38 or later
],
2019-12-12 16:24:04 +08:00
],
];
```
2022-11-15 22:48:42 +08:00
`publish` full configuration file using command
```shell
php bin/hyperf.php vendor:publish hyperf/redis
```
2020-08-11 00:28:32 +08:00
## Usage
2019-12-12 16:24:04 +08:00
2022-11-15 22:48:42 +08:00
`hyperf/redis` implements the proxy of `ext-redis` and connection pool. Users can directly inject `\Hyperf\Redis\Redis` through the dependency injection container to use the Redis client. What they actually get is a proxy of `\Redis` object.
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),
],
],
2022-11-15 22:48:42 +08:00
// Add a Redis connection pool named foo
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;
use Hyperf\Context\ApplicationContext;
2019-12-12 16:24:04 +08:00
2022-11-15 22:48:42 +08:00
$container = ApplicationContext::getContainer();
2019-12-12 16:24:04 +08:00
2022-11-15 22:48:42 +08:00
// Obtain or directly inject the RedisFactory class through the DI container
$redis = $container->get(RedisFactory::class)->get('foo');
2019-12-12 16:24:04 +08:00
$result = $redis->keys('*');
```
2022-11-15 22:48:42 +08:00
## Sentinel mode
To enable sentinel mode, you can modify the `.env` or `redis.php` configuration file as follows
Use `;` to split multiple sentinel nodes
2023-10-09 10:06:25 +08:00
```env
2022-11-15 22:48:42 +08:00
REDIS_HOST=
REDIS_AUTH="Redis instance password"
REDIS_PORT=
REDIS_DB=
REDIS_SENTINEL_ENABLE=true
REDIS_SENTINEL_PASSWORD="Redis sentinel password"
REDIS_SENTINEL_NODE=192.168.89.129:26381;192.168.89.129:26380;
```
2023-10-09 10:06:25 +08:00
```php
<?php
2022-11-15 22:48:42 +08:00
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),
],
],
];
```
2020-08-11 00:28:32 +08:00
## Cluster mode
2022-11-15 22:48:42 +08:00
### Use `name`
2023-10-09 10:06:25 +08:00
2020-08-11 00:28:32 +08:00
Configure `cluster`, modify `redis.ini`, or modify `Dockerfile`, as follows:
2023-10-09 10:06:25 +08:00
```shell
2020-08-11 00:28:32 +08:00
# - 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
// Ignore the other irrelevant configurations
return [
'default' => [
'cluster' => [
'enable' => true,
'name' => 'mycluster',
'seeds' => [],
],
],
];
```
2022-11-15 22:48:42 +08:00
### Use seeds
2020-08-11 00:28:32 +08:00
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
2022-11-15 22:48:42 +08:00
Users can modify `options` to set `Redis` configuration options.
2020-08-11 00:28:32 +08:00
2022-11-15 22:48:42 +08:00
For example, modify `Redis` serialization to `PHP` serialization.
2020-08-11 00:28:32 +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' => [
2023-10-09 10:06:25 +08:00
\Redis::OPT_SERIALIZER => \Redis::SERIALIZER_PHP,
// or 'serializer' => \Redis::SERIALIZER_PHP, v3.0.38 or later
2020-08-11 00:28:32 +08:00
],
],
];
```
2022-11-15 22:48:42 +08:00
For example, set the `Redis` never timeout:
2020-08-11 00:28:32 +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' => [
2023-10-09 10:06:25 +08:00
\Redis::OPT_READ_TIMEOUT => -1,
// or 'read_timeout' => -1, v3.0.38 or later
2020-08-11 00:28:32 +08:00
],
],
];
```
> Notice that, in some versions of `phpredis` extension, the value type of `options` has to `string`.