DreamingWell Logo

Creating a scalable Flex Application with PHP, MemCached and AMF

Posted by Travis Collins on February 11, 2009

MemCached is a free "distributed memory object caching system" that can work wonders on your application's responsiveness and server load. If your application does not need "instantaneous" data calculations for every user, you can use MemCached to greatly reduce your server's processing load, thereby increasing the users/server ratio. This article explains the basic concepts that apply to any server language, and includes some sample Flex (AMF) and PHP code.


Basic Concepts

All client to server communication requires the server to perform a series of operations and return a result. For many communications, the server is performing the exact same series of operations, and returning the exact same result for every user. By caching (storing) the calculated results, and serving that cache back to the client, we can remove all of the repetitive processing out of the communications cycle.

So, applying this concept to a real world example, take a look at RIAStats.com. The data displayed on the front page spans 30 days, and includes millions of records. Calculating this data takes quite a bit of time. Fortunately, there's no need to recalculate the percentages for every single visitor. Instead, we can calculate the percentages once, and cache the result. We then serve visitors the cached result.

Handling Results per Argument:
Most server methods take arguments that change the response. We can use a key-value pair concept to handle this. Simply concatenate the arguments into a unique string, to form your "key". Then cache the appropriate response, associating it with this unique key.

So again, take a look at RIAStats.com, and see the drop down options at the top. Changing these clearly changes the arguments sent the server, and the results from the server. You'll probably notice when you hit an un-cached result, because the response takes a while. However, if you reload your browser, and ask for the same results a second time, the response will be much quicker.

Timing-out Results:
We probably don't want to keep cached results forever, because our data is likely to change. We'll simply set a cache timeout that will clear our results. With proper considerations, we can make our data both timely and efficient.

Installing MemCached

MemCached is an open source (and therefore cross-platform) executable that must be installed on your servers. Your first step will be to install and verify that the MemCached service is running on at least one of your test servers. Memcached has a website and a separate Google Code project. (Search google for install MemCached for your operating system. For linux, here's a link to a google cache of install instructions).

After installing the MemCached server, you'll need into add php-memcached to your server. This provides php some built-in memcached communications functions.

Example PHP

The following PHP code comes directly from RIAStats and implements all of the above concepts with Memcached. We'll return an AMF encoded response, using this AMF encoder class (zip file of a php class file).

<?

require_once("amf.class.php");

/* Listing out the 'arguments' used for this service */
$siteKey = $_GET["siteKey"];
$browser = $_GET["browser"];
$language = $_GET["language"];
$os = $_GET["language"];
$includeAboutRIA = $_GET["includeAboutRIA"];

/* Init AMF encoder */
$encoder = new amfdata();

/* Set the return content type, because we're doing AMF here */
header('Content-type: application/x-amf');

/* Create they 'key' for this value */
$memCacheKey = "statss".$siteKey."b".$browser."o".$os."l".$language."i".$includeAboutRIA;

/* connect to memcached server */
$memCache = memcache_connect('localhost', 11211);

/* Get the cached value for this 'key' */
$memcacheResult = memcache_get($memCache, $memCacheKey);

/* Determine if there was a cached value */
if($memcacheResult != false) {

/*If so, return it */
print $memcacheResult;
exit(0);
}

$result = /* call some functions, do some operations, calculate somethings */

/* encode the result */
$response = $encoder->getEncodedResponse($result);

/* Cache the encoded response */
memcache_set($memCache, $memCacheKey, $response, 0, 86400);

/* Print out the results, memcached will be used next time*/
print $response;
exit(0);
?>

 

Post a comment




Remember Me?


(you may use HTML tags for style)

Twitter Status

Travis @wjrothman Please read the comment I just left on your Gizmodo article about "Why a Verizon iPhone May Drop Fewer Calls...". -TravisCollins

Last Seen in

Reston, Virginia

 
Privacy Policy

Copyright DreamingWell.com 1998 - 2011