Redis settings

Basic configuration

The Redis configuration for the Swoft app is in the configuration file app/bean.php. In this file, you can see that the redis array contains the Redis server used by the application:

The default redis.pool connection pool is built-in serialized. You can set, array directly, but the key auto-increment operation performed by set will be invalid. If you want to use redis as a database instead of a cache, you can configure a new connection pool (custom connection pool is not serialized)

 'redis'      => [
    'class'    => RedisDb::class,
    'driver'   => 'phpredis',
    'host'     => '127.0.0.1',
    'port'     => 6379,
    'database' => 0,
    // 'password' => 123445,
], 

The default server configuration should be sufficient for development. Of course, you can also change this array at will according to the environment you use. Simply assign a name, host and port to each Redis server in the configuration file.

  • Class specifies the current configuration driver type
  • The default redis driver is phpredis ( php-ext ), you can also change to predis to see predis.
  • Host connection address default 127.0.0.1
  • Port port default 6379
  • Database cache database index default 0

Detailed configuration

  'redis'      => [
        'class'         => RedisDb::class,
        'host'          => '127.0.0.1',
        'port'          => 6379,
        'database'      => 0,
        'retryInterval' => 10,
        'readTimeout'   => 0,
        'timeout'       => 2,
        'option'        => [          
            'prefix'      => 'xxx',
            'serializer' => Redis::SERIALIZER_PHP
        ],
    ], 
  • retryInterval retry interval
  • readTimeout read timeout 0: no timeout, in seconds
  • Timeout timeout 0: no timeout, in seconds
  • Option phpredis other configuration
    • Prefix redis prefix
    • Serializer serialization supports three options.
      • 0 is 0 to turn off serialization
      • Redis::SERIALIZER_NONE does not serialize data
      • Redis::SERIALIZER_PHP uses php built-in serialization/deserialization
      • Redis::SERIALIZER_IGBINARY Serialization/deserialization using igBinary

Tips: Turn off serializer set serializer option option to 0 to turn off serialization

Redis cluster configuration

Cluster configuration and base configuration is a bit different in the same file, configure the clusters configuration, general configuration of redis on the failure, the connection pool will use 集群配置 to create a connection, because the default configuration of the cluster will be used, if there is no cluster configuration , the ordinary configuration used.

 'redis' => [
    'class'    => \Swoft\Redis\RedisDb::class,
    'option'   => [
        'timeout'    => 3,
        'persistent' => true,
    ],
    'clusters' => [
        [
            'host'         => '127.0.0.1',
            'prefix'       => 'swoft-t',
            'read_timeout' => 1,
            'port'         => 6379,
            'database'     => 1,
            'password'     => 123445,
        ],
    ]
] 
  • Option cluster configuration
    • Timeout timeout default 0 unit seconds
    • Persistent persistence default false
  • Clusters redis clusters node two-dimensional array method configuration
    • Host node address
    • Port node port
    • Password node password
    • Read_timeout read timeout
    • Database node cache database index

Cluster has no serialization configuration

Redis connection pool configuration

Swoft has the same connection pool configuration, if you want to use a different cache database index or a different node. Then it is very suitable to define the connection pool, the configuration is in app\bean.php .

Note: Each worker will create the same connection pool. Not as many as possible, the parameter configuration is based on the machine configuration and the number of workers.

If it is a cluster, you can do this:

  'redis-clusters' => [
    'class'    => \Swoft\Redis\RedisDb::class,
    'option'   => [
        'timeout'    => 3,
        'persistent' => true,
    ],
    'clusters' => [
        [
            'host'         => '127.0.0.1',
            'prefix'       => 'swoft-t',
            'read_timeout' => 1,
            'port'         => 6379,
            'database'     => 1,
            'password'     => 123445,
        ],
    ]
],
'redis.clusters-pool'     => [
    'class'   => \Swoft\Redis\Pool::class,
    'redisDb' => \bean('redis-clusters'),
    'minActive'   => 10,
    'maxActive'   => 20,
    'maxWait'     => 0,
    'maxWaitTime' => 0,
    'maxIdleTime' => 40,
], 

Ordinary redis

 'redis-2'      => [
    'class'         => RedisDb::class,
    'host'          => '127.0.0.2',
    'port'          => 6378,
    'database'      => 1,
    'retryInterval' => 10,
    'readTimeout'   => 0,
    'timeout'       => 2,
    'option'        => [          
        'prefix'      => 'xxx',
        'serializer' => Redis::SERIALIZER_PHP
    ],
],
'redis.pool.2'     => [
    'class'   => \Swoft\Redis\Pool::class,
    'redisDb' => \bean('redis-2'),
    'minActive'   => 10,
    'maxActive'   => 20,
    'maxWait'     => 0,
    'maxWaitTime' => 0,
    'maxIdleTime' => 60,
], 
  • Redis.clusters-pool connection pool name customization
    • Class connection pool driver class generally does not need to change, if you can inherit the Pool class written as your own is also possible
    • redisDb specifies which redisDb configuration to use for the current connection, replacing the one defined in the example.
    • The number of connections that the minActive connection pool needs to maintain.
    • maxActive connection pool maximum number of connections
    • maxWait connection pool waits for the maximum number of connections, if there is no limit to 0 (default)
    • maxWaitTime The maximum wait time for the connection, in seconds, if there is no limit to 0 (default)
    • maxIdleTime connection maximum idle time, in seconds

Use the redis.clusters-pool connection pool:

 Redis::connection('redis.clusters-pool')->get($key); 

You can replace the default connection pool with your own

 'redis.pool'     => [
    'class'   => \Swoft\Redis\Pool::class,
    'redisDb' => \bean('redis-2')
] 

redis.pool is the default connection pool name. It is the same as normal to replace the redisDb attribute in the connection pool with its own defined usage. E.g:

 Redis::set($key, ["name" => "sakura"]); 

Implement predis

If you use predis you need to implement two contracts, Swoft\Redis\Contract\ConnectorInterface and Swoft\Redis\Contract\ConnectionInterface .

Refer to the implementation of PhpRedisConnection and PhpRedisConnector .

Then replace 'driver' => 'phpredis' in redis in the configuration with predis .

It is recommended to use the phpredis extension because it is more efficient

/docs/2.x/en/redis/setting.html
progress-bar