Beginning cloud development with cloudControl – Part 4 – Memcache

Here we are at part four of the Beginning Cloud Development with cloudControl series and in this part, we’re adding Memcached support. In part one of the series, we laid the foundation for the application and got up to speed with what cloudControl is, why it works and how to get started using it.

Then in part two, we started to flesh out the application that we started building in part one and added MySQL support to it. We showed how to work with cloudControl to manage the key requirements, such as enabling MySQL support, connecting to the MySQL database and keeping the database up to date from a maintenance perspective (creating the database schema and loading data in to the database).

Then in the third part of the series, we replaced MySQL with mongoDB support. Now, in this, the third part of the series, we’re going to finish up with adding Memcached support. As the core of the work’s already been done in the application, this tutorial will be shorter and simpler than the previous three. So, get out the code that you worked through from part 2, or download a copy of it from the github repository and let’s get started.


Here we are at part four of the Beginning Cloud Development with cloudControl series and in this part, we’re adding Memcached support. In part one of the series, we laid the foundation for the application and got up to speed with what cloudControl is, why it works and how to get started using it.

Then in part two, we started to flesh out the application that we started building in part one and added MySQL support to it. We showed how to work with cloudControl to manage the key requirements, such as enabling MySQL support, connecting to the MySQL database and keeping the database up to date from a maintenance perspective (creating the database schema and loading data in to the database).

Then in the third part of the series, we replaced MySQL with mongoDB support. Now, in this, the third part of the series, we’re going to finish up with adding Memcached support. As the core of the work’s already been done in the application, this tutorial will be shorter and simpler than the previous three. So, get out the code that you worked through from part 2, or download a copy of it from the github repository and let’s get started.

Now this part of the series isn’t as detailed as the previous three parts as, effectively, it’s a supplement to parts one and two. Our objective here is to integrate Memcached support and speed up the MySQL-based application that we setup in part two.

Details

<td>
  Intermediate
</td>
<td>
  40 mins
</td>
Skill Level:
Time Required:

Requirements

  • Memcache Library

Step 1 - Add Memcache Support

Just like all the previous add-ons, we’re using the command-line cctrlapp utility to add support for Memcached. And, also like the previous add-ons that we’ve used, we’re going to use the free version. Please feel free to use a paid one if you’d like – but for the purposes of this tutorial, that won’t be necessary.

Run the following command, and we’ll have Memcached support added to our development branch and deployment:

[bash] cctrlapp maltbluedev/development addon.add memcached.free [/bash]

To confirm that it’s been added run the following command and ensure that Memcached is in the output:

[bash] cctrlapp maltbluedev/development addon [/bash]

Step 2 – Set the Memcache application configuration

Assuming that Memcached was in the previous output, then we need to take the configuration from the previous command and add it to the application configuration in our application.ini. So, open config/application.ini and add in the following options to the development section, adjusted based on the output you have.

[bash] memcache.params.hostname = 127.0.0.1 memcache.params.username = A3TLHUx5Sr9XzQAaudkQkAS memcache.params.password = A3TLHUx5Sr9XzQAaudkQkAS memcache.params.bucket = A3TLHUx5Sr9XzQAaudkQkAS memcache.params.bucketsize = 5 [/bash]

Step 3 – Adjust the code to use memcache for the queries

Now that we’ve done this, we need to add in the Memcached initialisation code in config.php so that we have a Memcached object available for interaction with the caching server. One point of note, the cloudControl servers require two things that you normally may not need or have encountered previously; user credentials and use of the binary protocol.

You can find further instructions on the cloudControl website, but I’ll save you the trouble and list them in the code below. Please notice that it uses the setOption and setSaslData functions that take care of these points.

[cc lang=“php”] $m = new Memcached(); $m->setOption(Memcached::OPT_BINARY_PROTOCOL, 1); $m->setSaslData( $config->memcache->params->username, $config->memcache->params->password ); $m->addServer($config->memcache->params->hostname, 11211); [/cc]

You can see that we’ve made reference to the Zend Config object with the new settings to pass to the functions as required. With this done, we now have all that we need to start saving on MySQL processing cycles and to start caching retrieved results.

