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
Skill Level:
|
<td>
Intermediate
</td>
Time Required:
|
<td>
40 mins
</td>
Requirements
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.
Join the discussion
comments powered by Disqus