Magento CE使用Redis的配置过程

第一步:安装redis-server

按照magento官方要求,redis版本至少是2.6.9。对于Ubuntu来说,执行apt-get install redis-server,则直接安装redis 2.8.4。然后在命令行执行redis-server,以启动它的后台。

第二步:安装Cm_Cache_Backend_Redis

国外的文章说Magento CE 1.8之后的源代码中自带了这个模块,但经过我的核实,Magento CE 1.8.1.0确实是没有的。因此需要手动安装。

https://github.com/colinmollenhour/Cm_Cache_Backend_Redis上下载此模块的源代码,像普通的Magento模块一样装上即可。其实,这个模块只有一个文件,即Cm_Cache_Backend_Redis.php,不需要在app/etc/modules中配置。

第三步:开启RedisSession

Magento CE 1.8.1.0自带了RedisSession模块,需要在app/etc/modules/Cm_RedisSession.xml中启用,即把此文件中的active设置为true,如下所示。

<?xml version="1.0"?>
<
config> <modules> <Cm_RedisSession> <active>true</active> <codePool>community</codePool> </Cm_RedisSession> </modules> </config>

第四步:配置local.xml

在app/etc/local.xml中配置magento使用session作为cache和session的具体参数。

默认的local.xml:

<?xml version="1.0"?>
<config>
    <global>
        <install>
            <date><![CDATA[Tue, 10 Dec 2013 16:55:04 +0000]]></date>
        </install>
        <crypt>
            <key><![CDATA[861433264e3dcac9e0c59e2c5dce710c]]></key>
        </crypt>
        <disable_local_modules>false</disable_local_modules>
        <resources>
            <db>
                <table_prefix><![CDATA[]]></table_prefix>
            </db>
            <default_setup>
                <connection>
                    <host><![CDATA[127.0.0.1]]></host>
                    <username><![CDATA[db_username]]></username>
                    <password><![CDATA[db_password]]></password>
                    <dbname><![CDATA[db_name]]></dbname>
                    <initStatements><![CDATA[SET NAMES utf8]]></initStatements>
                    <model><![CDATA[mysql4]]></model>
                    <type><![CDATA[pdo_mysql]]></type>
                    <pdoType><![CDATA[]]></pdoType>
                    <active>1</active>
                </connection>
            </default_setup>
        </resources>
        <session_save><![CDATA[files]]></session_save>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <frontName><![CDATA[admin]]></frontName>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

配置了Redis的local.xml:

<?xml version="1.0"?>
<config>
  <global>
    <install>
      <date><![CDATA[Tue, 10 Dec 2013 16:55:04 +0000]]></date>
    </install>
    <crypt>
      <key><![CDATA[861433264e3dcac9e0c59e2c5dce710c]]></key>
    </crypt>
    <disable_local_modules>false</disable_local_modules>
    <resources>
      <db>
        <table_prefix><![CDATA[]]></table_prefix>
      </db>
      <default_setup>
        <connection>
          <host><![CDATA[127.0.0.1]]></host>
          <username><![CDATA[db_username]]></username>
          <password><![CDATA[db_password]]></password>
          <dbname><![CDATA[db_name]]></dbname>
          <initStatements><![CDATA[SET NAMES utf8]]></initStatements>
          <model><![CDATA[mysql4]]></model>
          <type><![CDATA[pdo_mysql]]></type>
          <pdoType><![CDATA[]]></pdoType>
          <active>1</active>
        </connection>
      </default_setup>
    </resources>

    <cache>
      <backend>Cm_Cache_Backend_Redis</backend>
      <backend_options>
        <server>127.0.0.1</server>
        <!-- or absolute path to unix socket for better performance -->
        <port>6379</port>
        <database>0</database>
        <password></password>
        <force_standalone>0</force_standalone>
        <!-- 0 for phpredis, 1 for standalone PHP -->
        <connect_retries>1</connect_retries>
        <!-- Reduces errors due to random connection failures -->
        <automatic_cleaning_factor>0</automatic_cleaning_factor>
        <!-- Disabled by default -->
        <compress_data>1</compress_data>
        <!-- 0-9 for compression level, recommended: 0 or 1 -->
        <compress_tags>1</compress_tags>
        <!-- 0-9 for compression level, recommended: 0 or 1 -->
        <compress_threshold>20480</compress_threshold>
        <!-- Strings below this size will not be compressed -->
        <compression_lib>gzip</compression_lib>
        <!-- Supports gzip, lzf and snappy -->
        <persistent>1</persistent>
        <!-- persistence value, 0: not in use, > 0 used as persistence ID -->
      </backend_options>
    </cache>

    <full_page_cache>
      <backend>Cm_Cache_Backend_Redis</backend>
      <backend_options>
        <server>127.0.0.1</server>
        <!-- or absolute path to unix socket for better performance -->
        <port>6379</port>
        <database>1</database>
        <password></password>
        <force_standalone>0</force_standalone>
        <!-- 0 for phpredis, 1 for standalone PHP -->
        <connect_retries>1</connect_retries>
        <!-- Reduces errors due to random connection failures -->
        <automatic_cleaning_factor>0</automatic_cleaning_factor>
        <!-- Disabled by default -->
        <!-- in FPC data is already gzipped, no need to do this twice -->
        <compress_data>0</compress_data>
        <!-- 0-9 for compression level, recommended: 0 or 1 -->
        <compress_tags>1</compress_tags>
        <!-- 0-9 for compression level, recommended: 0 or 1 -->
        <compress_threshold>20480</compress_threshold>
        <!-- Strings below this size will not be compressed -->
        <compression_lib>gzip</compression_lib>
        <!-- Supports gzip, lzf and snappy -->
        <lifetimelimit>43200</lifetimelimit>
        <!-- set lifetime for keys without TTL -->
        <persistent>2</persistent>
      </backend_options>
    </full_page_cache>

    <session_save>db</session_save>
    <redis_session>
      <!-- All options seen here are the defaults -->
      <host>127.0.0.1</host>
      <!-- Specify an absolute path if using a unix socket -->
      <port>6379</port>
      <password></password>
      <!-- Specify if your Redis server requires authentication -->
      <timeout>2.5</timeout>
      <!-- This is the Redis connection timeout, not the locking timeout -->
      <persistent></persistent>
      <!-- Specify unique string to enable persistent connections. E.g.: sess-db0;  
