Difference between revisions of "Install Redis Caching for Magento"

From Brian Nelson Ramblings
Jump to: navigation, search
(How to flush your Redis Cache)
(How to Check if your Redis Cache is Working)
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
==Install Redis Caching for Magento==
 
==Install Redis Caching for Magento==
 
+
[[File:Img-redis.jpg|frameless|right|Install Redis Cache Magento ]]
 
How to install redis for backend caching?  You see alot of talk about using apc and memcached for Magento caching, but if you are looking for a more stable caching, then redis is for you.
 
How to install redis for backend caching?  You see alot of talk about using apc and memcached for Magento caching, but if you are looking for a more stable caching, then redis is for you.
  
Line 25: Line 25:
 
  tar xzf Cm_Cache_Backend_Redis.tar.gz  
 
  tar xzf Cm_Cache_Backend_Redis.tar.gz  
  
  cd colinmollenhour-Cm_Cache_Backend_Redis-9bd58d2/
+
  cd colinmollenhour-Cm_Cache_Backend_Redis-*/
  
  cp Cm_Cache_Backend_Redis/Cm /path/to/magento/app/code/local/
+
  cp -r Cm /path/to/magento/app/code/local/
  
  cp Cm_Cache_Backend_Redis/lib/Credis /path/to/magento/lib/
+
  cp -r lib/Credis /path/to/magento/lib/
  
 
  cat > /path/to/magento/app/etc/modules/Cm_Cache.xml <<eof
 
  cat > /path/to/magento/app/etc/modules/Cm_Cache.xml <<eof
Line 51: Line 51:
 
  tar xzf credis.tar.gz
 
  tar xzf credis.tar.gz
  
  cd colinmollenhour-credis-5a48ac8/
+
  cd colinmollenhour-credis-*/
  
  cp ./* /path/to/magento/lib/Credis/
+
  cp -r ./* /path/to/magento/lib/Credis/
  
 
  cd /path/to/magento/
 
  cd /path/to/magento/
Line 61: Line 61:
 
  chown -R username. app/code/local/Cm/
 
  chown -R username. app/code/local/Cm/
  
  chown -R username. app/etc/modules/Cm_Cache.
+
  chown -R username. app/etc/modules/Cm_Cache.xml
  
 
===4) Configure Magento to use Redis ===
 
===4) Configure Magento to use Redis ===
Line 68: Line 68:
 
I recommend always making a backup copy of your local.xml file before making any changes to it
 
I recommend always making a backup copy of your local.xml file before making any changes to it
  
  cp app/etc/local.xml app/etc/local.xml-bk-date
+
  cp app/etc/local.xml app/etc/local.xml-bk-$(date +%F)
  
 
Now lets install redis as a backend cache
 
Now lets install redis as a backend cache
Line 74: Line 74:
 
  vim app/etc/local.xml
 
  vim app/etc/local.xml
  
Now add the following to your configuration
+
Now add the following to your configuration for redis using TCP
  
 
  <!-- This is a child node of config/global -->
 
  <!-- This is a child node of config/global -->
Line 82: Line 82:
 
     <server>127.0.0.1</server> <!-- or absolute path to unix socket -->
 
     <server>127.0.0.1</server> <!-- or absolute path to unix socket -->
 
     <port>6379</port>
 
     <port>6379</port>
 +
    <persistent></persistent> <!-- Specify unique string to enable persistent connections. E.g.: sess-db0;
 +
    <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>
 +
 +
If you are using a Socket for your redis instance
 +
 +
<cache>
 +
  <backend>Cm_Cache_Backend_Redis</backend>
 +
  <backend_options>
 +
    <server>/var/tmp/redis-multi_path/to/my.sock</server> <!-- or absolute path to unix socket -->
 +
    <port></port>
 
     <persistent></persistent> <!-- Specify unique string to enable persistent connections. E.g.: sess-db0;
 
     <persistent></persistent> <!-- Specify unique string to enable persistent connections. E.g.: sess-db0;
 
     <database>0</database> <!-- Redis database number; protection against accidental data loss is improved by not sharing databases -->
 
     <database>0</database> <!-- Redis database number; protection against accidental data loss is improved by not sharing databases -->
Line 118: Line 140:
  
 
  echo 'flushall' | redis-cli -h 127.0.0.1 -p 6379
 
  echo 'flushall' | redis-cli -h 127.0.0.1 -p 6379
 +
or
 +
echo 'flushall' | redis-cli -s /path/to/sock
 +
 +
===How to Check if your Redis Cache is Working===
 +
 +
To verify you are getting connections to your redis instance
 +
 +
echo 'info' | redis-cli -h 127.0.0.1 -p 6379 | grep connections
 +
or
 +
echo 'info' | redis-cli -s /path/to/sock | grep connections
  
 
===Additioanl Articles===
 
===Additioanl Articles===
Line 125: Line 157:
 
  --> [[Install Redis Caching for Magento]]
 
  --> [[Install Redis Caching for Magento]]
 
  --> [[Install Redis Full Page Caching for Magento Enterprise]]
 
  --> [[Install Redis Full Page Caching for Magento Enterprise]]
 +
--> [[Basic Redis commands - Cheat Sheet]]

Latest revision as of 17:38, 24 June 2014

Install Redis Caching for Magento

Install Redis Cache Magento

How to install redis for backend caching? You see alot of talk about using apc and memcached for Magento caching, but if you are looking for a more stable caching, then redis is for you.

1) Install Redis and phpRedis

This can be done using yum, which makes this step pretty easy.

yum install redis php-pecl-redis
service redis start
chkconfig redis on

Now to verify that redis is chkconfig on

chkconfig --list redis

2) Install Cm_Cache_Backend_Redis Magento Extension

This is the extension that is required on versions of CE 1.7.0.2 and older, as Magento 1.8.0.0 it comes pre installed.

wget -O Cm_Cache_Backend_Redis.tar.gz https://github.com/colinmollenhour/Cm_Cache_Backend_Redis/tarball/master
tar xzf Cm_Cache_Backend_Redis.tar.gz 
cd colinmollenhour-Cm_Cache_Backend_Redis-*/
cp -r Cm /path/to/magento/app/code/local/
cp -r lib/Credis /path/to/magento/lib/
cat > /path/to/magento/app/etc/modules/Cm_Cache.xml <<eof
<?xml version="1.0" encoding="UTF-8"?>
<config>
   <modules>
       <Cm_Cache>
           <active>true</active>
           <codePool>local</codePool>
       </Cm_Cache>
   </modules>
