Memcache in Codeigniter

So Codeigniter 2 has a caching library which supports Memcache.

This example compares calling the database directly and calling the Memcache version with how long it takes to load each.

Why use Memcache: Calling your database server on every request can be very expensive if you receive allot of traffic and provide results slowly.

By caching the database results you will only have to call the database once and future calls to the same query will be cached so delivered to the use allot quicker.

The cache time is controlled by you so you can make the cache last as long as you wish.

More information can be found at:

For those confuse about the memcached extension and the memcache extension, the short story is that both of them are clients of memcached server, and the memcached extension offer more features than the memcache extension.

Full working code can be found at

I will discuss how to get this working on windows as this caused the most problems for me.

Firstly you need to setup memcached so you download win32 binary from:

Unzip and save the folder in the route of your C drive.

Run the .exe provided in that folder to get Memcached installed.

So from now on to start the cache server you do the following:

Start > Run > cmd

net start "memcached Server"

Now you need to check if the PHP extension is active on your version.

Open php.ini

Uncomment extension=php_memcache.dll if it not already done.

Now in a fresh codeigniter install you add the files provided in

(Ignore the system folder if not on windows)

As you can see, I did make a change to one of the core files Cache_memcached.php in system.

This was the only way I could get this working on a windows machine unfortunately.

If you can get it working without this core change then great.

Now setup your MySQL database so a full comparision can be ran between normal MySQL calls and cached calls.

See database.php for user details and folder dbschema for the tables with content.

Now this is setup you should be able to go to your new website and see a quick check that tries to find Memcached.

This code connects to the cache, sets simple data into a key and pulls it out again.

This is not using any of the codeigniter features.

Now if all that is working we can try and use codeigniters caching system.

See memcached.php for each memcached server that you want to have setup. This example is 3 options.

Weight is important so read this answer if you do run more than one instance

Click the continue link to continue to the main comparison page.

Now you will have a full comparison between MySQL and Memcache loading time.

If you view this code you should be able to see how simple it is to implement.

My example doesn’t give it justise as the MySQL calls are small, but as queries get more complicated it will make a huge difference to the load required by the database server.

You can change the cache time to a larger value should you not need realtime results.

So I hope this helps some of you get started with Memcached on the Codeigniter framework.

1 comment for “Memcache in Codeigniter

  1. mallsop
    August 5, 2014 at 2:57 pm


Leave a Reply

Your email address will not be published. Required fields are marked *