Using in-memory caching for your web app can dramatically increase the responsiveness of your web app (Apdex) — especially if you make a lot of database calls or complicated joins. Getting caching setup for your web app but also for your development environment can be, well, a pain in the ass. So here’s my step-by-step guide to implement memcached (not memcache, as ElastiCache uses memcached) in CodeIgniter that runs during development with MAMP 2.0 and in production in AWS Elastic Beanstalk with ElastiCache. Phew. Let’s get started.
Install Memcached for MAMP 2.0
- Open Terminal and install libevent, autoconf, and libmemcached. I use homebrew.
brew install libevent brew install autoconf brew install libmemcached
.
- Find out what version of PHP you are using in MAMP. I’m using 5.4.10 currently. Go to the download page on php.net or for me, I need to go to the previous releases page.
- Unarchive the source in the /Applications/MAMP/bin/php/php5.4.10/include/php folder
- Make sure you
cd /Applications/MAMP/bin/php/php5.4.10/include/php
- Configure the source with
/Applications/MAMP/bin/php/php5.4.10/include/php/configure
- Change directories with
cd /Applications/MAMP/bin/php/php5.4.10/bin
- Then compile with
./pecl install memcached
- Change directories with
cd ../
- Add the memcached.so extension to your php.ini file with
echo -e "\n[memcached]\nextension=memcached.so" >> conf/php.ini
- Restart the MAMP servers and go to the Start page and click on phpinfo. ⌘+F to find ‘memcached’ to verify everything went well.
- Start the memcached service with
memcached -m 24 -p 11211 -d
Sweet. On to AWS.
Start a Cache Cluster in ElastiCache
Using the AWS Management Console, it’s pretty straight-forward setting up an ElastiCache cluster running Memcached.
- Login to AWS Console.
- Go to the ElastiCache console.
- Click Launch Cluster
- Give your cluster a name, select the size of your node and the number. If you’d like to be notified of updates you can select an existing SNS topic.
- Click Next and use the default security and group settings and Finish.
- The cluster will take a few minutes to spin up. Once it does, click on the link for the Configuration Endpoint. This will bring up a lightbox with the Node Endpoints. This URL will be what we use in our config file in CodeIgniter to identify our Memcached servers.
Load the Memcached Driver in CodeIgniter
Thanks to Codeigniter having a caching driver built-in to their framework, using memcached is simple.
- Add the file memcached.php to your application/config/ folder with the following content:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
$config = array( 'default' => array( 'host' => 'localhost', 'port' => 11211, 'weight' => 1 ) ); - Include the memcached driver in your controller and test the save() & get() functions with
$this->load->driver('cache'); if($this->cache->memcached->is_supported()){ $data = $this->cache->memcached->get('foo'); if (!$data){ echo 'cache miss!<br />'; $data = 'bar'; $this->cache->memcached->save('foo',$data, 60); } echo $data; echo '<pre>'; var_dump($this->cache->memcached->cache_info()); echo '</pre>'; }
- This should output:
cache miss!
bar
and then a bunch of cache info. - Reload the page. You shouldn’t see ‘cache miss!‘ anymore for the next 60 seconds.