本人使用 magento CE 1.9 版本。
第一步:安装redis-server
按照magento官方要求,redis版本至少是2.6.9。对于Ubuntu来说,执行apt-get install redis-server,则直接安装redis 2.8.4。然后在命令行执行redis-server,以启动它的后台。
第二步:安装Cm_Cache_Backend_Redis,开启RedisSession
Magento CE 1.9自带了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中配置redis:cache 、redis_session 、session_save。
配置了Redis的local.xml:
<?xml version="1.0"?>
<config>
<global>
<install>
<date><![CDATA[Wed, 19 Nov 2014 07:21:52 +0000]]></date>
</install>
<crypt>
<key><![CDATA[5ca90fa2f4e4afb4bc159c0461345105]]></key>
</crypt>
<disable_local_modules>false</disable_local_modules>
<resources>
<db>
<table_prefix><![CDATA[]]></table_prefix>
</db>
<default_setup>
<connection>
<host><![CDATA[sqldb:3601]]></host>
<username><![CDATA[snap]]></username>
<password><![CDATA[snapper2016]]></password>
<dbname><![CDATA[i2m_mg]]></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>
<slave_database_forum>
<connection>
<host><![CDATA[sqldb:3601]]></host>
<username><![CDATA[snap]]></username>
<password><![CDATA[snapper2016]]></password>
<dbname><![CDATA[i2m_forum]]></dbname>
<initStatements><![CDATA[SET NAMES utf8]]></initStatements>
<model><![CDATA[mysql4]]></model>
<type><![CDATA[pdo_mysql]]></type>
<pdoType><![CDATA[]]></pdoType>
<active>1</active>
</connection>
</slave_database_forum>
</resources>
<cache>
<backend>Cm_Cache_Backend_Redis</backend>
<backend_options>
<server>10.0.30.168</server> <!-- or absolute path to unix socket -->
<port>6379</port>
<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 -->
<database>0</database> <!-- Redis database number; protection against accidental data loss is improved by not sharing databases -->
<password></password> <!-- Specify if your Redis server requires authentication -->
<force_standalone>0</force_standalone><!-- 0 for phpredis, 1 for standalone PHP -->
<connect_retries>1</connect_retries> <!-- Reduces errors due to random connection failures; a value of 1 will not retry after the first failure -->
<read_timeout>10</read_timeout> <!-- Set read timeout duration; phpredis does not currently support setting read timeouts -->
<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, lz4 (as l4z) and snappy -->
<use_lua>0</use_lua> <!-- Set to 1 if Lua scripts should be used for some operations -->
</backend_options>
</cache>
<session_save>db</session_save>
<redis_session>
<!-- All options seen here are the defaults -->
<host>10.0.30.168</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>
session_save改为db,之前是files。也就是说,magento默认将session相关的数据存在文件系统。
第四步:清理magento缓存
进入var/cache,执行rm –rf *命令,以删除安装redis之前magento产生的所有缓存文件。
另外,也可以进入系统后台缓存页面处理。
第五步:验证安装结果
至此,Magento使用Redis作为缓存就已经配置好了。
为了确保已经生效,查看一下缓存的内容
方法一:可以进入redis命令行
# redis-cli
# select 0
# keys *
以上三条执行可以在redis命令行下看到redis数据库0缓存内容的key。如果安装redis之后,有人访问过你的magento站点,那么最后一条命令就能显示至少好几十个key,仔细看的话就能发现有许多magento特有的关键字。
方法二:使用PHP脚本查看
<?php
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('10.0.30.168', 6379);
//查看服务是否运行
echo "Server Status: " . $redis->ping();
// 获取数据并输出
$arrList = $redis->keys("*");
echo "Stored keys: </br>";
print_r($arrList);
?>
第六步:magento中使用redis缓存
$redis = Mage::getModel('Cm_RedisSession_Model_Session');//Cm_RedisSession_Model_Session
//write
$writeResult = $redis ->write('test_id','test data');//return bool
var_dump($writeResult );
//read
$result= $redis ->read('test_id');
var_dump($result);//return 'test data'