Faster your drupal site with Memcached

Memcache installation and configuration for Drupal

Introduction Memcached

You will get an idea about the working of Memcached with his name only.

Memcached is an open source high performance and free caching system or service.

It's helps to cache API calls, database data and pages in RAM to perform application faster.
This cached system works on TCP protocol which means you can able to install it on a separate server and use it on another server which leads to the big hash table to store your data.
One more important thing about Memcached is if your Memcached is running out of space then older data is overwritten or replaced by newer data. It will work as a non-persistent cache.
It will work or runs on default port i.e. 11211

Advantages:

1. It is open source cache system which means its freely available.
2. It will work as cross-platform.
3. It will also handle the sessions.
4. it is a distributed memory system.

Disadvantages:

1. Data will not be persistent means data will be flush out if you restart the service.
2. It will be using only 1/4 of the size of your RAM memory, so if your size out of space then data is overwritten.

When we use Memcached?

We will use memcached to decrease the load on the database by caching database queries which are most frequently or more user visited.

Installation:

Follow below step by step process to install service on a linux server.

Step 1: Install Memcached Package

Run below command to install the memcached and libmemcached-tools package on your server.

  • apt-get  install  memcached  libmemcached-tools

Step 2: Check configurtion

After installing service, you need to check the below lines enabled in memcached.conf file if not enable it by uncommenting it.

vim /etc/php5/mods-available/memcache.ini

; uncomment the next line to enable the module
extension=memcache.so

[memcache]
memcache.dbpath="/var/lib/memcache"
memcache.maxreclevel=0
memcache.maxfiles=0
memcache.archivememlim=0
memcache.maxfilesize=0
memcache.maxratio=0

save and close the file.

Step 3: Change memory size

The most important part of installation is to set the value of Memory limit to store data on Memcached. This value should be 1/4 of your system or server
RAM memory.

vim /etc/memcached.conf

#Replace below value from default 64 MB to 512Mb or as per RAM size of your server.
-m 512

save and close the file.

Step 4: Restart Service

After all, configuration changes you will need to restart the service to take effect of changes you did in the configuration file.

/etc/init.d/memcached  start|stop|restart

Step 5: Check Memcached is running

Check service is up and running with below commands.

netstat -tulnp | grep memcached
o/p :

tcp  0  0  127.0.0.1:11211  0.0.0.0:*  LISTEN 1391/memcached

Configure memcached with Drupal 7.x

Now we are going to install Memcache Drupal module which is integrating the drupal caching system with Memcache.

Download Memcache module and put it under the "sites/all/modules".

Step 1: Download modules

Now integrating Memcache with Drupal you need to install two modules which are memcache and memcache-storage on the server under the "sites/all/modules" directory.

Download below links and extract it.

  • cd ../sites/all/modules/
  • wget https://ftp.drupal.org/files/projects/memcache-7.x-1.5.tar.gz
  • wget https://ftp.drupal.org/files/projects/memcache_storage-7.x-1.4.tar.gz
  • tar -xvf memcache-7.x-1.5.tar.gz
  • tar -xvf memcache_storage-7.x-1.4.tar.gz
  • rm -f memcache-7.x-1.5.tar.gz memcache_storage-7.x-1.4.tar.gz

Step 2: Change directory permissions

Now change the permission of extracted directory of Memcache so they are accessible for Drupal.

  • chown -R www-data.www-data memcache memcache_storage
  • find -type d -exec chmod 775 {} ;
  • find -type f -exec chmod 664 {} ;

Step 3: Add Module

After completing installation part from server end login to your Drupal site admin panel and click on "Modules" tab. Once you're in modules tab come all way down or search "Memcache" you will find the list of the Memcache module just click on the check box of all three modules then click save the configuration.

Memcache Module drupal

Step 4: Change in settings.php

After saving the configuration in admin panel you will get some error of settings.php file is not configured, etc. just ignored the error and come to the server then open settings.php file which resides under "sites/default/" directory. Add below provided Memcached code to the end of settings.php file this code helps to integrate Memcached with the drupal caching system.

vim ../sites/default/settings.php

//Add the 127.0.0.1 or if your using different server for memcached then private ip of that server on which memcached is binding.
$conf['memcache_servers'] = array('127.0.0.1:11211' => 'default');

$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
$conf['cache_backends'][] = 'sites/all/modules/memcache_storage/memcache_storage.inc';
$conf['lock_inc'] = 'sites/all/modules/memcache/memcache-lock.inc';
$conf['memcache_stampede_protection'] = TRUE;
$conf['cache_default_class'] = 'MemCacheDrupal';

// The 'cache_form' bin must be assigned to non-volatile storage.
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';

// Don't bootstrap the database when serving pages from the cache.
$conf['page_cache_without_database'] = TRUE;
$conf['page_cache_invoke_hooks'] = FALSE;
// Add prefix value as per your requirement.
$conf['memcache_key_prefix'] = 'drupal';

save and close the file.

Step 5: Check module installed correctly

Go back to the Drupal or your site admin panel and open tab "Reports-->status report" and you will see all your Memcached modules installed successfully.

memcache-module2

To check Memcached running click on "more information" link next to Memcached and refresh your site you will see the hits count will be increasing which means Memcached storing sites data.

memcache-module3

I hope you like the article if you find any difficulties while installing MemCache with Drupal then please do comment your queries or problem via the comment section, till then stay tuned to techthings.org for more such valuable articles.

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *