memcached

image from www.memcached.org

Based on their official website memcached is defined as:

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.

— memcached.org

MemCached Server

This is how I installed(a simple installation) my memcached server that can be accessed by a webserver on our local area network. The server is running Ubuntu as its operating system. Caution: The version on ubuntu‘s repository could not be the latest version. Refer to memcached’s website for the latest version. If you want to install the latest version from the source you can refer to memcached’s wiki page

Open synaptic package manager and install memcached and php5-memcached. After the installation restart the webserver using the command:

sudo apache2ctl restart

Start memcached by using the command:

/etc/init.d/memcached start

By default memcached is running on port 11211 and is accessible by the machine with IP 127.0.0.1 which is the machine’s local IP address with 64MB memory allocation

To try if memcached is working use this simple PHP code

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
< ?php
$m = new Memcached();
$m->addServer('localhost', 11211);
$m->set('int', 99);
$m->set('string', 'a simple string');
$m->set('array', array(11, 12));
$m->set('object', new stdclass, time() + 300);
var_dump($m->get('int'));
var_dump($m->get('string'));
var_dump($m->get('array'));
var_dump($m->get('object'));
?>

Open the script on the browser. Try opening the cli and telnet to memcached using the command:

telnet 127.0.0.1 11211

The image below shows how the cli would look like after the command

telnet to memcached

Check memcached’s status by using the command

stats

We can get a cached value using the command:

get string

It should return “a simple string” as assigned on the script.

To explore more, commands can be seen on this page http://lzone.de/articles/memcached.htm

To allow another machine (local web server) to store data on the memcached server we need to edit the configuration file using the command:

sudo nano /etc/memcached.conf

In my case I changed the allotted memory to 256MB and allowed IP of 191.168.1.254 which is our windows webserver as seen on the image below:

memcached configuration

After the configuration I restarted memcached using the command:

sudo /etc/init.d/memcached restart

The Webserver (XAMPP)

To quickly test the memcached server I used xampp portable version which can be downloaded on their official website www.apachefriends.org.

As stated earlier the webserver is already allowed to use the memcached server. As an information memcached is a daemon which runs like the mysql server, so we need PHP to connect to the memcached server.

  1. To start, download memcached on code.jellycan.com/memcached which is win32 binary.
  2. After downloading extract the file (recommended: C:\memcached). You should have the file memcached.exe
  3. We can install memcached as a service on the command line using the command:

    C:\memcached\memcached.exe -d install
  4. Start memcached using the command:

    C:\memcached\memcached.exe -d start
  5. After running memcached we need to tie it with PHP using the php_memcached.dll. You should have this file on your PHP‘s ext folder, if not download the file at these links http://downloads.php.net/pierre/, http://www.pureformsolutions.com/pureform.wordpress.com/2008/06/17/php_memcache.dll or on our server: php_memcached.dll.rar
  6. enable the use of php_memcached.dll by editing your php.ini and uncomment the line extension=php_memcache.dll if it does not exist add this line. After editing the file restart the webserver.

You can access the data on the memcached server by using the code below:

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
< ?php
$memcache = new Memcache();
if(!$memcache->connect('191.168.1.7', 11211))
die("Couldn't connect to memcached!");
$key="string";
$result = $memcache->get($key);
if($result)
{
echo "$key is $result";
}
else
{
echo 'There is no data saved named '. $key;
}
?>

You should have an output of “a simple string”. I hope this helped you set up the server. Happy coding!

Sources:
http://pureform.wordpress.com/2008/01/10/installing-memcache-on-windows-for-php/

http://memcached.org

http://blogs.oracle.com/oswald/entry/cache_cache_cache_part_1