memcached

What is memcached?

memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use
in speeding up dynamic web applications by alleviating database load.

How it Works?

First, you start up the memcached daemon on as many spare machines as you have. The daemon has no configuration file, just a few command line options, only 3 or 4 of which you’ll likely use:

# ./memcached -d -m 2048 -l 10.0.0.40 -p 11211

This starts memcached up as a daemon, using 2GB of memory, and listening on IP 10.0.0.40, port 11211. Because a 32-bit process can only address 4GB of virtual memory (usually significantly less, depending on your operating system), if you have a 32-bit server with 4-64GB of memory using PAE you can just run multiple processes on the machine, each using 2 or 3GB of memory.

How to install ?

The folowing steps involved in installing the memcached,

1. Download memcache from code.jellycan.com/memcached/ [grab the ‘win32 binary’ version]

2. Install memcache as a service:
* Unzip and copy the binaries to your desired directory (eg. c:\memcached) [you should see one file, memcached.exe] – thanks to Stephen for the heads up on the new version
* If you’re running Vista, right click on memcached.exe and click Properties. Click the Compatibility tab. Near the bottom you’ll see Privilege Level, check “Run this program as an administrator”.
* Install the service using the command: c:\memcached\memcached.exe -d install from the command line
* Start the server from the Microsoft Management Console or by running one of the following commands: c:\memcached\memcached.exe -d start, or net start “memcached Server”

Now that you have memcache installed, you’ll have to tie it in with PHP in order to use it.

1. Check your php extensions directory [should be something like: C:\php\ext] for php_memcache.dll
If you don’t have any luck finding it, try looking at one of these sites:
– downloads.php.net/pierre/ [thanks to Henrik Gemal]
– pecl4win.php.net/ext.php/php_memcache.dll [currently down]
http://www.pureformsolutions.com/pureform.wordpress.com/2008/06/17/php_memcache.dll for PHP 5.2.*
– kromann.info/download.php?strFolder=php5_1-Release_TS&strIndex=PHP5_1 for PHP 5.1.* [thanks, Rich]

2. Now find your php.ini file [default location for XP Pro is C:\WINDOWS\php.ini] and add this line to the extensions list:

extension=php_memcache.dll

3. Restart apache

4. Run this code to test the installation: [found on http://www.php.net/memcache%5D

<?php
$memcache = new Memcache;
$memcache->connect(“localhost”,11211); # You might need to set “localhost” to “127.0.0.1”

echo “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”));
?>

If you see anything but errors, you are now using memcache!

EDIT the memcached setting:

Memcached, by default, loads with 64mb of memory for it’s use which is low for most applications. To change this to something else, navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached Server in your registry, find the ImagePath entry and change it to look something like this:

“C:\memcached\memcached.exe” -d runservice -m 512

Now when you start the service via net start “memcached Server”, it will run with 512mb of memory at it’s disposal.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: