Magento 2 Redis Page Cache and Default Cache Example

From Brian Nelson Ramblings
Jump to: navigation, search

Magento 2 Redis Page Cache and Default Cache Example

Magento provides command line options to configure Redis page and default caching.

Although you can also configure caching by editing the <Magento install dir>app/etc/env.php file, the command line is the recommended method, especially for initial configuration.

The command line provides validation, thereby ensuring the configuration is syntactically correct.

You must install Redis before continuing.

Setting up Redis for Full Page Caching

To configure Redis page caching on Magento, run the setup:config:set command with additional parameters.

bin/magento setup:config:set --page-cache=redis --page-cache-redis-<parameter_name>=<parameter_value>...

where

--page-cache=redis enables Redis page caching. If this feature has already been enabled, omit this parameter.

--page-cache-redis-<parameter_name>=<parameter_value> is a list of parameter/value pairs that configure page caching:

CLI Parameter Parameter Default value
page-cache-redis-server server 127.0.0.1
page-cache-redis-port port 6379
page-cache-redis-db database 0
page-cache-redis-password password

Example command

The following example enables Redis page caching, sets the host to 127.0.0.1 and assigns the database number to 1. All other parameters are set to the default value.

bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=127.0.0.1 --page-cache-redis-db=1

Setting up Redis for Magento 2 Default Caching

Run the setup:config:set command and specify parameters that specific to Redis default caching.

bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-<parameter_name>=<parameter_value>...

where

--cache-backend=redis enables Redis default caching. If this feature has already been enabled, omit this parameter.

--cache-backend-redis-<parameter_name>=<parameter_value> is a list of parameter/value pairs that configure default caching:

CLI Parameter Parameter Default value
page-cache-redis-server server 127.0.0.1
page-cache-redis-port port 6379
page-cache-redis-db database 0
page-cache-redis-password password

Example command

The following example enables Redis default caching, sets the host to 127.0.0.1 and assigns the database number to 0. Redis uses default values for all other parameters.

bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=127.0.0.1 --cache-backend-redis-db=0

Results in you Env.php

As a result of the two example commands, Magento adds lines similar to the following to <Magento install dir>app/etc/env.php:

'cache' => [
    'frontend' => [
        'default' => [
            'backend' => 'Cm_Cache_Backend_Redis',
            'backend_options' => [
                'server' => '127.0.0.1',
                'database' => '0',
                'port' => '6379'
                'compress_data' => '1',
                'compress_tags' => '1',
                'compress_threshold' => '20480',
                'lifetimelimit' => '57600',
            ],
        ],
        'page_cache' => [
            'backend' => 'Cm_Cache_Backend_Redis',
            'backend_options' => [
                'server' => '127.0.0.1',
                'port' => '6379',
                'database' => '1',
                'compress_data' => '1',
                'compress_tags' => '1',
                'compress_threshold' => '20480',
                'lifetimelimit' => '57600',
            ]
        ]
    ]
],

Redis monitor command

In a command prompt on the server on which Redis is running, enter:

redis-cli monitor