PHP5 & Memcached (Example)
In this example I’ll be showing you how to read and write from a memcached server using PHP5, using singleton class design and some basic static functions to connect your servers up before performing the caching. For those of you who don’t know what memcached is I have included the description from the memcached website below.
If you want to know more technical information visit the official website: memcached.org
“Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.”
For instructions on how to install memcached; visit the above website and view the tutorials and how-to’s – It’s pretty simple to set up on a Linux server and depending on which distribution you are using you can probably install it with a few lines in the terminal.
Make sure you have both memcached and php5-memcached extension installed correctly and that apache is restarted so the new module is loaded.
Cacher Class
Below is the Cacher class that will allow you to communicate with memcached; it’s basically an extended memcache class with extra features to only connect once to the memcache server (as it’s static) this will allow us to read and write from the memcached server.
Remember to change the $MEMCACHE_SERVERS array to include the IP address of the memcache server; otherwise it will not work.
<?php
class Cacher extends Memcache {
static private $MEMCACHE_SERVERS = array(
"127.0.0.1", //ADD SERVER IP 1 HERE
);
static public $memcacheObj = NULL;
static function cache() {
if (self::$memcacheObj == NULL) {
if (class_exists('Memcache')) {
self::$memcacheObj = new Memcache;
foreach(self::$MEMCACHE_SERVERS as $server){
self::$memcacheObj->addServer ($server);
}
} else {
return false;
}
}
return self::$memcacheObj;
}
static function flushCache() {
if (self::$memcacheObj == NULL) {
self::cache();
}
return self::$memcacheObj->flush();
}
static function stats() {
if (self::$memcacheObj == NULL) {
self::cache();
}
return self::$memcacheObj->getExtendedStats();
}
}
?>
Usage
The usage of this class is fairly simple; include this file anywhere you wish to use caching and call the static function within the class. I have provided an example below:
<?php
include('cacher.php');
$myData = Cacher::cache()->get("myData");
//If $myData is not from the cache.
if($myData === false) {
$myData = doLongFunc(); //replace with whatever function you wish to cache.
Cacher::cache()->set("myData", $myData, false, 300); //300 is 5 Minutes
}
echo $myData;
?>
That’s basically it, if the data exists in the memcache store it will use it otherwise it’ll get the data normally and then store it into the cache. The example above shows basic usage, you could of course have other functions reading/writing cache data for you. Remember to use different ‘keys’ for different data you wish to cache.
There are also two other functions I have put into the class stats() and flushCache() for quick access, you can use them the same way: Cacher::stats(); and Cacher::flushCache();
I will probably be doing a follow up tutorial on how to install memcached and some more advanced usage of it; but for now that’s all – Happy caching!
