7 min read


Magento 1.4 Development Cookbook

Magento 1.4 Development Cookbook Extend your Magento store to the optimum level by developing modules and widgets
  • Develop Modules and Extensions for Magento 1.4 using PHP with ease
  • Socialize your store by writing custom modules and widgets to drive in more customers
  • Achieve a tremendous performance boost by applying powerful techniques such as YSlow, PageSpeed, and Siege
  • Part of Packt’s Cookbook series: Each recipe is a carefully organized sequence of instructions to complete the task as efficiently as possible
        Read more about this book      

(For more resources on Magento, see here.)

The reader can benefit from the previous article on Magento 1.4: Performance Optimization.

Using APC/Memcached as the cache backend

Magento has got a cache system that is based on files by default. We can boost the overall performance by changing the cache handler to a better engine like APC or Memcached. This recipe will help us to set up APC or Memcached as the cache backend.

Getting ready

Installation of APC:

Alternative PHP Cache (APC) is a PECL extension. For any Debian-based Distro, it can be installed with an easy command from the terminal:

sudo apt-get install php5-apc


sudo pecl install APC

You can also install it from the source. The package download location for APC is: http://pecl.php.net/package/APC. Check whether it exists or not in phpinfo(). If you cannot see an APC block there, then you might not have added APC in the php.ini file.

Installation of Memcached:

Memcached is also available in most OS package repositories. You can install it from the command line:

sudo apt-get install php5-memcached

Memcached could be installed from source as well. Check whether it exists or not in phpinfo(). If you cannot see a Memcached block there, then you might not have added Memcached in the php.ini file.

You can also check it via the telnet client. Issue the following command in the terminal:

telnet localhost 11211

We can issue the get command now:

get greeting

Nothing happened? We have to set it first.

set greeting 1 0 11
Hello World
get greeting
Hello World

How to do it…

  1. Okay, we are all set to go for the APC or Memcached. Let’s do it now for APC. Open local.xml in your favorite PHP editor. Add the cache block as follows:




  2. Delete all files from the var/cache/ directory. Reload your Magento and benchmark it now to see the boost in performance. Run the benchmark several times to get an accurate result.
    ab -c 5 -n 100 http://magento.local.com/
  3. You can use either APC or Memcached. Let’s test it with Memcached now. Delete the cache block as we set with APC previously and add the cache block as follows:



    memcached apc / memcached / xcache /
    file database / file (default)
    – used for 2 levels cache setup, necessary for all shared memory
    memcached cache backend related config
    any number of server nodes can be included

  4. Save the local.xml file, clear all cache files from /var/cache/ and reload your Magento in the frontend and check the performance.
  5. Mount var/cache as TMPFS:
    mount tmpfs /path/to/your/magento/var/cache -t tmpfs -o size=64m

How it works…

Alternative PHP Cache (APC) is a free, open source opcode cache framework that optimizes PHP intermediate code and caches data and compiled code from the PHP bytecode compiler in shared memory, which is similar to Memcached. APC is quickly becoming the de facto standard PHP caching mechanism, as it will be included built-in to the core of PHP, starting with PHP 6. The biggest problem with APC is that you can only access the local APC cache.

Memcached’s magic lies in its two-stage hash approach. It behaves as though it were a giant hash table, looking up key = value pairs. Give it a key, and set or get some arbitrary data. When doing a memcached lookup, first the client hashes the key against the whole list of servers. Once it has chosen a server, the client then sends its request, and the server does an internal hash key lookup for the actual item data. Memcached affords us endless possibilities (query caching, content caching, session storage) and great flexibility. It’s an excellent option for increasing performance and scalability on any website without requiring a lot of additional resources.

Changing the var/cache to TMPFS is a very good trick to increase disk I/O. I personally found both APC’s and Memcached’s performance pretty similar. Both are good to go. If you want to split your cache in multiple servers go for the Memcached. Good Luck!

The highlighted sections in code are for the APC and Memcached settings, respectively.



Please enter your comment!
Please enter your name here