Tuesday 19 March 2019

Memcache Integration with Drupal 8

Memcache

Memcache improves Drupal application performance by moving standard caches out of the database and caching the results of other expensive database operations. Memcache stores these caches/DB objects in RAM.

Install memcache on the server and configure it with Drupal 8 to reduce the load on the database with every page load.

Install memcache on server
1. Run the following commands in terminal.
    sudo apt-get update
    sudo apt install memcached
    sudo apt install php-memcached
2. Run following command to make sure memcache daemon is working fine.
    ps aux | grep memcached
3. Create phpinfo.php page and check there "memcached" extension is properly configured in PHP7
    vim /var/www/html/phpinfo.php
4. Restart both memcached and php7-fpm services.
    service memcached restart
    service php7.0-fpm restart
    check in browser by opening above create phpinfo.php file.










5. After installing memcache in your server, download Memcache module and Memcache Storage Module
Memcache module having 2 submodules: 
1. Memcache  2. Memcache Admin. Enable memcache and memcache admin module.

Configure Memcache and Memcache Admin module Drupal 8
1. Add below configurations in settings.php file.
/**
 * Memcache settings
 */
$settings['memcache']['servers'] = ['127.0.0.1:11211' => 'default'];
$settings['memcache']['bins'] = ['default' => 'default'];
$settings['cache']['default'] = 'cache.backend.memcache';
$settings['memcache']['key_prefix'] = 'cms_memcache_';
$settings['cache']['bins']['bootstrap'] = 'cache.backend.memcache';
$settings['cache']['bins']['discovery'] = 'cache.backend.memcache';
$settings['cache']['bins']['config'] = 'cache.backend.memcache';
$settings['cache']['bins']['container'] = 'cache.backend.memcache';
$settings['cache']['bins']['data'] = 'cache.backend.memcache';
$settings['cache']['bins']['default'] = 'cache.backend.memcache';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.memcache';
$settings['cache']['bins']['entity'] = 'cache.backend.memcache';
$settings['cache']['bins']['menu'] = 'cache.backend.memcache';
$settings['cache']['bins']['page'] = 'cache.backend.memcache';
$settings['cache']['bins']['render'] = 'cache.backend.memcache';
$settings['cache']['bins']['rest'] = 'cache.backend.memcache';
$settings['cache']['bins']['signal'] = 'cache.backend.memcache';
$settings['cache']['bins']['toolbar'] = 'cache.backend.memcache';
$settings['cache']['bins']['ultimate_cron_logger'] = 'cache.backend.memcache';


2. Memcache statistics can be seen on the admin > Reports > Memcache Statistics as in screenshot.




3. To enable statistics on all pages. go to admin > config > system > memcache







 4. Now visit a single page and see the statistics for that page.
a. First time page load. 







b. After first time page load. 






Now compare the page load time in above both screenshot. After first time load, page is coming from the memcache and page load time has been decreased.


Configuring Memcache and Memcache Storage with Drupal 8
1. Paste the below code after opening the settings.php file.
    // Set’s default cache storage as Memcache and excludes database connection for cache
    $settings['cache']['default'] = 'cache.backend.memcache_storage';
   // Set’s Memcache key prefix for your site and useful in working sites with same memcache as           backend.
   $settings['memcache_storage']['key_prefix'] = '';
  // Set’s Memcache storage server’s.
  $settings['memcache_storage']['memcached_servers'] =  ['127.0.0.1:11211' => 'default'];

2. To debug Memcache, include below code in settings.php file.
// Enables to display total hits and misses
$settings['memcache_storage']['debug'] = TRUE;

Now you can see Memcache messages by debug enabled in above line.

NOTE: Comment memcache settings from settings.php file before uninstalling Memcache and Memcache storage.

1 comment:

  1. Hi Raj
    Your Drupal Blog is very nice. Can you help me in issue which i am facing currently - Memcache instance could not be initialized. Check memcache is running and reachable

    ReplyDelete