bugs with phpredis and php-fpm are known:  
https://github.com/nicolasff/phpredis/issues/70 -->
      <db>0</db>
      <!-- Redis database number; protection from accidental loss is improved by  
using a unique DB number for sessions -->
      <compression_threshold>2048</compression_threshold>
      <!-- Set to 0 to disable compression (recommended when  
suhosin.session.encrypt=on); known bug with strings over 64k:  
https://github.com/colinmollenhour/Cm_Cache_Backend_Redis/issues/18 -->
      <compression_lib>gzip</compression_lib>
      <!-- gzip, lzf or snappy -->
      <log_level>1</log_level>
      <!-- 0 (emergency: system is unusable), 4 (warning; additional information,  
recommended), 5 (notice: normal but significant condition), 6 (info: informational  
messages), 7 (debug: the most information for development/testing) -->
      <max_concurrency>6</max_concurrency>
      <!-- maximum number of processes that can wait for a lock on one session; for  
large production clusters, set this to at least 10% of the number of PHP processes  
-->
      <break_after_frontend>5</break_after_frontend>
      <!-- seconds to wait for a session lock in the frontend; not as critical as  
admin -->
      <break_after_adminhtml>30</break_after_adminhtml>
      <bot_lifetime>7200</bot_lifetime>
      <!-- Bots get shorter session lifetimes. 0 to disable -->
    </redis_session>
    
  </global>
  <admin>
    <routers>
      <adminhtml>
        <args>
          <frontName><![CDATA[admin]]></frontName>
        </args>
      </adminhtml>
    </routers>
  </admin>
</config>

对比以上两个local.xml可以发现,后者比前者多了cache、full_page_cache和redis_session这三个节点;后者的session_save为db,前者的session_save为files。也就是说,magento默认将session相关的数据存在文件系统。

第五步:清理magento缓存

进入var/cache,执行rm –rf *命令,以删除安装redis之前magento产生的所有缓存文件。

另外,最好进入到admin panel,执行一下Flush Magento Cache和Flush Cache Storage。

第六步:验证安装结果

至此,Magento使用Redis作为缓存就已经配置好了,为了确保已经生效,可以进入redis命令行查看一下缓存的内容。

# redis-cli

# select 0

# keys *

以上三条执行可以在redis命令行下看到redis数据库0缓存内容的key。如果安装redis之后,有人访问过你的magento站点,那么最后一条命令就能显示至少好几十个key,仔细看的话就能发现有许多magento特有的关键字。

posted on 2016-03-13 21:45  烟雨梦江南  阅读(1638)  评论(0)    收藏  举报

导航