这个配置是官方发布的
Magento提供了命令行选项来配置Redis页面和默认缓存。尽管可以通过编辑app/etc/env.php文件来配置缓存,但是建议使用命令行,特别是对于初始配置。命令行提供验证,以确保配置在语法上正确。
您必须先安装Redis,然后再继续。
配置Redis默认缓存
运行setup:config:set命令并指定特定于Redis默认缓存的参数。
$ bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-<parameter_name>=<parameter_value>...
示例命令
以下示例启用Redis页面缓存,将主机设置为127.0.0.1并将数据库号分配为1。所有其他参数均设置为默认值。
$ bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=127.0.0.1 --page-cache-redis-db=1
将类似于以下内容的行添加到app/etc/env.php:
'cache' => [
'frontend' => [
'default' => [
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' => [
'server' => '127.0.0.1',
'database' => '0',
//'password'=>'redis_password',
'port' => '6379'
],
],
'page_cache' => [
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'database' => '1',
//'password'=>'redis_password',
'compress_data' => '0'
]
]
]
],
password视情况填写
新的Redis缓存实现*
从Magento 2.3.5开始,建议使用扩展的Redis缓存实现:\Magento\Framework\Cache\Backend\Redis。
'cache' => [
'frontend' => [
'default' => [
'backend' => '\\Magento\\Framework\\Cache\\Backend\\Redis',
'backend_options' => [
'server' => '127.0.0.1',
'database' => '0',
'port' => '6379'
],
],
],
配置redis作为Magento的session缓存:
修改app/etc/env.php
'session' =>
array (
'save' => 'redis',
'redis' =>
array (
'host' => '127.0.0.1',
'port' => '6379',
'password' => '',
'timeout' => '2.5',
'persistent_identifier' => '',
'database' => '0',
'compression_threshold' => '2048',
'compression_library' => 'gzip',
'log_level' => '1',
'max_concurrency' => '6',
'break_after_frontend' => '5',
'break_after_adminhtml' => '30',
'first_lifetime' => '600',
'bot_first_lifetime' => '60',
'bot_lifetime' => '7200',
'disable_locking' => '0',
'min_lifetime' => '60',
'max_lifetime' => '2592000'
)
),
配置Redis作为Magento2的page储存:app/etc/env.php
return array (
// Other directives
'cache' =>
array (
'frontend' => array(
'default' => array(
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' => array(
'server' => '127.0.0.1', // or absolute path to unix socket
'port' => '6379',
'persistent' => '', // Specify a unique string like "cache-db0" to enable persistent connections.
'database' => '0',
'password' => '',
'force_standalone' => '0', // 0 for phpredis, 1 for standalone PHP
'connect_retries' => '1', // Reduces errors due to random connection failures
'read_timeout' => '10', // Set read timeout duration
'automatic_cleaning_factor' => '0', // Disabled by default
'compress_data' => '1', // 0-9 for compression level, recommended: 0 or 1
'compress_tags' => '1', // 0-9 for compression level, recommended: 0 or 1
'compress_threshold' => '20480', // Strings below this size will not be compressed
'compression_lib' => 'gzip', // Supports gzip, lzf and snappy,
'use_lua' => '0' // Lua scripts should be used for some operations
)
),
'page_cache' => array(
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' => array(
'server' => '127.0.0.1', // or absolute path to unix socket
'port' => '6379',
'persistent' => '', // Specify a unique string like "cache-db0" to enable persistent connections.
'database' => '1', // Separate database 1 to keep FPC separately
'password' => '',
'force_standalone' => '0', // 0 for phpredis, 1 for standalone PHP
'connect_retries' => '1', // Reduces errors due to random connection failures
'lifetimelimit' => '57600', // 16 hours of lifetime for cache record
'compress_data' => '0' // DISABLE compression for EE FPC since it already uses compression
)
)
)
),
// Other directives
);
Redis ping命令
输入以下命令:
$ redis-cli ping
PONG 应该是回应。
如果两个命令均成功,则Redis设置正确。