Installing and running APC cache with CodeIgniter
Codeigniter 2 now includes an excellent cache driver, which makes it dead easy to use any one of the great available cache librairies such as APC o memcached. In this post I will focus on APC, it’s installation on an Ubuntu 10.04 server, and a few benchmarks to compare the various cache systems. For my testing, I will use an Ubuntu Server installation, on a VirtualBox virtual machine with 256 Mb of RAM and a single processor. This enables me to test things until everything breaks, than just scrap that VM and start with a clean one. However, a description of this setup is a story for another day…
Installation
On Ubuntu, and on other debian based linuxes, you can install APC by typing
on the command line. Of course, you will have to install Apache and php before that, but that is really easy:
will install PHP and all needed dependencies, including apache So now that APC is installed, just restart apache
and you’re ready to go. In codeigniter, you don’t need to do anything special. Just turn the cache on by putting the following line somewhere in your controller:
Where ‘n’ is the number of minutes to cache that particular content. The cache library is clever enough to pick the best cache driver from the four available ones (memcached, APC, file and dummy). Forget about the dummy driver: it doesn’t do anything
Benchmarks
These are the results for 50 concurrent connections : For APC we get about 43 requests/s, with the file caching mecanism around 25, requests/s, and with no cache at all we are below 18 requests/s
For low concurency (2) the improvement when using APC is similar: in this case the number of requests per second nearly doubled, from 19 to 47 between the case without any caching system and the APC case
So just turning the default codeigniter cache on gives us a significant speed improvement, going all the way and installing APC can nearly double the speed of your application. Not too shabby I should think.
It is so easy to take advantage of this caching mechasism on Codeigniter 2 that it would really be a pity not to do so!
The only caching library missing here is memcached. Have you tried any of these caching libraries with codeigniter 2?