Installation

Requirements

This extension works on PHP 7.4 and later. Redis version 2.6.12 or later is required for all components to work properly.

Composer

The preferred way to install this extension is with Composer:

php composer.phar require --prefer-dist yiisoft/yii2-redis

Alternatively, you may directly add this package to the require section of your composer.json

"yiisoft/yii2-redis": "~2.1.0"

…and then run php composer.phar update.

Configuration

The most basic usage involves defining a Connection component in your application configuration:

return [
    // ...
    'components' => [
        'redis' => [
            'class' => yii\redis\Connection::class,
            'hostname' => 'localhost',
            'port' => 6379,
            'database' => 0,
        ],
    ]
];

You can then interact with the redis store via that redis application component:

Yii::$app->redis->set('mykey', 'some value');
echo Yii::$app->redis->get('mykey');

See yii\redis\Connection for a full list of available methods.

The included cache, mutex, and session drivers require additional configuration, and rely on the existence of this redis connection component, by default.