Project

General

Profile

Feature #211

Adding Memcache Support to Magento

Added by Daniel Curtis almost 7 years ago. Updated over 5 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Target version:
-
Start date:
10/04/2013
Due date:
% Done:

100%

Estimated time:
0.50 h
Spent time:

Description

With the addition of a load balancer and multiple instances of the same parts2.altservice.com site on different servers, I needed to have session data stored at a central location to be accessed by each web server. Currently I use memcached, which Magento supports through the Zend Framework. So adding support is done by making sure that the php.ini has the following information:

[Session]
session.save_handler = memcached
session.save_path = "tcp://yourMemcachedIP:11211"

This sets PHP to use memcached for session storage.

Next the app/etc/local.xml in the Magento directory must be edited to define memcached storage:

<config>
<global>
    <install>
        <date></date>
    </install>
    <crypt>
        <key></key>
    </crypt>
    <disable_local_modules>false</disable_local_modules>
    <resources>
        <db>
            <table_prefix></table_prefix>
        </db>
        <default_setup>
            <connection>
            </connection>
        </default_setup>
    </resources>
    <session_save><![CDATA[memcache]]></session_save> <!-- db / memcache / empty=files -->
    <session_save_path><![CDATA[tcp://X.X.X.X:11211?persistent=1&weight=2&timeout=10&retry_interval=10]]></session_save_path><!-- e.g. for memcache session save handler tcp://10.0.0.1:11211?persistent=1&weight=2&timeout=10&retry_interval=10 -->
    <session_cache_limiter><![CDATA[private]]></session_cache_limiter><!-- see http://php.net/manual/en/function.session-cache-limiter.php#82174 for possible values -->
    <cache>
        <backend>memcached</backend><!-- apc / memcached / xcache / empty=file -->
        <slow_backend>database</slow_backend> <!-- database / file (default) - used for 2 levels cache setup, necessary for all shared memory storages -->
        <slow_backend_store_data></slow_backend_store_data> <!-- 1 / 0 (default) - used for 2 levels cache setup, sets whether store data in db slow cache backend -->
        <auto_refresh_fast_cache>1</auto_refresh_fast_cache> <!-- 1 / 0 (default) - used for 2 levels cache setup, sets whether refresh data in fast cache backend -->
        <memcached><!-- memcached cache backend related config -->
            <servers><!-- any number of server nodes can be included -->
                <server>
                    <host><![CDATA[X.X.X.X]]></host>
                    <port><![CDATA[11211]]></port>
                    <persistent><![CDATA[1]]></persistent>
                </server>
                <server>
                    <host><![CDATA[X.X.X.X]]></host>
                    <port><![CDATA[11211]]></port>
                    <persistent><![CDATA[1]]></persistent>
                </server>
            </servers>
        </memcached>
    </cache>
</global>
<admin>
    <routers>
        <adminhtml>
            <args>
                <frontName><![CDATA[admin]]></frontName>
            </args>
        </adminhtml>
    </routers>
</admin>

And finally make sure to have the internal Cache Management turned off

History

#1 Updated by Daniel Curtis almost 7 years ago

  • Description updated (diff)

#2 Updated by Daniel Curtis almost 7 years ago

Memcached

The above configuration is intended for a memcache server. The configuration for a memcached server with support for session management looks more like this:

<config>
    <global>
...
<!-- Optimize sessions for Memcached use -->
        <session_save><![CDATA[memcached]]></session_save>
        <session_save_path><![CDATA[mem.example.com:11211?persistent=0]]></session_save_path>
        <session_cache_limiter><![CDATA[private]]></session_cache_limiter>

<!-- Optimize cache for Memcached use -->
        <cache>
            <backend>memcached</backend>
            <memcached>
                <servers>
                    <server>
                        <host><![CDATA[mem.example.com]]></host>
                        <port><![CDATA[11211]]></port>
                        <persistent><![CDATA[0]]></persistent>
                        <weight><![CDATA[2]]></weight>
                        <timeout><![CDATA[10]]></timeout>
                        <retry_interval><![CDATA[10]]></retry_interval>
                        <status><![CDATA[]]></status>
                    </server>
                </servers>
                <compression><![CDATA[0]]></compression>
                <cache_dir><![CDATA[]]></cache_dir>
                <hashed_directory_level><![CDATA[]]></hashed_directory_level>
                <hashed_directory_umask><![CDATA[]]></hashed_directory_umask>
                <file_name_prefix><![CDATA[]]></file_name_prefix>
            </memcached>   
        </cache>
...
    </global>
</config>

I had issues getting this to work by having the configuration parameter persistent as 1. To fix it I disabled persistent by switching the parameter to 0.

#3 Updated by Daniel Curtis over 5 years ago

  • Project changed from 6 to Website Hosting

Also available in: Atom PDF