Installing and Using Memcached for PHP on Ubuntu

September 11, 2011 · Posted in Development 

Memcached is 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. Start from my article “Memcached Overview” to get an overview of memcached on Linux and Windows.

Install Memcached server

sudo apt-get install memcached

If needed install the following packages:
sudo apt-get install make
sudo apt-get install php5-dev
sudo apt-get install php-pear
sudo apt-get install zlib1g-dev

Restart apache
sudo service apache2 restart

Now memcached is installed.

Run memcached server

memcached -u root -d -m 24 -l 127.0.0.1 -p 11211

Replace 24 with a number of megabytes of cache.

Test memcached with Telnet

telnet 127.0.0.1 11211

and do these tests:

set test2 1 0 2
ab
STORED
set test3 1 0 3
abc
STORED
get test2
VALUE test2 1 2
ab
END
get test3
VALUE test3 1 3
abc
END
set test4 1 0 2
abcde
CLIENT_ERROR bad data chunk
ERROR

Start memcached server automatically after system reboots

Edit the file “/etc/rc.local”:

Add this line

memcached -u www-data -d -m 16 -l 127.0.0.1 -p 11211

before the line
exit 0

Configuring Memcached server:

Edit the file /etc/memcached.conf

 

Restart memcached:

sudo /etc/init.d/memcached restart

PHP Memcached Clients

There are two main PHP libraries which work with a memcached server: php-memcache and php-memcached.

php-memcache vs. php-memcached

php-memcache

The client library php-memcache was developed in 2004 and it is stable and is used in 99.9% of projects based on Memcached server.

The main disadvantage of this library is a lack of functionality. It implements only a part of memcache protocol and doesn’t not allow you to use advanced functions.

php-memcached

The client library php-memcached was developed comparerly early, but it used successfuly in several big projects (for instance, digg.com).

The main advantage of this library is that it fully implements memcache protocol, including:

  • CAS tokens
  • Callbacks
  • Method getDelayed()
  • Supporting of binary protocol
  • Avoid serialization using igbinary

 

performance

In most cases php-memcache libary is faster than php-memcached.

The shown time is the time of performing basic operations of setting/getting/deleting values 10 000 times based on this PHP script:

<?php
 
$ops = 10000;
 
$m = new Memcache();
$m->addServer('localhost');
 
$md = new Memcached();
$md->addServer('localhost', 11211);
 
echo "Test operations: {$ops}";
echo "<h3>get test</h3>";
 
$s = microtime(true);
for ( $i = 0; $i < $ops; $i++ ) $m->get( md5(rand(1000, 99999)) );
echo "Memcache: " . ($res['memcache']['set'] = microtime(true) - $s ) . "<br />";
 
$s = microtime(true);
for ( $i = 0; $i < $ops; $i++ ) $md->get( md5(rand(1000, 99999)) );
echo "Memcached: " . ($res['memcached']['set'] = microtime(true) - $s );
 
echo "<h3>set test</h3>";
 
$s = microtime(true);
for ( $i = 0; $i < $ops; $i++ ) $m->set(md5(rand(1000, 99999)), 2);
echo "Memcache: " . ($res['memcache']['get'] = microtime(true) - $s ) . "<br />";
 
$s = microtime(true);
for ( $i = 0; $i < $ops; $i++ ) $md->set(md5(rand(1000, 99999)), 2);
echo "Memcached: " . ($res['memcached']['get'] = microtime(true) - $s );
 
echo "<h3>delete test</h3>";
 
$s = microtime(true);
for ( $i = 0; $i < $ops; $i++ ) $m->delete(md5(rand(1000, 99999)));
echo "Memcache: " . ($res['memcache']['delete'] = microtime(true) - $s ) . "<br />";
 
$s = microtime(true);
for ( $i = 0; $i < $ops; $i++ ) $md->delete(md5(rand(1000, 99999)));
echo "Memcached: " . ($res['memcached']['delete'] = microtime(true) - $s );
 
echo "<h3>combined test</h3>";
 
$s = microtime(true);
for ( $i = 0; $i < $ops; $i++ )
{
	$key = md5(rand(1000, 99999));
	$m->set($key, 2);
	$m->get($key);
	$m->delete($key);
}
echo "Memcache: " . ($res['memcache']['combined'] = microtime(true) - $s ) . "<br />";
 
$s = microtime(true);
for ( $i = 0; $i < $ops; $i++ )
{
	$key = md5(rand(1000, 99999));
	$md->set($key, 2);
	$md->get($key);
	$md->delete($key);
}
echo "Memcached: " . ($res['memcached']['combined'] = microtime(true) - $s );
?>

The data and code is taken from this article (in russian).

 

Install php5-memcache client

sudo apt-get install php5-memcache

Enable the extension in php.ini (/etc/php5/apache2/php.ini) by adding the line

extension=memcache.so

Restart apache:

sudo service apache2 restart

 

Examples

Basic example of using memcched on PHP

<?php
    $memcache = new Memcache; // instantiating memcache extension class
    $memcache->connect("localhost",11211);
// try 127.0.0.1 instead of localhost if it is not working
 
    echo "Server's version: " . $memcache->getVersion() . "<br />\n";
 
    // we will create an array which will be stored in cache serialized
    $testArray = array('horse', 'cow', 'pig');
    $tmp       = serialize($testArray);
 
    $memcache->add("key", $tmp, 30);
 
    echo "Data from the cache:<br />\n";
    print_r(unserialize($memcache->get("key")));
?>

 

 

Comments

  • http://maxivak.com/memcached-overview/ Memcached Overview : Max Ivak Personal Site

    [...] Read this post “Installing and Using Memcached for PHP on Ubuntu“. [...]

  • http://www.imIBc.com Homepage

    … [Trackback]…

    [...] Read More here: maxivak.com/installing-and-using-memcached-for-php-on-ubuntu/ [...]…

  • Ashish Mathukiya

    Good example

  • Ashish Mathukiya

    How to store smarty template in memchache??