Login Contact Us
My cart

Magento Cache Configuration in local.xml

Feb 20, 2014
by Mexbs team
Magento Tutorials

In this article we will explain Magento cache configuration options through the local.xml. But first we need to explain briefly about Magento cache backend and cache frontend.

What is Magento cache frontend?

Magento cache frontend is the actual class that Magento uses in order to use the cache. This class is always Varien_Cache_Core. That is, whenever Magento will need to save a cache, it will call Varien_Cache_Core::save and so on.

What is Magento cache backend?

Magento cache backend is a class that is used by the Magento cache frontend to communicate with the caching compontent. It can be seen as a driver. Possible Magento cache backends are: Zend_Cache_Backend_File, Zend_Cache_Backend_Apc, Zend_Cache_Backend_Xcache, Zend_Cache_Backend_Sqlite, Zend_Cache_Backend_TwoLevels, Varien_Cache_Backend_Database, Varien_Cache_Backend_Eaccelerator, Varien_Cache_Backend_Memcached. You can choose only one of them to use, dependent on which cache you prefer to use. For instnace, if you want to use the database as a cache, than you'd use Varien_Cache_Backend_Database as cache backend. One of the cache backends mentioned above is Zend_Cache_Backend_TwoLevels. This is a special cache backend. It allows a usage of two cache backends simultaneously. Lets take a closer look on it.

Magento Two-Levels Cache Backend

It is possible to use two-levels cache in Magento. As we said, the two-levels cache is a Magento cache backend which uses 2 another cache backends. While one cache will be used as a first level cache and the second will be used as a second level cache. Every time that Magento will need to save data into cache, it will save it both in the first level cache and in the second level cache. Later on, whenever Magento will need to fetch something from the cache, it will first look in the first level cache, if it didn't find it, it will try to get it from the second level cache. If it was found in the second level cache, it will save it again in the first level cache. Naturally, the first level cache should be faster then the second level cache. Thus, in Magento the first level cache is called the "fast backend" and the second level cache - the "slow backend". Magento allows only those 4 caches to be used as fast backend - Apc, memcache, xcache and eaccelerator. Those 4 caches cannot act as slow backends and they cannot be used standalone (ie - not in two-level cache installation). Every cache can act as slow cache. The common two-level cache setups are Apc-filesystem, memcached-filesystem, Apc-database and memcached-database. Now we will go through the common cache setups: filesystem, apc&filesystem, memcached&filesystem, database, apc&database, memcached&database. Later on, we will show a trick that will allow us to use memcached standalone.

Variation #1 - Filesystem

Whenever there are no cache configurations in the local.xml, Magento uses Zend_Cache_Backend_File as a cache backend. That is, the filesystem.This is the way local.xml comes within the clean Magento installation. That is, in order to use filesystem as a cache, just don't put any cache configurations inside the local.xml.

Variation #2 - APC & Filesystem

In order to configure Magento to use APC as fast backend and Filesystem as slow backend, we need to add the following in the local.xml

<config>
...
    <global>
    ...

        <cache>apc</cache>

    ...
    </global>
...
</config>

Pay an attention that we didn't specify the filesystem as a slow backend. This is because Magento sees that we want to use Apc as cache, and as we told before, Apc can only be used as fast backend inside two-levels cache. Thus, Magento knows that it should use two-levels cache where Apc is the fast backend. Then Magento needs to choose the slow backend. Since we didn't specify any, Magento choses the default slow backend which is the filesystem (Zend_Cache_Backend_File).

Variation #3 - memcache & Filesystem

Similarly to Apc, we only need to specify the memcahce and Magento will automatically use the two-level cache backend, where memcache will be used as the fast backend and filesystem as slow backend. Pay an attention that when using memcached, Magento accepts the xml tag <memcached> to define the memcached configurations (although, it is possible to use <backend_options> instead of <memcached>).

<config>
...
    <global>
    ...

        <cache>memcached</cache>
            <memcached>
                <servers>
                    <server>
                        <host><![CDATA[127.0.0.1]]></host>
                        <port><![CDATA[11211]]></port>
                    </server>
                </servers>
            </memcached>
    ...
    </global>
...
</config>

Memcached speed improvement for single web server installation

If you are running single web server for your Magento website, and the memcached is running on the same server as Magento, it is highly encouraged to use the unix socket instead the TCP communication. The unix socket uses the memcopy, which is faster than the communication over TCP. In order to use socket, you should configure the memcached to use a socket, by commenting out the lines with -p and -l inside the memcached configuration file (in debian: /etc/memcached.conf), and adding the following lines:

-s /var/run/memcached/memcached.sock
-a 0777

If there is no directory /var/run/memcached, create it and run:

chown -R nobody /var/run/memcached
chmod -R 777 /var/run/memcached

Afterwards, restart the memcached (in debian: /etc/init.d/memcached restart). Finally, your local.xml should look like this:

<config>
...
    <global>
    ...
        <cache>
    <backend>memcached</backend>
    <memcached>
        <servers>
            <server>
                <host>unix:///var/run/memcached/memcached.sock</host>
                <port>0</port>
                <persistent>0</persistent>
            </server>
        </servers>
    </memcached>
</cache>
    ...
    </global>
...
</config>

Variation #4 - Apc& Database

<config>
...
    <global>
    ...
        <cache>
        <backend>Apc</backend>
        <slow_backend>database</slow_backend>
        <slow_backend_store_data>1</slow_backend_store_data>
        </cache>
    ...
    </global>
...
</config>

You can see that there is a tag <slow_backend_store_data>1</slow_backend_store_data> it says to Magento that the cahe should be stored at the database, if this setting is set to 0 or not set at all - only the cache tags will be saved in the database. If you have big memory for Apc, it makes sense to set slow_backend_store_data to 0, since you would probably not need the slow cache. Additionally, setting it to 0 will save some space in your database. But if you have limited RAM, but a lot of disk storage space, it is a good idea to set this to 1. This way, the Apc will handle small amount of most frequently accessed cache, and the database will handle the rest.

Variation #5 - memcached& Database

<config>
...
    <global>
    ...
        <cache>memcached</cache>
        <slow_backend>database</slow_backend>
    <slow_backend_store_data>1</slow_backend_store_data>
            <memcached>
                <servers>
                    <server>
                        <host><![CDATA[127.0.0.1]]></host>
                        <port><![CDATA[11211]]></port>
                    </server>
                </servers>
            </memcached>
    ...
    </global>
...
</config>