Thursday, 4 October 2012

CodeIgniter + Memcached on CentOS 6.x

*This setup uses PHP 5.4.x from remi repository, on CentOS 6.3*

Do you have trouble to get Memcached working on CodeIgniter?
Well, I do.

Recently, I have been researching and playing around with CodeIgniter.
After having some fun with experimenting page building and DB access,
Data Caching was my next "toy".

But things doesn't goes well.
So I'm writing this down as a memo for myself too.

1. You need to have memcached.php under the application/config/ directory.
* NOTE: CodeIgniter v2.x does not contain the file.
* Refer: http://codeigniter.com/forums/viewthread/180005/#926666
* The config's sample also provided in the thread.

2. You need to get libmemcached and libmemcached-devel from the correct repository.
* I get my PHP 5.4 from remi repository, so I need to get it from remi repository too.
* If you do not get the correct library, you won't be able to continue the next step.

3. Install php memcached module, and add it into php.ini
* Just run #pecl install memcached, after installation just edit php.ini
* In order to build/install, you will need gcc and gcc-c++.

4. Most importantly, install memcached.
* In CentOS 6.x, memcached is normally provided in the default repository.
* If you are referring this setup and installing it on CentOS 5.x, you will need RPMForge repository to get the job done.

5. After everything from above are finished, you are ready to go.
* get the memcached daemon run and restart your apache/httpd server.

6. In CodeIgniter, Use the code below to test if your memcached works
// $this->load->driver('cache', array('adapter' => 'memcached'));
// var_dump($this->cache->memcached->is_supported());
* This should return you boolean(true) and you can use memcached as your data caching.

Have a nice day. :D