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 open with its own serialization. You can set,array directly but the key self-enhancement operation performed through set will be invalidated. If you want to use Redis as a database instead of caching you can configure a new connection pool (custom connection pool is not open serialization)

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

The default server configuration should be sufficient for development. Of course, you can also change this array at will depending on the environment you are using. Simply give each Redis server a name in the configuration file, host and port you can do so.

  • Class specifies the current configuration driver type
  • Using that Redis driver by default is phpredis (php-ext), you can also replace it with a predis concrete look implementation Predis
  • Host Connection Address default127.0.0.1
  • Port Port Default6379
  • Database Cache Database Index default0

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, unit seconds
  • Timeout Timeout 0: No timeout, unit seconds
  • Additional Configurations for option Phpredis
    • Prefix Redis prefix
    • Serializer serialization supports three options.
      • 0 is 0 and the serialization is turned off.
      • Redis::SERIALIZER_NONE non-serialization of data
      • Redis::SERIALIZER_PHP uses PHP built-in serialization/deserialization
      • Redis::SERIALIZER_IGBINARY the use of igBinary serialization/deserialization

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

Redis cluster configuration

Cluster configuration and basic configuration are a bit different in the same file, configured clusters configuration, the normal Redis configuration is invalidated, the connection pool will also use 集群配置 to create the connection, because the configuration of the cluster will be used by default, if there is no cluster configuration , The normal configuration is 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 defaults to 0 unit seconds
    • Persistent whether to persist default false
  • Clusters Redis clusters node two-dimensional array method configuration
    • Host node Address
    • Port Node Port
    • Password node password
    • Read_timeout Read timeout
    • Cache database index for database nodes

Cluster does not have serialization configuration

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