3 This file contains installation instructions for the 8.x-2.x version of the
4 Drupal Memcache module. Configuration differs between 8.x and 7.x versions
5 of the module, so be sure to follow the 7.x instructions if you are configuring
6 the 7.x-1.x version of this module!
11 - Availability of a memcached daemon: http://memcached.org/
12 - One of the two PECL memcache packages:
13 - http://pecl.php.net/package/memcache (recommended)
14 - http://pecl.php.net/package/memcached
16 For more detailed instructions on installing a memcached daemon or either of the
17 memcache PECL extensions, please see the documentation online at
18 https://www.drupal.org/node/1131458 which includes links to external
19 walk-throughs for various operating systems.
23 These are the steps you need to take in order to use this software. Order
26 1. Make sure you have one of the PECL memcache packages installed.
27 2. Enable the memcache module.
28 You need to enable the module in Drupal before you can configure it to run
29 as the default backend. This is so Drupal knows where to find everything.
30 2. Edit settings.php to configure the servers, clusters and bins that memcache
31 is supposed to use. You do not need to set this if the only memcache backend
32 is localhost on port 11211. By default the main settings will be:
33 $settings['memcache']['servers'] = ['127.0.0.1:11211' => 'default'];
34 $settings['memcache']['bins'] = ['default' => 'default'];
35 $settings['memcache']['key_prefix'] = '';
36 7. Edit settings.php to make memcache the default cache class, for example:
37 $settings['cache']['default'] = 'cache.backend.memcache';
38 8. If you wish to arbitrarily send cache bins to memcache, then you can do the
39 following. E.g. for the cache_render bin:
40 $settings['cache']['bins']['render'] = 'cache.backend.memcache';
42 ## ADVANCED CONFIGURATION ##
44 ### Multiple memcache backends ###
46 $settings['memcache']['servers'] = [
47 '127.0.0.1:11211' => 'default', // Default host and port
48 '127.0.0.1:11212' => 'default', // Default host with port 11212
49 '127.0.0.2:11211' => 'default', // Default port, different IP
50 'server1.com:11211' => 'default', // Default port with hostname
51 'unix:///path/to/socket' => 'default', 'Unix socket'
54 ### Multiple servers, bins and clusters ###
56 $settings['memcache'] = [
58 'server1:port' => 'default',
59 'server2:port' => 'default',
60 'server3:port' => 'cluster1',
61 'serverN:port' => 'clusterN',
62 'unix:///path/to/socket' => 'clusterS',
65 'default' => 'default',
73 The bin/cluster/server model can be described as follows:
75 - Servers are memcached instances identified by host:port.
77 - Clusters are groups of servers that act as a memory pool. Each cluster can
78 contain one or more servers.
80 - Multiple bins can be assigned to a cluster.
82 - The default cluster is 'default'.
84 - If a bin can not be found it will map to 'default'.
88 If you want to have multiple Drupal installations share memcached instances,
89 you need to include a unique prefix for each Drupal installation in the memcache
90 config in settings.php:
92 $settings['memcache']['key_prefix'] = 'something_unique';
94 ### Key Hash Algorithm ###
96 Note: if the length of your prefix + key + bin combine to be more than 250
97 characters, they will be automatically hashed. Memcache only supports key
98 lengths up to 250 bytes. You can optionally configure the hashing algorithm
99 used, however sha1 was selected as the default because it performs quickly with
102 $settings['memcache']['key_hash_algorithm'] = 'sha1';
104 Visit http://www.php.net/manual/en/function.hash-algos.php to learn more about
105 which hash algorithms are available.
107 ### Memcache Distribution ###
109 To use this module with multiple memcached servers, it is important that you set
110 the hash strategy to consistent. This is controlled in the PHP extension, not the
113 If using PECL memcache:
114 Edit /etc/php.d/memcache.ini (path may changed based on package/distribution) and
116 memcache.hash_strategy=consistent
118 You need to reload apache httpd after making that change.
120 If using PECL memcached:
121 Memcached options can be controlled in settings.php. Consistent distribution is
122 the default in this case but could be set using:
124 $setting['memcache']['options'] = [
125 Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
130 Memcache locks can be enabled through the services.yml file.
133 # Replaces the default lock backend with a memcache implementation.
135 class: Drupal\Core\Lock\LockBackendInterface
136 factory: memcache.lock.factory:get
138 ## Cache Container on bootstrap (with cache tags on database) ##
139 By default Drupal starts the cache_container on the database, in order to override that you can use the following code on your settings.php file. Make sure that the $class_load->addPsr4 is poiting to the right location of memcache (on this case modules/contrib/memcache/src)
141 In this mode, the database is still bootstrapped so that cache tag invalidation can be handled. If you want to avoid database bootstrap, see the container definition in the next section instead.
143 $memcache_exists = class_exists('Memcache', FALSE);
144 $memcached_exists = class_exists('Memcached', FALSE);
145 if ($memcache_exists || $memcached_exists) {
146 $class_loader->addPsr4('Drupal\\memcache\\', 'modules/contrib/memcache/src');
148 // Define custom bootstrap container definition to use Memcache for cache.container.
149 $settings['bootstrap_container_definition'] = [
153 'class' => 'Drupal\Core\Database\Connection',
154 'factory' => 'Drupal\Core\Database\Database::getConnection',
155 'arguments' => ['default'],
158 'class' => 'Drupal\Core\Site\Settings',
159 'factory' => 'Drupal\Core\Site\Settings::getInstance',
161 'memcache.settings' => [
162 'class' => 'Drupal\memcache\MemcacheSettings',
163 'arguments' => ['@settings'],
165 'memcache.factory' => [
166 'class' => 'Drupal\memcache\Driver\MemcacheDriverFactory',
167 'arguments' => ['@memcache.settings'],
169 'memcache.timestamp.invalidator.bin' => [
170 'class' => 'Drupal\memcache\Invalidator\MemcacheTimestampInvalidator',
171 # Adjust tolerance factor as appropriate when not running memcache on localhost.
172 'arguments' => ['@memcache.factory', 'memcache_bin_timestamps', 0.001],
174 'memcache.backend.cache.container' => [
175 'class' => 'Drupal\memcache\DrupalMemcacheInterface',
176 'factory' => ['@memcache.factory', 'get'],
177 'arguments' => ['container'],
179 'cache_tags_provider.container' => [
180 'class' => 'Drupal\Core\Cache\DatabaseCacheTagsChecksum',
181 'arguments' => ['@database'],
183 'cache.container' => [
184 'class' => 'Drupal\memcache\MemcacheBackend',
185 'arguments' => ['container', '@memcache.backend.cache.container', '@cache_tags_provider.container', '@memcache.timestamp.invalidator.bin'],
191 ## Cache Container on bootstrap (pure memcache) ##
192 By default Drupal starts the cache_container on the database, in order to override that you can use the following code on your settings.php file. Make sure that the $class_load->addPsr4 is poiting to the right location of memcache (on this case modules/contrib/memcache/src)
194 For this mode to work correctly, you must be using the overridden cache_tags.invalidator.checksum service.
195 See example.services.yml for the corresponding configuration.
197 $memcache_exists = class_exists('Memcache', FALSE);
198 $memcached_exists = class_exists('Memcached', FALSE);
199 if ($memcache_exists || $memcached_exists) {
200 $class_loader->addPsr4('Drupal\\memcache\\', 'modules/contrib/memcache/src');
202 // Define custom bootstrap container definition to use Memcache for cache.container.
203 $settings['bootstrap_container_definition'] = [
208 'class' => 'Drupal\Core\Site\Settings',
209 'factory' => 'Drupal\Core\Site\Settings::getInstance',
211 'memcache.settings' => [
212 'class' => 'Drupal\memcache\MemcacheSettings',
213 'arguments' => ['@settings'],
215 'memcache.factory' => [
216 'class' => 'Drupal\memcache\Driver\MemcacheDriverFactory',
217 'arguments' => ['@memcache.settings'],
219 'memcache.timestamp.invalidator.bin' => [
220 'class' => 'Drupal\memcache\Invalidator\MemcacheTimestampInvalidator',
221 # Adjust tolerance factor as appropriate when not running memcache on localhost.
222 'arguments' => ['@memcache.factory', 'memcache_bin_timestamps', 0.001],
224 'memcache.timestamp.invalidator.tag' => [
225 'class' => 'Drupal\memcache\Invalidator\MemcacheTimestampInvalidator',
226 # Remember to update your main service definition in sync with this!
227 # Adjust tolerance factor as appropriate when not running memcache on localhost.
228 'arguments' => ['@memcache.factory', 'memcache_tag_timestamps', 0.001],
230 'memcache.backend.cache.container' => [
231 'class' => 'Drupal\memcache\DrupalMemcacheInterface',
232 'factory' => ['@memcache.factory', 'get'],
233 # Actual cache bin to use for the container cache.
234 'arguments' => ['container'],
236 # Define a custom cache tags invalidator for the bootstrap container.
237 'cache_tags_provider.container' => [
238 'class' => 'Drupal\memcache\Cache\TimestampCacheTagsChecksum',
239 'arguments' => ['@memcache.timestamp.invalidator.tag'],
241 'cache.container' => [
242 'class' => 'Drupal\memcache\MemcacheBackend',
243 'arguments' => ['container', '@memcache.backend.cache.container', '@cache_tags_provider.container', '@memcache.timestamp.invalidator.bin'],
249 ## TROUBLESHOOTING ##
253 Failed to set key: Failed to set key: cache_page-......
256 Upgrade your PECL library to PECL package (2.2.1) (or higher).
259 Zlib compression at the php.ini level and Memcache conflict.
260 See http://drupal.org/node/273824
264 A module offering a UI for memcache is included. It provides aggregated and
265 per-page statistics for memcache.
269 ### Memcached PECL Extension Support ###
271 We also support the Memcached PECL extension. This extension backends
272 to libmemcached and allows you to use some of the newer advanced features in
275 NOTE: It is important to realize that the memcache php.ini options do not impact
276 the memcached extension, this new extension doesn't read in options that way.
277 Instead, it takes options directly from Drupal. Because of this, you must
278 configure memcached in settings.php. Please look here for possible options:
280 https://secure.php.net/manual/en/memcached.constants.php
282 An example configuration block is below, this block also illustrates our
283 default options (selected through performance testing). These options will be
284 set unless overridden in settings.php.
286 $settings['memcache']['options'] = [
287 Memcached::OPT_COMPRESSION => TRUE,
288 Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
291 These are as follows:
293 * Turn on compression, as this allows more data to be stored and in turn
294 should result in less evictions.
295 * Turn on consistent distribution, which allows you to add/remove servers
298 Other options you could experiment with:
299 + Memcached::OPT_BINARY_PROTOCOL => TRUE,
300 * This enables the Memcache binary protocol (only available in Memcached
301 1.4 and later). Note that some users have reported SLOWER performance
302 with this feature enabled. It should only be enabled on extremely high
303 traffic networks where memcache network traffic is a bottleneck.
304 Additional reading about the binary protocol:
305 https://raw.githubusercontent.com/memcached/old-wiki/master/MemcacheBinaryProtocol.wiki
306 Note: The information on the link above will eventually be ported to
307 the new wiki under https://github.com/memcached/memcached/wiki.
309 + Memcached::OPT_TCP_NODELAY => TRUE,
310 * This enables the no-delay feature for connecting sockets; it's been
311 reported that this can speed up the Binary protocol (see above). This
312 tells the TCP stack to send packets immediately and without waiting for
313 a full payload, reducing per-packet network latency (disabling "Nagling").
315 It's possible to enable SASL authentication as documented here:
316 http://php.net/manual/en/memcached.setsaslauthdata.php
317 https://code.google.com/p/memcached/wiki/SASLHowto
319 SASL authentication requires a memcached server with SASL support (version 1.4.3
320 or greater built with --enable-sasl and started with the -S flag) and the PECL
321 memcached client version 2.0.0 or greater also built with SASL support. Once
322 these requirements are satisfied you can then enable SASL support in the Drupal
323 memcache module by enabling the binary protocol and setting
324 memcache_sasl_username and memcache_sasl_password in settings.php. For example:
326 $settings['memcache']['sasl'] = [
327 'username' => 'user',
328 'password' => 'password',
331 // When using SASL, Memcached extension needs to be used
332 // because Memcache extension doesn't support it.
333 $settings['memcache']['extension'] = 'Memcached';
334 $settings['memcache']['options'] = [
335 \Memcached::OPT_BINARY_PROTOCOL => TRUE,