aws-elasticache

CodeIgniter + Memcached = Happy Appy

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

  1. Open Terminal and install libevent, autoconf, and libmemcached. I use homebrew.
    brew install libevent
    brew install autoconf
    brew install libmemcached

    .

  2. 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.
  3. Unarchive the source in the /Applications/MAMP/bin/php/php5.4.10/include/php folder
  4. Make sure you
    cd /Applications/MAMP/bin/php/php5.4.10/include/php
  5. Configure the source with
    /Applications/MAMP/bin/php/php5.4.10/include/php/configure
  6. Change directories with
    cd /Applications/MAMP/bin/php/php5.4.10/bin
  7. Then compile with
    ./pecl install memcached
  8. Change directories with
    cd ../
  9. Add the memcached.so extension to your php.ini file with
    echo -e "\n[memcached]\nextension=memcached.so" >> conf/php.ini
  10. Restart the MAMP servers and go to the Start page and click on phpinfo. +F to find ‘memcached’ to verify everything went well.
  11. 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.

  1. Login to AWS Console.
  2. Go to the ElastiCache console.
  3. Click Launch Cluster
  4. 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.
  5. Click Next and use the default security and group settings and Finish.
  6. 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.

  1. 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 ) );
  2. 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>';
    }
  3. This should output:
    cache miss!
    bar

    and then a bunch of cache info.
  4. Reload the page. You shouldn’t see ‘cache miss!‘ anymore for the next 60 seconds.
  • Matias

    Hi, Thanks for this great article. Quick question, I created my ElastiCache instance but can’t seem to connect to it from the outside. I can only connect to it via an EC2 instance.

    (see this http://aws.amazon.com/elasticache/faqs/#How_ElastiCache_Node_VPC)

    Is there any way to have an application hosted outside of AWS but yet use an ElastiCache node?

    Thanks again.

    • Graves

      Hi Matias,

      You will need to modify the Clusters Security group to allow port access from external connections on the port you decided to use for memcached

      • davelassanske

        Thanks for the reply Graves!