</config>
eof

3) Install Credis Library

Again not needed if you are using Magento 1.8.0.0 or higher, as its already included in the base install

wget -O credis.tar.gz https://github.com/colinmollenhour/credis/tarball/master
tar xzf credis.tar.gz
cd colinmollenhour-credis-*/
cp -r ./* /path/to/magento/lib/Credis/
cd /path/to/magento/
chown -R username. lib/Credis/
chown -R username. app/code/local/Cm/
chown -R username. app/etc/modules/Cm_Cache.xml

4) Configure Magento to use Redis

Now comes the fun part, you are going to need to setup redis in your local.xml file.

I recommend always making a backup copy of your local.xml file before making any changes to it

cp app/etc/local.xml app/etc/local.xml-bk-$(date +%F)

Now lets install redis as a backend cache

vim app/etc/local.xml

Now add the following to your configuration for redis using TCP

<cache>
  <backend>Cm_Cache_Backend_Redis</backend>
  <backend_options>
    <server>127.0.0.1</server> 
    <port>6379</port>
    <persistent></persistent> 
    <password></password> 
    <force_standalone>0</force_standalone>  
    <connect_retries>1</connect_retries>    
    <read_timeout>10</read_timeout>         
    <automatic_cleaning_factor>0</automatic_cleaning_factor> 
    <compress_data>1</compress_data>  
    <compress_tags>1</compress_tags>  
    <compress_threshold>20480</compress_threshold>  
    <compression_lib>gzip</compression_lib> 
    <use_lua>0</use_lua> 
  </backend_options>
</cache>

If you are using a Socket for your redis instance

<cache>
  <backend>Cm_Cache_Backend_Redis</backend>
  <backend_options>
    <server>/var/tmp/redis-multi_path/to/my.sock</server> 
    <port></port>
    <persistent></persistent> 
    <password></password> 
    <force_standalone>0</force_standalone>  
    <connect_retries>1</connect_retries>    
    <read_timeout>10</read_timeout>         
    <automatic_cleaning_factor>0</automatic_cleaning_factor> 
    <compress_data>1</compress_data>  
    <compress_tags>1</compress_tags>  
    <compress_threshold>20480</compress_threshold>  
    <compression_lib>gzip</compression_lib> 
    <use_lua>0</use_lua> 
  </backend_options>
</cache>

Clearning your Magento Cache

Now the last step is a giving, any time you make any changes to your Magento configuration/template is to clear your sessions and cache. You do NOT want problems later, because you forgot to clear them and you now find a problem. Sometimes problems show their heads up to a week or so, so clear cache/session and see if you have any problems/issues.

If you were using files prior to the change

rm -r /path/magento/var/session/* /path/magento/var/cache/*

If you were using memcache prior to change

echo "flush_all" | nc 127.0.0.1 11211

or if using a sock

echo "flush_all" | nc -U /path/to/socket

How to flush your Redis Cache

Now you will want to flush your redis cache anytime you make a change to your site. The fastest way to do this is to run the following command via the command line.

echo 'flushall' | redis-cli -h 127.0.0.1 -p 6379

or

echo 'flushall' | redis-cli -s /path/to/sock

How to Check if your Redis Cache is Working

To verify you are getting connections to your redis instance

echo 'info' | redis-cli -h 127.0.0.1 -p 6379 | grep connections

or

echo 'info' | redis-cli -s /path/to/sock | grep connections

Additioanl Articles

--> How to install Redis on a Centos 6.4 Server
--> Magento Connect is not Working
--> Changing the Minimum Search Query - MySQL Full Text Only
--> Install Redis Caching for Magento
--> Install Redis Full Page Caching for Magento Enterprise
--> Basic Redis commands - Cheat Sheet