Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010年3月15日起,Redis的开发工作由VMware主持。从2013年5月开始,Redis的开发由Pivotal赞助。

默认的Magento2是使用Zend_Cache_Backend_File作为缓存处理,更改为redis,可以让Magento2运行速度更快.

使用redis可以作为我们Magento2的session及page储存,使用redis可以完全替代memcached,并且更好的运行.

Redis通过索引文件中的tag标记来工作,不需要对每个缓存文件进行完整的扫描,所以速度更快。

从Magento 2.0.6开始,你就可以使用Redis或者Memcached存储session(Magento 2.0.6之前也可以,但是有bug).

Redis的下载地址:
https://redis.io/download

编译安装好redis之后,我们就需要配置我们的Magento2使用redis作为缓存.

首先配置redis作为Magento的session缓存:

// vi 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储存:

// vi 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
 
);

配置Magento编译环境(产品模式)中使用redis:

首先找到:

page_cache

然后把上面内容替换为

Cm_Cache_Backend_Redis 127.0.0.1 6379 0 0 1 10 0 1 1 20480 gzip Cm_Cache_Backend_Redis page_cache 127.0.0.1 6379 1 0 1 57600 0

Redis相关的内容,大家如果有什么疑问.可以直接留言,待我再更新此blog.

相关文档:

http://devdocs.magento.com/guides/v2.0/config-guide/redis/config-redis.html

http://devdocs.magento.com/guides/v2.0/config-guide/redis/redis-pg-cache.html

http://devdocs.magento.com/guides/v2.0/config-guide/redis/redis-session.html