Magento supports many backend cache with the help of the Zend framework. Memcache and APC are commonly used. However, Redis is rapidly becoming a popular and powerful cache system for Magento and other web applications.
Redis is an open source, BSD licensed, advanced key-value store that can optionally be used in Magento for back end and session storage. It can be used to cache the database resulting in exploitation of less database resources, and provides a tunable persistent cache. It is a good alternative to memcacheD.
When first time page is loaded, a database is queried on the server. Redis caches the query. Next time other user loads the page the results are provided from the redis without quering the actual database. It implements a persistent object cache (no expiration). An object cache works by caching the SQL queries in memory which are needed to load the web page. When the data of main database server is updated,then corresponding key in the redis is invalidated. So the it provides the updated data instead of caching the data. If a query is not available in redis, the database provides the result and it adds the result to its cache.
So, let’s begin with its installing and configure it for Magento.
Update and install redis-server.
1 2 3 4 5 |
sudo su apt-get update apt-get install redis-server |
Configure Magento,
1 |
nano /<magento-root-directory>/app/etc/modules/Cm_RedisSession.xml |
To enable the module, edit
1 2 3 4 5 6 7 8 9 10 |
<config> <modules> <Cm_RedisSession> <active>false</active> <codePool>community</codePool> </Cm_RedisSession> </modules> </config> |
To
1 2 3 4 5 6 7 8 9 10 |
<config> <modules> <Cm_RedisSession> <active>true</active> <codePool>community</codePool> </Cm_RedisSession> </modules> </config> |
Then, edit the ‘local.xml’ file located in magento directory.
1 |
nano /magento-root-dirrectory/app/etc/local.xml |
Change
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?xml version="1.0"?> <config> <global> <install> <date><![CDATA[Tue, 04 Oct 2016 09:53:37 +0000]]></date> </install> <crypt> <key><![CDATA[e972f3c4e8436052de805bdf1f40de0f]]></key> </crypt> <disable_local_modules>false</disable_local_modules> <resources> <db> <table_prefix><![CDATA[]]></table_prefix> </db> <default_setup> <connection> <host><![CDATA[localhost]]></host> <username><![CDATA[magento]]></username> <password><![CDATA[magento]]></password> <dbname><![CDATA[magento]]></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> |
To
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
<?xml version="1.0"?> <config> <global> <install> <date><![CDATA[Tue, 04 Oct 2016 09:53:37 +0000]]></date> </install> <crypt> <key><![CDATA[e972f3c4e8436052de805bdf1f40de0f]]></key> </crypt> <disable_local_modules>false</disable_local_modules> <resources> <db> <table_prefix><![CDATA[]]></table_prefix> </db> <default_setup> <connection> <host><![CDATA[localhost]]></host> <username><![CDATA[magento]]></username> <password><![CDATA[magento]]></password> <dbname><![CDATA[magento]]></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> <!-- This is a child node of config/global --> <cache> <backend>Cm_Cache_Backend_Redis</backend> <backend_options> <server>127.0.0.1</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 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><![CDATA[files]]></session_save--> <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 server requires authentication --> <timeout>2.5</timeout> <!-- This is the 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, lz4 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 --> <fail_after>10</fail_after> <!-- seconds after which we bail from attempting to obtain lock (in addition to break after time) --> <break_after_adminhtml>30</break_after_adminhtml> <first_lifetime>600</first_lifetime> <!-- Lifetime of session for non-bots on the first write. 0 to disable --> <bot_first_lifetime>60</bot_first_lifetime> <!-- Lifetime of session for bots on the first write. 0 to disable --> <bot_lifetime>7200</bot_lifetime> <!-- Lifetime of session for bots on subsequent writes. 0 to disable --> <disable_locking>0</disable_locking> <!-- Disable session locking entirely. --> <min_lifetime>60</min_lifetime> <!-- Set the minimum session lifetime --> <max_lifetime>2592000</max_lifetime> <!-- Set the maximum session lifetime --> </redis_session> </global> <admin> <routers> <adminhtml> <args> <frontName><![CDATA[admin]]></frontName> </args> </adminhtml> </routers> </admin> </config> |
Clear all the cache and sessions:
1 2 3 |
rm -rf /var/www/html/magento/var/session/* rm -rf /var/www/html/magento/var/cache/* |
Or, log in to the Admin panel as an administrator,
System > Cache Management,
and, click on ‘Flush Magento Cache’.
Now, restart as,
1 |
service redis-server restart |
To check if Redis-server is working or not, Enter
1 2 |
redis-cli ping |
If result is,
1 |
PONG |
Then your server is responding.
Also you can monitor all the traffic by,
1 |
redis-cli monitor |
then refresh the page. If you can see logs generating on your terminal, having random alphanumeric characters, then it means caching is working.
You can also check if Redis-server is able to set keys or not,
1 2 3 4 5 6 7 8 |
redis-cli 127.0.0.1:6379> set mykey KEY OK 127.0.0.1:6379> get mykey "KEY" 127.0.0.1:6379> exit |
You can also use ‘info’ command to get information and statistics about the server as,
1 |
redis-cli info |
Is this possible to clear cache for specific product only? For example, I have thousands of product and If I update product which is having id 5,at that time I need to clear cache of product which is having id 5 only.
Is this possible?
Thank You.