Friday, April 17, 2009

Memcached - Distributed Memory Object Cache System

Memcached is a high performance, distributed memory object caching system. This caching system running primarily in Linux/Unix and there are various port in Windows, Mac OS X and Solaris.

Setting up the memcached in windows is easy, download the Win32 binary and put it somewhere. Fire the following commands:
memcached.exe -d install
/* Install the memcached as windows service */


memcached.exe -d start -m 512
/* Start the memcached instance with maximum memory 512M */


Configuring PHP using the Memcached:
  1. Download php_memcache.dll in Google as pecl4win.php.net was down in 2008.
  2. Copy the php_memcache.dll to \ext
  3. Edit the php.ini to include the line
  4. extension=php_memcache.dll

  5. Restart Apache
Test the memcache is working through the following code.

<?php
$memcache = new Memcache;
$memcache->addServer("localhost",11211); /* Default Port */
$memcache->addServer("localhost", 22333); /* Comment this out if you are not starting multiple incidence */

print "Server's version: " . $memcache->getVersion() . "<br />\n";

$tmp_object = new stdClass;
$tmp_object->str_attr = "test";
$tmp_object->int_attr = 123;

$memcache->set("key",$tmp_object,false,10);
echo "Store data in the cache (data will expire in 10 seconds)<br />\n";

echo "Data from the cache:<br />\n";
var_dump($memcache->get("key"));
?>


Read the PHP Memcache Menu

There is another key-value pair cache called Redis. Will talk more in other posts.

No comments:

Post a Comment