Optimize APC Cache is one of easy task what bring a better speed for Magento what should be done on any Magento VPS what support APC. 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.
Optimize APC Cache by Opcode method
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.
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:
Optimize APC Cache by coding cache
When you program you can try to save all of cache with APC cache, it will faster a lot if you save in database or file system. The caching like this is supported very well in Magento, they optimize apc cache in code already, so that you just need to turn it on and then everything will be handled by Magento.
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:
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.
Optimize APC Cache in Development mode
It can be a nightmare with developer if they have to delete cache opcode everytime they change any file, unless it will not update new source files. The reason is because APC cache opcode by default. In development mode we just need to set so it doesn’t cache opcode by default then everything should be fine by adding this line to your .htaccess file.
php_flag apc.cache_by_default Off
And finally, remember to comment out this line in product mode.
For more information how to make your magento site faster you can take a look my other article How to Speed up Magento in Easy steps?