I've recently discovered something truly awesome that many of you might not know about: the extension for PHP. The extension can be obtained from the PECL website. The memcache extension is an interface to the memcached daemon which was originally developed by Danga Interactive. It's purpose at the time was speeding up LiveJournal.com which is a very heavy-traffic website. In this article I'll explain what the memcache extension is and how it can help you increase the performance of any dynamic website dramatically.

Installing the memcache extension

In order to install the memcache extension you need to install several packages on your server. Therefore this extension probably isn't for everyone because I don't think it's very widespread among providers offering shared hosting environments. What you need is the following packages *: Installation instructions are fairly straightforward which is why I'm not going to discuss those here. Once you've installed the extension you need to restart your Apache webserver. You can check whether the extension is running by using phpinfo(). If all is ok you'll see a section about memcache.

The memcached daemon

After installing the memcache extension you need to start the memcached daemon. You can do this by issueing the following command:
memcached -d -m 1024 -l 10.0.0.1 -p 11211
The switch tells it to start as a daemon. The -m switch specifies the amount of memory in megabytes. The -l switch specifies the IP to listen on and finally the -p switch specifies the port to listen on. The default port is 11211 and if your machine has just 1 IP you can omit the -l parameter. In the above example I set the amount of memory to 1GB. Of course you should use a sensible amount of memory. Making your machine swap to disk sort of defeats the purpose of a memory cache daemon. Note that it's perfectly fine to run the memcached daemon on another machine than the one you're running your actual PHP project on. You could even set up a machine totally dedicated to being a memory cache server. And if that's not even enough you can set up muliple servers as well. The sky is the limit. Note: if you try to start the memcached as root it will require you to specify a user under which it should run with an additional -u switch

Using the memcache extension

The memcache extension has it's own section in the PHP manual in which the API is described. I guess not so many people have discovered this great extension yet since there aren't any user contributed notes to be found on these pages. However if you're a bit of a PHP programmer you'll probably find what you need here. In the examples here I'm keeping things simple. I'm assuming a memcached daemon running on the same machine on which the PHP project is running. The extension comes with both a procedural and an object oriented interface. Of course we're using the OO interface here, with PHP5. In order to make life as easy as possible we can create a little singleton:
include_once("constants.php");
// Memcache singleton object
class clsMem extends Memcache {
	static private $m_objMem = NULL;
	static function getMem() {
		if (self::$m_objMem == NULL) {
			self::$m_objMem = new Memcache;
			// connect to the memcached on some 
                        //host __MEMHOST running it om __MEMPORT
			self::$m_objMem->connect(__MEMHOST, __MEMPORT) 
                        or die ("The memcached server");
		}
		return self::$m_objMem;
	}
}
In the include file constants.php I've defined __MEMHOST and __MEMPORT to be the IP number and port on my server. Excellent, we're all set up. We can start enjoying the memcache extension now. Let's look at some examples! Let's create an entry in the cache.
$sMyString = "I'm going to be cached!";
clsMem::getMem()->set("mykey", $sMyString, false, 600) 
or die ("Could not write to the cache");
The memcache object has several methods. With the Memcache::set method we can add an entry to the cache. The method had four arguments:
  • a key
  • any serializable variable we'd like to cache, in this case it's a string
  • a boolean whether we want to use on-the-fly zip compression using libz
  • the cache expiry measured in seconds.
That's all there's to it. We've now inserted a string into the cache. Now let's suppose we want to use this string a while later in another PHP page:
$sMyString = clsMem::getMem()->get("mykey") 
or die ("Couldn't find the requested item in the cache!");
echo $sMyString;
You've probably guessed it, it will print out the previously cached string, if we try this within 10 minutes after storing it of course. If we're too late the Memcache::get method will return false. Of course caching simple strings doesn't really make sense. The fun really starts when we cache resultsets from heavy database queries. Let's have a look at a way to do this!
function cachedSQL($sSQL)  {
  $sSQL = "SELECT some really heavy stuff FROM some table";
  if($objResultset = clsMem::getMem()->get(MD5($sSQL))  {
    // return the cached resultset
    return $objResultSet;
    }
  else  {
    // assuming we have a PEAR::DB based database 
    // singleton handling the query
    $objResultSet = DB::getDB()->query->($sSQL);
    // Store the resultset in the cache
    clsMem::getMem()->set(MD5($sSQL), $objResultSet, true, 600);
    // return it 
    return $objResultSet;
    }
  }

Conclusion

Here we are, we now have cached SQL queries without any trouble. Of course I won't have to tell you what this can do for the performance of your (heavy load) website. As noted earlier, we can cache any PHP entity that can be serialized. This means strings, integers, objects, arrays, almost anything really. The memcache extension is easy to use with it's extremely simple interface. At this moment I'm using it to build a complex chat engine for my mobile date & chat community. At this moment every refresh on the chatboxes result in numerous SQL queries. Those will all be gone very soon and all chat activity (which is volatile data anyway) will be stored in the memory cache. If you've never tried this PECL extension for PHP you should most definitely check it out! Resources: *Want to on Windows? I have no idea whether that will work. Most probably not like just about everything else...
 
RockySomewhere near the Orion NebulaBookalicio.usGolden Gate BridgeThames River BankJackie and mePimpin' it