Memcached is a popular memory object caching system. Since it’s an open-source software and free, you should use Memcached for your Drupal 8 website to speed up your website.
In this article-cum-guide, we explain how to improve the performance of your Drupal 8 website on shared hosting plans.
You can implement Memcached with HTML, CSS, or JavaScript caching.
Step-by-Step Process
Here are the steps you need to follow:
If your web hosting company allows to change the current PHP version, then set your PHP to the latest version. Then, from the extensions available, check both Memcache and Memcached. Save the changes you’ve made.
You should do the same if you’re using RHEL, CentOS, or CloudLinux.
For shared hosting accounts, the option to change such settings in cPanel might not be available because of security reasons. So you’d have to contact your hosting provide for that.
For those on VPS or dedicated servers, they can install Memcached with Sudo commands, which are:
Once finished, you need to restart the extensions of the Apache web server by executing this command:
So go ahead and download the Memcache module from Durpal modules library. It will provide the backend necessary for Caching and Locking. The files relating to Caching and Locking are memcache.inc and memcache-lock.inc respectively.
To install the Memcache module, follow this path - /admin/modules/install. Then, upload the downloaded modules.
Once done, enable two modules, which are:
In case your web server doesn’t come with PECL Memcache, execute this command to install it as well:
sudo pecl install memcache
Go to this directory - /admin/config/system/memcache
Change the settings and configurations as needed. You’ll also find statistics and debugging data. Lastly, you must leave the “Show memcache statistics at the bottom of each page”.
To get started, execute this command in Drush:
Then, rebuild the Drush cache:
You’ll see two new commands after clearing the cache, which are:
If you need more information on these commands, you can use:
Open the settings.php file from the location sites/default and add in the following code:
If you’re on any other web hosting plan powered by cPanel, you need to find out Memcached’s port number and use that to replace the port value “11211” mentioned in the code above.
If you’ve got websites that are running Memcached with more than one server in clusters, then open the settings.php file and add in the following code that contain routing information:
Multiple Memcache Backends:
Multiple Servers, Bins and Clusters:
This will conclude the installation of Memcached. Unless you need advanced configurations for your Drupal 8 website, this should be it. You may, however, test the settings in administration for debugging.
Here are a few options:
Add a Key Hash Algorithm
You may also want to enable Memcache locks. For this, open the services.yml file and replace the default lock backend with the following code:
After you’re done, save the changes and reload things like Apache and Drupal 8. Now you have optimized Caching with Memcached for your Drupal 8 website. For more information, get in touch with your hosting support team.
In this article-cum-guide, we explain how to improve the performance of your Drupal 8 website on shared hosting plans.
You can implement Memcached with HTML, CSS, or JavaScript caching.
Step-by-Step Process
Here are the steps you need to follow:
- Install Memcached
If your web hosting company allows to change the current PHP version, then set your PHP to the latest version. Then, from the extensions available, check both Memcache and Memcached. Save the changes you’ve made.
You should do the same if you’re using RHEL, CentOS, or CloudLinux.
For shared hosting accounts, the option to change such settings in cPanel might not be available because of security reasons. So you’d have to contact your hosting provide for that.
For those on VPS or dedicated servers, they can install Memcached with Sudo commands, which are:
Code:
sudo apt-get update
Code:
sudo apt install memcached
sudo apt install php-memcached
Code:
service memcached restart
service php7.0-fpm restart
- Install the Memcache Module
So go ahead and download the Memcache module from Durpal modules library. It will provide the backend necessary for Caching and Locking. The files relating to Caching and Locking are memcache.inc and memcache-lock.inc respectively.
To install the Memcache module, follow this path - /admin/modules/install. Then, upload the downloaded modules.
Once done, enable two modules, which are:
- Memcache
- Memcache Admin
In case your web server doesn’t come with PECL Memcache, execute this command to install it as well:
sudo pecl install memcache
Go to this directory - /admin/config/system/memcache
Change the settings and configurations as needed. You’ll also find statistics and debugging data. Lastly, you must leave the “Show memcache statistics at the bottom of each page”.
- Install Memcache Module Using Drush and Drush Commands
To get started, execute this command in Drush:
Code:
drush en memcache
Code:
drush cc drush
Code:
memcache-flush (mcf) Flush all Memcached objects in a bin.
memcache-stats (mcs) Retrieve statisticsfrom Memcached.
Code:
drush help mcf
drush help mcs
- Set Advanced Configuration Options
Open the settings.php file from the location sites/default and add in the following code:
Code:
$settings['memcache']['servers'] = ['127.0.0.1:11211' => 'default'];
$settings['memcache']['bins'] = ['default' => 'default'];
$settings['memcache']['key_prefix'] = '';
$settings['cache']['default'] = 'cache.backend.memcache';
$settings['cache']['bins']['render'] = 'cache.backend.memcache';
If you’ve got websites that are running Memcached with more than one server in clusters, then open the settings.php file and add in the following code that contain routing information:
Multiple Memcache Backends:
Code:
$settings['memcache']['servers'] = [
'127.0.0.1:11211' => 'default', // Default host and port
'127.0.0.1:11212' => 'default', // Default host with port 11212
'127.0.0.2:11211' => 'default', // Default port, different IP
'server1.com:11211' => 'default', // Default port with hostname
'unix:///path/to/socket' => 'default', 'Unix socket'
];
Code:
$settings['memcache'] = [
'servers' = [
'server1:port' => 'default',
'server2:port' => 'default',
'server3:port' => 'cluster1',
'serverN:port' => 'clusterN',
'unix:///path/to/socket' => 'clusterS',
],
'bins' => [
'default' => 'default',
'bin1' => 'cluster1',
'binN' => 'clusterN',
'binX' => 'cluster1',
'binS' => 'clusterS',
],
];
- Lock, Key Hash Algo, & Memcache Prefix Options
Here are a few options:
- Add Memcache Prefix
Code:
$settings['memcache']['key_prefix'] = 'something_unique';
Code:
$settings['memcache']['key_hash_algorithm'] = 'sha1';
Code:
lock:
class: DrupalCoreLockLockBackendInterface
factory: memcache.lock.factory:get