Optimizing APC Cache Settings for Magento

Optimizing APC Cache Settings for Magento

Mar 25

  • Created: Mar 25, 2011 12:00 PM

Optimizing APC Cache Settings for Magento
APC (Alternative PHP Cache) is an opcode cache for PHP that works very well to speed up page loads when used on servers running Magento. There are many opcode caches available (APC, accelerator, xcache, etc) which can be used with Magento, but we will be focusing on APC here as it along with eAccelerator are recommended by Magento in their performance whitepapers. eAccelerator also works very well with Magento, but please note that you must use version 0.9.5 or older.

APC can be used for two purposes within Magento. The first is as an opcode cache that PHP will utilize to cache any PHP files that are compiled through it. APC can also be utilized by Magento as an fast backend cache for Magento data. This can be configured within the Magento local.xml.

Using the default settings with APC in it’s apc.ini will work with Magento out of the box, but the following settings are the most important and should be modified for Magento:


The apc.shm_size is the maximum amount of space in MB that APC is allowed to use to cache data. This needs to be set high enough so that all data that is being cached can be cached without purging data. The default of 32MB is much too small, a setting of 256MB is recommended.

apc.num_files_hint=10000<br />

Both apc.num_files_hint and apc.user_entries_hint are used by APC to help optimize its memory usage when creating the cached memory space. The apc.ini defaults are both set very low at 1000 and 4096 respectably. Based on the amount and size of files within Magento, it is recommended to set these both high at a value of 10000.


This sets the maximum file size that can be cached by APC. The dafault size is 1MB, Magento recommends this be increased to 5MB.


The apc.stat variable is an important one. It decides if APC should check each file that has been cached locally on disk to see if it has been recently modified. With apc.stat enabled, if the file has been modified it will recompile the PHP code and cache the new version. The benefit to this is if you are developing a site, any time you make changes to a PHP file you will not have to manually flush the APC cache to see the changes live. The downside is it creates a slight performance hit as each file must have an additional stat() call to check for the files modified time.

The Magento whitepaper states that this should always be set to 0, but this is not always the case. If you are developing Magento or making frequent code changes, setting apc.stat to 1 will make things much easier for you.

For best performance on a production server, set:


For development purposes and overall ease of use, set:


APC does have a useful tool to see how the APC cache is performing, it is a PHP file included with the APC distribution called apc.php. If you have installed APC via pecl, you will find it on your server in: /usr/share/doc/php-pecl-apc-x.y.z/apc.php

One note about security. We strongly recommend you first edit this file and set the username and password inside it before placing it within your site for viewing. We also recommend changing the name of the file to something other than apc.php and additionally .htpasswd password securing it’s location so it cannot be viewed by anyone without a userid and password.

Once apc.php is secured, viewing it in a browser will show you something similar to the following:

Optimizing APC Cache Settings for Magento

With this data you can see some useful information such as cache hit rates, cache memory used vs free, and fragmentation stats. It will also allow you to view the cached data. You can see a list of all files that have been cached along with their size, creation time, access time, modified time, and number of hits to each file. This information can show you how much cache space is being used allowing you to make further memory settings if necessary. The apc.php page also gives you an additional option when logged in to clear the opcode cache. This can be used when you have apc.stat set to zero and are making changes to files requiring the opcode cache to be flushed.

  • piotr

    Thanks for tutorial.
    One question : in Your case You didnt modified Magento XML and You dont cache user entries in user cache. Would it be better to have turn magento support for APC?

  • Sean

    If you’re running LiteSpeed and APC cache, DON’T ENABLE APC in LOCAL.XML

    It leads to massive fragmentation and user entries never seem to get hits if they successfully cache up. Also, the cache resets every 1-2 hours and dumps everything.

    The File Cache works great at caching up all the Magento files it can get its hands on without messing with local.xml

  • Sean


    on newer installs of APC end up with an annoying This entry now uses M or G type message, so should be:


  • http://www.magikthemes.com Ashish @ Magik Themes

    APC is awesome if you can configure it well. Make sure that fragmentation stays under 50% at all times and check user level values which will be visible after logging in.

  • big boss

    I read a article under the same title some time ago, but this articles quality is much, much better. How you do this.. bank rate cd

  • http://giamsatgps.vn/ dinh vi

    Dear all,
    How can I improve APC setting for my server with 1GB of RAM?
    Thanks alot!

  • http://twitter.com/iperich iperich

    I don’t know what Magento is, but thanks for the info, very clear!!

  • me

    You can also use apc_clear_cache(); in PHP to clear the cache.