Drupal MemCached Integration

, , ,

Though drupal has built-in cache mechanism, by default, Drupal will save all its caches to database, which means, every time you hit a cache, there will be a database query. This is obviously not the best solutions.

To speed up your site's cache, a better way is use a 3rd party caching system, such as Memcached, and stores all caches in memory. And in this article, we will tell you how to integrate Memcached with drupal.

What's Memcached

Memcached is a distributed memory object caching system which is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read. It is now widely used by hundreds of thousands of sites on the internet such as YouTube, Zynga, Facebook and Twitter.

Memcached runs on Unix, Linux Windows and MacOSX and is distributed under a permissive freee software license.

Install Memcached

Memcached is very likely in your source packages, so you can just try:

sudo yum install memcached

If you want build memcached from source code, remember install libevent first.

sudo yum install libevent-dev

To verify memcached is installed correctly, run:

memcached -h

And you'll see the help info for memcached.

After memcached installation completed, you can edit options for memcached to meet your need.

vi /etc/sysconfig/memcached

There are some common options:

CACHESIZE="64"    # Max cache size, Unit: MB.
MAXCONN=”1024″    # Max connection allowed.

Other options can be seen by:

memcached -h

If everything is ok, you can now start the memcached service.

service memcached start

Use the following shell code to test the connectivity:

echo -e "stats\r" | nc localhost 11211

Install Memcached Extension for PHP

To use memcached in php, you need install the PECL memcache extension for PHP.

yum install php-pecl-memcached

Edit your php.ini file, make sure the following line in your php.ini


Restart apache server, access "/admin/reports/status/php" of your drupal site. You'll found a new memcached section.

Install Memcached Module for Drupal

Download "Memcache API and Integration" module from http://drupal.org/project/memcache . Uncompress, and put it at /sites/all/modules/

Add following code to your settings.php (/sites/default)

$conf = array(
 'cache_inc' => './sites/all/modules/memcache/memcache.inc',

Access your drupal admin page, enable the "Memcache" and "Memcache Admin" module.
Then clear all the cache in "Administration -> Configuration -> Performance" Page.

Till now, the integration is done. Hope you enjoy it.

And you can view the memcached status at Administration -> Reports -> Memcached

Configuring Drupal With Multiple Memcached Instances

If you using more than one memcached server, you should edit the settings.php like this. (/sites/default/). Add something like this.

$conf = array(
  'cache_inc' => './sites/all/modules/memcache/memcache.inc',
  'memcache_servers' => array(
 // Servers/instances configured here
 'localhost:11211' => 'default',
 'localhost:11212' => 'page',
 'localhost:11213' => 'filter',
  'memcache_bins' => array(
 // Bins assigned here
 'cache'        => 'default',
 'cache_menu'   => 'default',
 'cache_page'   => 'page',
 'cache_filter' => 'filter',

Related Post

Latest Post

Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.