In the last blog ( Magento Optimization - Part 1 ) we discussed about the 2-level caching mechanism used in Magento. Here, we will talk about how to add APC and Memcached as a caching mechanism in detail. Other than APC and Memcached, we can also use XCache and eAccelerator as a fast backend. But we will not go much deeper for those two, as APC and Memcached are the most commonly used caching mechanism.
Memcached is a high-performance memory object caching system that is designed to speed page loads on dynamic content websites. Magento supports Memcached for caching many objects, but it is not enabled by default and will require some simple changes to your configured local.xml file to use.
To enable Memcached you will need to check if the daemon is running. By default the daemon is configured to listen to port 11211 so you can perform a netstat command and see if there is activity on 11211. If it’s running, you need to check if Memcached PHP support is enabled by looking at the output of phpinfo().
If the Memcached is not installed, then you can install it on a Debian type distro with the below command :
sudo apt-get install memcached php5-memcache
You’ll need to enable Memcached via its configuration file (there will be a notice about this when you run the above command). Then start the service and restart Apache. After this, you can add the following <cache>section to your app/etc/local.xml:
These settings are for a single local server running Memcached on the default 11211 port. To see your Memcached in action you can issue a telnet localhost 11211 command and type: stats
Using APC is really easy. You can just install the PHP module and restart your Apache server and your sites should immediately benefit from this.
To install APC on a Debian based Linux distro, run:
sudo apt-get install php5-apc
Then just restart your Apache server. You should start seeing improvements after browsing to a few pages on your site.
This is great already, however, there is another step you can do to integrate APC with your Magento site.
If you look at: app/etc/local.xml.additional, you will notice that there is a <cache>element that allows you to specify which caching method you are using in the <backend>element.
We are dealing with APC here so we’ll just add:
You will notice the <prefix>element containing MAGE_. This is simply a prefix to tell APC what to use when it creates the cache. This is particularly important if you are running several Magento sites on the same web server. In this case, you will need to enter a unique prefix for each site.
Finally, to put the polish on your speed improvements, you can also tweak the APC configuration to further optimize your cache. This is done by editing the apc.ini file usually located (on Debian based distros) in: /etc/php5/conf.d/apc.ini.
Below are the recommended configuration for APC:
extension=apc.so #name dependent on your APC cache install
apc.enabled=1 ;Turn APC cache on
apc.optimization=0 ;Experimental keep off
apc.shm_segments=1 ;Shared memory segments
apc.shm_size=128 ;Max shared memory dependent on OS
apc.enable_cli=1 ;Allow command line php to function
apc.cache_by_default=1 ;Enabled, 0 for filters
apc.max_file_size=10M ;Maximum cached file size
apc.stat=1 # 1 for dev, 0 for production, whether the source file is checked for mod date
;apc.include_once_override=1 ;Use PHP5.3+ for include_once optimization
In the next blog, we will look into other methods which can be used to speed up Magento.