默认的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的下载地址: 编译安装好redis之后,我们就需要配置我们的Magento2使用redis作为缓存. 首先配置redis作为Magento的session缓存:
配置Redis作为Magento2的page储存:
配置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>
(责任编辑:好模板) |