Memcached Config For Drupal On Ubuntu

Memcached is a 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 used in Drupal mostly for caching Database queries in memory to improve authenticated user experience and reduce load on database. Primarily cache tables are swapped to have a memcached backend.

We can find a lot of tutorials to install Memcached on debian system including memcache PHP extension. However this post is mostly on configuration for Drupal 7 sites.

Once memcached is installed, we can provide a per site memcached socket. This can be done my modifying /etc/init.d/memcached file. Approximately at line 44: include the following.

# Make /var/run/memcached directory
if [ ! -d "/var/run/memcached" ]; then
  mkdir /var/run/memcached
  chown memcache /var/run/memcached
fi

As you can see from the usage notes in the /etc/init.d/memcached file, you need to copy /etc/memcached.conf to a new file and edit it to create the configuration for a Memcached daemon:

# The naming convention is memcached_myserver.conf, this will
# create the config for a Memcached instance called myserver
# You can name what you please
cp /etc/memcached.conf /etc/memcached_mysite.conf

Now to edit /etc/memcached_myserver.conf to our liking. Lets say the new site for which the config below is mysite.com

# The memory limit is on line 23, it is measured in MB, it is probably safe to leave it at 64MB.
# You can monitor it and increase it later if necessary.
# Worth noting is that the daemon will not start out using this
# much memory for storage, but will only grow to use this much space.
-m 64
# Comment out the default port on line 26 like so:
# -p 11211
#Comment out the IP address option on line 35 like so:
# -l 127.0.0.1
# At the bottom of the file add the location of your socket by specifying the -s switch:
-s /var/run/memcached/memcached_mysite.sock
# The name of the socket isn't important as long as you remember it for later.
# I just stick to the config file convention, replacing .conf with .sock as I then always
# know which socket belongs to which instance.

# Finally add the -a switch to specify the octal file mode of the socket when it is created:
-a 0766
# You may not want the socket to be world writable, I have not set it up like that just yet.

All Memcached instances can be started by executing

# Start all Memcached instances with:
service memcached start
# Alternatively start instances by name, for memcached_mysite.conf:
service memcached start mysite
# Stopping works the same way:
service memcached stop # For all the instances
service memcached stop mysite # For one instance

In the settings.php file of mysite.com , update at the bottom section as follows. However before these changes download the memcache drupal module as the path of memcache.inc file would be needed for swapping the backend from DatabaseCache to MemCache.

$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
$conf['cache_default_class'] = 'MemCacheDrupal';
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
# If you wanted multiple Drupal installations to share one Memcache instance use the prefix like so.
$conf['memcache_key_prefix'] = 'mysite_1245';
$conf['memcache_servers'] = array('unix:///var/run/memcached/memcached_mysite.sock' => 'default');
// This assigns all cache bins to the 'default' cluster from above
// $conf['memcache_bins'] = array('cache' => 'default');
$conf['memcache_bins'] = array(
'cache' => 'default'
,'cache_block' => 'default'
,'cache_bootstrap' => 'default'
,'cache_content' => 'default'
,'cache_field' => 'default'
,'cache_filter' => 'default'
,'cache_image' => 'default'
,'cache_menu' => 'default'
,'cache_page' => 'default'
,'cache_views' => 'default'
,'cache_path' => 'default'
,'cache_views_data' => 'default');
// For Anonymous Users
$conf['page_cache_without_database'] = TRUE;
$conf['page_cache_invoke_hooks'] = FALSE;
//Locking mechanism
$conf['lock_inc'] = 'sites/all/modules/memcache/memcache-lock.inc';
//Stampede Protection
$conf['memcache_stampede_protection'] = TRUE;

Enable memcache_admin available with the downloaded memcache module and view the statistics in the reports section.

Source
--
http://www.failover.co/blog/drupal-7-memcache-pecl-memcache-memcached-an...
http://www.thefanclub.co.za/how-to/how-install-memcached-on-ubuntu-for-d...