服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > magento教程 >

为Magento2配置redis

时间:2017-01-16 23:24来源:未知 作者:最模板 点击:
默认的Magento2是使用Zend_Cache_Backend_File作为缓存处理,更改为redis,可以让Magento2运行速度更快. 使用redis可以作为我们Magento2的session及page储存,使用redis可以完全替代memcached,并且更好的运行

默认的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:

首先找到:

 

<!--?xml version="1.0"?-->
 
<!-- app/etc/di.xml -->
 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation="../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
 
    <!-- Other directives -->
 
    <type name="Magento\Framework\App\Cache\Frontend\Pool">
        <arguments>
            <argument name="frontendSettings" xsi:type="array">
                <item name="page_cache" xsi:type="array">
                    <item name="backend_options" xsi:type="array">
                        <item name="cache_dir" xsi:type="string">page_cache</item>
                    </item>
                </item>
            </argument>
        </arguments>
    </type>
 
    <!-- Other directives -->
 
</config>

然后把上面内容替换为

<!--?xml version="1.0"?-->
 
<!-- app/etc/di.xml -->
 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation="../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
 
    <!-- Other directives -->
 
    <type name="Magento\Framework\App\Cache\Frontend\Pool">
        <arguments>
            <argument name="frontendSettings" xsi:type="array">
                <!-- Place general cache inside Redis, use database 0 -->
                <item name="default" xsi:type="array">
                    <item name="backend" xsi:type="string">Cm_Cache_Backend_Redis</item>
                    <item name="backend_options" xsi:type="array">
 
                        <!-- IP or absolute path to unix socket -->
                        <item name="server" xsi:type="string">127.0.0.1</item>
 
                        <item name="port" xsi:type="number">6379</item>
 
                        <!-- Specify a unique string like "cache-db0" to enable persistent connections. -->
                        <!--<item name="persistent" xsi:type="number">cache-db0</item>-->
 
                        <!-- Database number, unique per cache frontend -->
                        <item name="database" xsi:type="number">0</item>
 
                        <!--<item name="password" xsi:type="string"></item>-->
 
                        <!-- 0 for phpredis, 1 for standalone PHP -->
                        <item name="force_standalone" xsi:type="number">0</item>
 
                        <!-- Reduces errors due to random connection failures -->
                        <item name="connect_retries" xsi:type="number">1</item>
 
                        <!-- Set read timeout duration -->
                        <item name="read_timeout" xsi:type="number">10</item>
 
                        <!-- Disabled by default -->
                        <item name="automatic_cleaning_factor" xsi:type="number">0</item>
 
                        <!-- 0-9 for compression level, recommended: 0 or 1 -->
                        <item name="compress_data" xsi:type="number">1</item>
 
                        <!-- 0-9 for compression level, recommended: 0 or 1 -->
                        <item name="compress_tags" xsi:type="number">1</item>
 
                        <!-- Strings below this size will not be compressed -->
                        <item name="compress_threshold" xsi:type="number">20480</item>
 
                        <!-- Supports gzip, lzf and snappy -->
                        <item name="compression_lib" xsi:type="string">gzip</item>
                    </item>
                </item>
 
                <!-- Place Page Cache inside Redis, use database 1 -->
                <item name="page_cache" xsi:type="array">
                    <item name="backend" xsi:type="string">Cm_Cache_Backend_Redis</item>
                    <item name="backend_options" xsi:type="array">
                        <item name="cache_dir" xsi:type="string">page_cache</item>
 
                        <!-- IP or absolute path to unix socket -->
                        <item name="server" xsi:type="string">127.0.0.1</item>
 
                        <item name="port" xsi:type="number">6379</item>
 
                        <!-- Specify a unique string like "cache-db0" to enable persistent connections. -->
                        <!--<item name="persistent" xsi:type="number">cache-db0</item>-->
 
                        <!-- Database number, unique per cache frontend -->
                        <item name="database" xsi:type="number">1</item>
 
                        <!--<item name="password" xsi:type="string"></item>-->
 
                        <!-- 0 for phpredis, 1 for standalone PHP -->
                        <item name="force_standalone" xsi:type="number">0</item>
 
                        <!-- Reduces errors due to random connection failures -->
                        <item name="connect_retries" xsi:type="number">1</item>
 
                        <!-- 16 hours of lifetime for cache record -->
                        <item name="lifetimelimit" xsi:type="number">57600</item>
 
                        <!-- DISABLE compression for EE FPC since it already uses compression -->
                        <item name="compress_data" xsi:type="number">0</item>
                    </item>
                </item>
            </argument>
        </arguments>
    </type>
 
    <!-- Other directives -->
 
</config>

(责任编辑:最模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容