Step 4 – Adjusting the List.php code

If you’ve written non-trivial applications, then the following will be more than familiar to you.

[php] if ($m) { if (($result = $m->get($cacheKey)) === FALSE) { $result = $db->fetchAll($sqlFindAll); echo “fetched result from database
”; $m->set($cacheKey, $result); } else { if (is_null($result)) { $result = $db->fetchAll($sqlFindAll); echo “fetched result from database
”; $m->set($cacheKey, $result); } echo “fetched result from cache
”; } } else { $result = $db->fetchAll($sqlFindAll); echo “fetched result from database
”; } [/php]

First we check that we have a valid cache object. Then, if we do, we attempt to retrieve the database results for a list of all users from the cache, based on the $cacheKey variable. If that’s equivalent to FALSE, then the data was not available, so we query it and then store it in Memcached.

However, if it is available, then we simply continue on. If however, we don’t have a valid Memcached object, then we proceed as before and query the database and return the results. So, make the changes to the code and get ready to deploy them.

Step 5 - Push and Deploy the Changes

Like all deploys previously, commit the changes to the git repository, then run a push and deploy to make the changes live. After likely no more than half a minute, the changes will be ready to display and the execution speed will be significantly improved.

Winding Up

Well, this is the end of the series and it’s been quite a lot of fun writing it, getting to know Henning and some of the team at cloudControl and getting more familiar with the cloudControl product which, as I’m unashamed to admit, I’m very impressed with.

I hope that you’ve actively participated throughout the series and seen just how flexible and full featured the service is. If you have any questions about the product, please, don’t hesitate to either drop a comment or get in touch with the team there at cloudControl. They’ve been no end of help as I’ve needed it through the course of the development of this series.

So, what did you think? Let me know in the comments or send a tweet.


You might also be interested in these tutorials too...

Fri, Dec 16, 2011

Beginning cloud development with cloudControl - Part 2 - MySQL

In part one of the series we got a birds eye view of a great cloud development solution for PHP - cloudControl. We looked at the concept of what it is, what you can do with it and ran through a basic deployment with a rather basic application. If you missed the first part, I strongly encourage you to read it before continuing on with part two. When you’re done, come on back and work through it here.

In this, part two of the series, things start to get more serious. In this part, we’re going to start to flesh out the application started in part one, adding in MySQL support - showing just how simple cloudControl makes this for us.

Mon, Jan 2, 2012

Beginning cloud development with cloudControl – Part 3 – MongoDB

Here we are at part three of the beginning cloud development with cloudControl series and in this part, we’re adding mongoDB support. In part one of the series, we laid the foundation for the application and got up to speed with what cloudControl is, why it works and how to get started using it.

Then in part two, we started to flesh out the application that we started building in part one and added MySQL support to it. We showed how to work with cloudControl to manage the key requirements, such as enabling MySQL support, connecting to the MySQL database and keeping the database up to date from a maintenance perspective (creating the database schema and loading data in to the database).

In this, the third part of the series, we’re replacing MySQL that we introduced in part two with mongoDB support.

Thu, Dec 8, 2011

Beginning cloud development with cloudControl

##.

Don’t forget to grab the other parts as well:


So you’ve heard all about the cloud. In the current day and age, you’d have to be living under a rock to have not encountered it by now. Whether it’s the ads from Amazon, Rackspace or Microsoft, the extensions to your favourite framework, such as SimpleCloud in Zend Framework or iCloud from Apple – The Cloud is Everywhere!

But these aren’t the only options available to us as developers. In this series I want to introduce you to and help you get started with another cloud service - cloudControl.

Fri, Apr 27, 2012

Zend Form Mastery with Zend Config - Part 1 Custom Filter Paths

When you’re working with Zend Form you keep your configuration as much out of code as you can - right? Well, if you’ve been working withZend Form for more than a little while, you know that Zend Config really makes that pretty simple - well, some of the time. In this series we look, comprehensively at how to do it all with Zend_Config.


Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.

Join the discussion

comments powered by Disqus