Geeky Nuggets

Installing and running APC cache with CodeIgniter

February 13, 2011 | 3 Minutes Read

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

sudo apt-get install php-apc
on the command line. Of course, you will have to install Apache and php before that, but that is really easy:
sudo apt-get install php5
will install PHP and all needed dependencies, including apache So now that APC is installed, just restart apache
sudo /etc/init.d/apache2 restart
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:
$this->output->cache(n);
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

APC: 43, File: 25, None: 18

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

APC: 46.37 rps, File: 27.23 rps, None: 18.99 rps

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?

Older posts