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 *:- memcached, the daemon itself
- libevent
- If you use Linux, you'll want epoll
- And of course the memcache extension from PECL
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;
}
}
$sMyString = "I'm going to be cached!";
clsMem::getMem()->set("mykey", $sMyString, false, 600)
or die ("Could not write to the cache");
- 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.
$sMyString = clsMem::getMem()->get("mykey")
or die ("Couldn't find the requested item in the cache!");
echo $sMyString;
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;
}
}