You are here

README.txt in Memcache API and Integration 7

Same filename and directory in other branches
  1. 8.2 README.txt
  2. 5.2 README.txt
  3. 5 README.txt
  4. 6 README.txt
## IMPORTANT NOTE ##

This file contains installation instructions for the 7.x-1.x version of the
Drupal Memcache module. Configuration differs between 7.x and 6.x versions
of the module, so be sure to follow the 6.x instructions if you are configuring
the 6.x-1.x version of this module!

## REQUIREMENTS ##

- PHP 5.1 or greater
- Availability of a memcached daemon: http://memcached.org/
- One of the two PECL memcache packages:
  - http://pecl.php.net/package/memcache (recommended)
  - http://pecl.php.net/package/memcached (latest versions require PHP 5.2 or
    greater)

## INSTALLATION ##

These are the steps you need to take in order to use this software. Order
is important.

 1. Install the memcached binaries on your server and start the memcached
    service. Follow best practices for securing the service; for example,
    lock it down so only your web servers can make connections. Find community
    maintained documentation with a number of walk-throughs for various
    operating systems at https://www.drupal.org/node/1131458.
 2. Install your chosen PECL memcache extension -- this is the memcache client
    library which will be used by the Drupal memcache module to interact with
    the memcached server(s). Generally PECL memcache (3.0.6+) is recommended,
    but PECL memcached (2.0.1+) also works well for some people. There are
    known issues with older versions. Refer to the community maintained
    documentation referenced above for more information.
 3. Put your site into offline mode.
 4. Download and install the memcache module.
 5. If you have previously been running the memcache module, run update.php.
 6. Optionally edit settings.php to configure the servers, clusters and bins
    for memcache to use. If you skip this step the Drupal module will attempt to
    talk to the memcache server on port 11211 on the local host, storing all
    data in a single bin. This is sufficient for most smaller, single-server
    installations.
 7. Edit settings.php to make memcache the default cache class, for example:
      $conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
      $conf['cache_default_class'] = 'MemCacheDrupal';
    The cache_backends path needs to be adjusted based on where you installed
    the module.
 8. Make sure the following line also exists, to ensure that the special
    cache_form bin is assigned to non-volatile storage:
      $conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
 9. Optionally also add the following two lines to tell Drupal not to bootstrap
    the database when serving cached pages to anonymous visitors:
      $conf['page_cache_without_database'] = TRUE;
      $conf['page_cache_invoke_hooks'] = FALSE;
    If setting page_cache_without_database to TRUE, you also have to set
    page_cache_invoke_hooks to FALSE or you'll see an error like "Fatal error:
    Call to undefined function module_list()".
10. Bring your site back online.

## DRUSH ##

Enable the memcache module at admin/modules or with 'drush en memcache', then
rebuild the drush cache by running 'drush cc drush'. This will enable the
following drush commands:

  memcache-flush (mcf)  Flush all Memcached objects in a bin.
  memcache-stats (mcs)  Retrieve statistics from Memcached.

For more information about each command, use 'drush help'. For example:
  drush help mcf

Or:
  drush help mcs

## ADVANCED CONFIGURATION ##

This module is capable of working with one memcached instance or with multiple
memcached instances run across one or more servers. The default is to use one
server accessible on localhost port 11211. If that meets your needs, then the
configuration settings outlined above are sufficient for the module to work.
If you want to use multiple memcached instances, or if you are connecting to a
memcached instance located on a remote machine, further configuration is
required.

The available memcached servers are specified in $conf in settings.php. If you
do not specify any servers, memcache.inc assumes that you have a memcached
instance running on localhost:11211. If this is true, and it is the only
memcached instance you wish to use, no further configuration is required.

If you have more than one memcached instance running, you need to add two arrays
to $conf; memcache_servers and memcache_bins. The arrays follow this pattern:

'memcache_servers' => array(
  server1:port => cluster1,
  server2:port => cluster2,
  serverN:port => clusterN,
  'unix:///path/to/socket' => clusterS
)

'memcache_bins' => array(
   bin1 => cluster1,
   bin2 => cluster2,
   binN => clusterN,
   binS => clusterS
)

You can optionally assign a weight to each server, favoring one server more than
another. For example, to make it 10 times more likely to store an item on
server1 versus server2:

'memcache_servers' => array(
  server1:port => array('cluster' => cluster1, 'weight' => 10),
  server2:port => array('cluster' => cluster2, 'weight' => 1'),
)

The bin/cluster/server model can be described as follows:

- Servers are memcached instances identified by host:port.

- Clusters are groups of servers that act as a memory pool. Each cluster can
  contain one or more servers.

- Bins are groups of data that get cached together and map 1:1 to the $table
  parameter of cache_set(). Examples from Drupal core are cache_filter and
  cache_menu. The default is 'cache'.

- Multiple bins can be assigned to a cluster.

- The default cluster is 'default'.

## LOCKING ##

The memcache-lock.inc file included with this module can be used as a drop-in
replacement for the database-mediated locking mechanism provided by Drupal
core. To enable, define the following in your settings.php:

  $conf['lock_inc'] = 'sites/all/modules/memcache/memcache-lock.inc';

Locks are written in the 'semaphore' table, which will map to the 'default'
memcache cluster unless you explicitly configure a 'semaphore' cluster.

## STAMPEDE PROTECTION ##

Memcache includes stampede protection for rebuilding expired cache items. To
enable stampede protection, define the following in settings.php:

  $conf['memcache_stampede_protection'] = TRUE;

To avoid lock stampedes, it is important that you enable the memcache lock
implementation when enabling stampede protection -- enabling stampede protection
without enabling the Memcache lock implementation can cause worse performance and
can result in dropped locks due to key-length truncation.

Memcache stampede protection is primarily designed to benefit the following
caching pattern: a miss on a cache_get() for a specific cid is immediately
followed by a cache_set() for that cid. Of course, this is not the only caching
pattern used in Drupal, so stampede protection can be selectively disabled for
optimal performance.  For example, a cache miss in Drupal core's
module_implements() won't execute a cache_set until drupal_page_footer()
calls module_implements_write_cache() which can occur much later in page
generation.  To avoid long hanging locks, stampede protection should be
disabled for these delayed caching patterns.

Memcache stampede protection can be disabled for entire bins, specific cid's in
specific bins, or cid's starting with a specific prefix in specific bins. For
example:

  $conf['memcache_stampede_protection_ignore'] = array(
    // Ignore some cids in 'cache_bootstrap'.
    'cache_bootstrap' => array(
      'module_implements',
      'variables',
      'lookup_cache',
      'schema:runtime:*',
      'theme_registry:runtime:*',
      '_drupal_file_scan_cache',
    ),
    // Ignore all cids in the 'cache' bin starting with 'i18n:string:'
    'cache' => array(
      'i18n:string:*',
    ),
    // Disable stampede protection for the entire 'cache_path' and 'cache_rules'
    // bins.
    'cache_path',
    'cache_rules',
  );

Only change the following stampede protection tunables if you're sure you know
what you're doing, which requires first reading the memcache.inc code.

The value passed to lock_acquire, defaults to '15':
  $conf['memcache_stampede_semaphore'] = 15;

The value passed to lock_wait, defaults to 5:
  $conf['memcache_stampede_wait_time'] = 5;

The maximum number of calls to lock_wait() due to stampede protection during a
single request, defaults to 3:
  $conf['memcache_stampede_wait_limit'] = 3;

When adjusting these variables, be aware that:
 - there is unlikely to be a good use case for setting wait_time higher
   than stampede_semaphore;
 - wait_time * wait_limit is designed to default to a number less than
   standard web server timeouts (i.e. 15 seconds vs. apache's default of
   30 seconds).

## CACHE LIFETIME ##

Memcache respects Drupal core's minimum cache lifetime configuration. This
setting affects all cached items, not just pages. In some cases, it may
be desirable to cache different types of items for different amounts of time.
You can override the minimum cache lifetime on a per-bin basis in settings.php.
For example:

  // Cache pages for 60 seconds.
  $conf['cache_lifetime_cache_page'] = 60;
  // Cache menus for 10 minutes.
  $conf['cache_lifetime_menu'] = 600;

## CACHE HEADER ##

Drupal core indicates whether or not a page was served out of the cache by
setting the 'X-Drupal-Cache' response header with a value of HIT or MISS. If
you'd like to confirm whether pages are actually being retreived from Memcache
and not another backend, you can enable the following option:

  $conf['memcache_pagecache_header'] = TRUE;

When enabled, the Memcache module will add its own 'Drupal-PageCache-Memcache'
header. When cached pages are served out of the cache the header will include an
'age=' value indicating how many seconds ago the page was stored in the cache.

## PERSISTENT CONNECTIONS ##

As of 7.x-1.6, the memcache module uses peristent connections by default. If
this causes you problems you can disable persistent connections by adding the
following to your settings.php:

  $conf['memcache_persistent'] = FALSE;

## STRICT COMPATIBILITY WITH DB CACHE EXPIRATIONS ##

Due to the way database caching works, the native Drupal cache will return
expired cache objects which were set to expire in the future even after their
expiration timestamp, because it doesn't clean up cache entries until the
cache bins are garbage collected (normally during a cron.php run's general
cache wipe). However, memcache can expire cached items at the specific time
requested. Therefore the default behavior of the memcache module does not
match the Drupal API for cache_set, which states that cache items set to
expire in the future are kept at least until the given timestamp, after which
they behave like CACHE_TEMPORARY (removed at the next general cache wipe).

If you wish to return to the behavior described in the cache_set API, and
allow expired entries to appear valid until a general cache wipe, define the
following in settings.php:

  $conf['memcache_expire_wait_gc'] = TRUE;

This setting works independently from stampede support, though it changes the
time at which timestamp-cached items are considered expired, and therefore
affects the time at which stampede behavior happens (if enabled).

## EXAMPLES ##

Example 1:

First, the most basic configuration which consists of one memcached instance
running on localhost port 11211 and all caches except for cache_form being
stored in memcache. We also enable stampede protection, and the memcache
locking mechanism. Finally, we tell Drupal to not bootstrap the database when
serving cached pages to anonymous visitors.

  $conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.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;

Note that no servers or bins are defined.  The default server and bin
configuration which is used in this case is equivalant to setting:

  $conf['memcache_servers'] = array('localhost:11211' => 'default');


Example 2:

In this example we define three memcached instances, two accessed over the
network, and one on a Unix socket -- please note this is only an illustration of
what is possible, and is not a recommended configuration as it's highly unlikely
you'd want to configure memcache to use both sockets and network addresses like
this, instead you'd consistently use one or the other.

The instance on port 11211 belongs to the 'default' cluster where everything
gets cached that isn't otherwise defined. (We refer to it as a "cluster", but in
this example our "clusters" involve only one instance.) The instance on port
11212 belongs to the 'pages' cluster, with the 'cache_page' table mapped to
it -- so the Drupal page cache is stored in this cluster.  Finally, the instance
listening on a socket is part of the 'blocks' cluster, with the 'cache_block'
table mapped to it -- so the Drupal block cache is stored here. Note that
sockets do not have ports.

  $conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.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 no 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;

  // Important to define a default cluster in both the servers
  // and in the bins. This links them together.
  $conf['memcache_servers'] = array('10.1.1.1:11211' => 'default',
                                    '10.1.1.1:11212' => 'pages',
                                    'unix:///path/to/socket' => 'blocks');
  $conf['memcache_bins'] = array('cache' => 'default',
                                 'cache_page' => 'pages',
                                 'cache_block' => 'blocks');


Example 3:

Here is an example configuration that has two clusters, 'default' and
'cluster2'. Five memcached instances running on four different servers are
divided up between the two clusters. The 'cache_filter' and 'cache_menu' bins
go to 'cluster2'. All other bins go to 'default'.

  $conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.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 no 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;

  $conf['memcache_servers'] = array('10.1.1.6:11211' => 'default',
                                    '10.1.1.6:11212' => 'default',
                                    '10.1.1.7:11211' => 'default',
                                    '10.1.1.8:11211' => 'cluster2',
                                    '10.1.1.9:11211' => 'cluster2');

  $conf['memcache_bins'] = array('cache' => 'default',
                                 'cache_filter' => 'cluster2',
                                 'cache_menu' => 'cluster2');
  );

## PREFIXING ##

If you want to have multiple Drupal installations share memcached instances,
you need to include a unique prefix for each Drupal installation in the $conf
array of settings.php. This can be a single string prefix, or a keyed array of
bin => prefix pairs:

   $conf['memcache_key_prefix'] = 'something_unique';

Using a per-bin prefix:

   $conf['memcache_key_prefix'] = array(
     'default' => 'something_unique',
     'cache_page' => 'something_else_unique'
   );

In the above example, the 'something_unique' prefix will be used for all bins
except for the 'cache_page' bin which will use the 'something_else_unique'
prefix. Note that if using a keyed array for specifying prefix, you must specify
the 'default' prefix.

It is also possible to specify multiple prefixes per bin. Only the first prefix
will be used when setting/getting cache items, but all prefixes will be cleared
when deleting cache items. This provides support for more complicated
configurations such as a live instance and an administrative instance each with
their own prefixes and therefore their own unique caches. Any time a cache item
is deleted on either instance, it gets flushed on both -- thus, should an admin
do something that flushes the page cache, it will appropriately get flushed on
both instances. (For more discussion see the issue where support was added,
https://www.drupal.org/node/1084448.) This feature is enabled when you configure
prefixes as arrays within arrays. For example:

  // Live instance.
  $conf['memcache_key_prefix'] = array(
    'default' => array(
      'live_unique', // live cache prefix
      'admin_unique', // admin cache prefix
    ),
  );

The above would be the configuration of your live instance. Then, on your
administrative instance you would flip the keys:

  // Administrative instance.
  $conf['memcache_key_prefix'] = array(
    'default' => array(
      'admin_unique', // admin cache prefix
      'live_unique', // live cache prefix
    ),
  );

## EXPERIMENTAL - ALTERNATIVE SERIALIZE ##

This is a new experimental feature added to the memcache module in version
7.x-1.6 and should be tested carefully before utilizing in production.

To optimize how data is serialized before it is written to memcache, you can
enable either the igbinary or msgpack PECL extension. Both switch from using
PHP's own human-readable serialized data strucutres to more compact binary
formats.

No specicial configuration is required.  If both extensions are enabled,
memcache will automatically use the igbinary extension. If only one extension
is enabled, memcache will automatically use that extension.

You can optionally specify which extension is used by adding one of the
following to your settings.php:

  // Force memcache to use PHP's core serialize functions
  $conf['memcache_serialize'] = 'serialize';

  // Force memcache to use the igbinary serialize functions (if available)
  $conf['memcache_serialize'] = 'igbinary';

  // Force memcache to use the msgpack serialize functions (if available)
  $conf['memcache_serialize'] = 'msgpack';

To review which serialize function is being used, enable the memcache_admin
module and visit admin/reports/memcache.

IGBINARY:

The igbinary project is maintained on GitHub:
 - https://github.com/phadej/igbinary

The official igbinary PECL extension can be found at:
 - https://pecl.php.net/package/igbinary

Version 2.0.1 or greater is recommended.

MSGPACK:

The msgpack project is maintained at:
  - https://msgpack.org

The official msgpack PECL extension can be found at:
  - https://pecl.php.net/package/msgpack

Version 2.0.2 or greater is recommended.

## MAXIMUM LENGTHS ##

If the length of your prefix + key + bin combine to be more than 250 characters,
they will be automatically hashed. Memcache only supports key lengths up to 250
bytes. You can optionally configure the hashing algorithm used, however sha1 was
selected as the default because it performs quickly with minimal collisions.

Visit http://www.php.net/manual/en/function.hash-algos.php to learn more about
which hash algorithms are available.

$conf['memcache_key_hash_algorithm'] = 'sha1';

You can also tune the maximum key length BUT BE AWARE this doesn't affect
memcached's server-side limitations -- this value is primarily exposed to allow
you to further shrink the length of keys to optimize network performance.
Specifying a length larger than 250 will almost certainly lead to problems
unless you know what you're doing.

$conf['memcache_key_max_length'] = 250;

By default, the memcached server can store objects up to 1 MiB in size. It's
possible to increase the memcached page size to support larger objects, but this
can also lead to wasted memory. Alternatively, the Drupal memcache module splits
these large objects into smaller pieces. By default, the Drupal memcache module
splits objects into 1 MiB sized pieces. You can modify this with the following
tunable to match any special server configuration you may have. NOTE: Increasing
this value without making changes to your memcached server can result in
failures to cache large items.

(Note: 1 MiB = 1024 x 1024 = 1048576.)

$conf['memcache_data_max_length'] = 1048576;

It is generally undesirable to store excessively large objects in memcache as
this can result in a performance penalty. Because of this, by default the Drupal
memcache module logs any time an object is cached that has to be split into
multiple pieces. If this is generating too many watchdog logs, you should first
understand why these objects are so large and if anything can be done to make
them smaller. If you determine that the large size is valid and is not causing
you any unnecessary performance penalty, you can tune the following variable to
minimize or disable this logging. Set the value to a positive integer to only
log when an object is split into this many or more pieces. For example, if
memcache_data_max_length is set to 1048576 and memcache_log_data_pieces is set
to 5, watchdog logs will only be written when an object is split into 5 or more
pieces (objects >4 MiB in size). Or, to to completely disable logging set
memcache_log_data_pieces to 0 or FALSE.

$conf['memcache_log_data_pieces'] = 2;

## MULTIPLE SERVERS ##

To use this module with multiple memcached servers, it is important that you set
the hash strategy to consistent. This is controlled in the PHP extension, not
the Drupal module.

If using PECL memcache:
Edit /etc/php.d/memcache.ini (path may changed based on package/distribution)
and set the following:
memcache.hash_strategy=consistent

You need to reload apache httpd after making that change.

If using PECL memcached:
Memcached options can be controlled in settings.php.  The following setting is
needed:
$conf['memcache_options'] = array(
  Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
);

## DEBUG LOGGING ##

You can optionally enable debug logging by adding the following to your
settings.php:
  $conf['memcache_debug_log'] = '/path/to/file.log';

By default, only the following memcache actions are logged: 'set', 'add',
'delete', and 'flush'. If you'd like to also log 'get' and 'getMulti' actions,
enble verbose logging:
  $conf['memcache_debug_verbose'] = TRUE;

This file needs to be writable by the web server (and/or by drush) or you will
see lots of watchdog errors. You are responsible for ensuring that the debug log
doesn't get too large. By default, enabling debug logging will write logs
looking something like:

  1484719570|add|semaphore|semaphore-memcache_system_list%3Acache_bootstrap|1
  1484719570|set|cache_bootstrap|cache_bootstrap-system_list|1
  1484719570|delete|semaphore|semaphore-memcache_system_list%3Acache_bootstrap|1

The default log format is pipe delineated, containing the following fields:
  timestamp|action|bin|cid|return code

You can specify a custom log format by setting the memcache_debug_log_format
variable. Supported variables that will be replaced in your format are:
'!timestamp', '!action', '!bin', '!cid', and '!rc'.
For example, the default log format (note that it includes a new line at the
end) is:
  $conf['memcache_debug_log_format'] = "!timestamp|!action|!bin|!cid|!rc\n";

You can change the timestamp format by specifying a PHP date() format string in
the memcache_debug_time_format variable. PHP date() formats are documented at
http://php.net/manual/en/function.date.php. By default timestamps are written as
a unix timestamp. For example:
  $conf['memcache_debug_time_format'] = 'U';

## TROUBLESHOOTING ##

PROBLEM:
 Error:
  Failed to load required file memcache/dmemcache.inc
 Or:
 cache_backends not properly configured in settings.php, failed to load
 required file memcache.inc

SOLUTION:
You need to enable memcache in settings.php. Search for "Example 1" above
for a basic configuration example.

PROBLEM:
 Error:
  PECL !extension version %version is unsupported. Please update to
  %recommended or newer.

SOLUTION:
Upgrade to the latest available PECL extension release. Older PECL extensions
have known bugs and cause a variety of problems when using the memcache module.

PROBLEM:
 Error:
  Failed to connect to memcached server instance at <IP ADDRESS>.

SOLUTION:
Verify that the memcached daemon is running at the specified IP and PORT. To
debug you can try to telnet directly to the memcache server from your web
servers, example:
   telnet localhost 11211

PROBLEM:
 Error:
  Failed to store to then retrieve data from memcache.

SOLUTION:
Carefully review your settings.php configuration against the above
documentation. This error simply does a cache_set followed by a cache_get
and confirms that what is written to the cache can then be read back again.
This test was added in the 7.x-1.1 release.

The following code is what performs this test -- you can wrap this in a <?php
tag and execute as a script with 'drush scr' to perform further debugging.

        $cid = 'memcache_requirements_test';
        $value = 'OK';
        // Temporarily store a test value in memcache.
        cache_set($cid, $value);
        // Retreive the test value from memcache.
        $data = cache_get($cid);
        if (!isset($data->data) || $data->data !== $value) {
          echo t('Failed to store to then retrieve data from memcache.');
        }
        else {
          // Test a delete as well.
          cache_clear_all($cid, 'cache');
        }

PROBLEM:
 Error:
  Unexpected failure when testing memcache configuration.

SOLUTION:
Be sure the memcache module is properly installed, and that your settings.php
configuration is correct. This error means an exception was thrown when
attempting to write to and then read from memcache.

PROBLEM:
 Error:
  Failed to set key: Failed to set key: cache_page-......

SOLUTION:
Upgrade your PECL library to PECL package (2.2.1) (or higher).

WARNING:
Zlib compression at the php.ini level and Memcache conflict.
See http://drupal.org/node/273824

## MEMCACHE ADMIN ##

A module offering a UI for memcache is included. It provides aggregated and
per-page statistics for memcache.

## Memcached PECL Extension Support

We also support the Memcached PECL extension. This extension backends
to libmemcached and allows you to use some of the newer advanced features in
memcached 1.4.

NOTE: It is important to realize that the memcache php.ini options do not impact
the memcached extension, this new extension doesn't read in options that way.
Instead, it takes options directly from Drupal. Because of this, you must
configure memcached in settings.php. Please look here for possible options:

http://us2.php.net/manual/en/memcached.constants.php

An example configuration block is below, this block also illustrates our
default options (selected through performance testing). These options will be
set unless overridden in settings.php.

  $conf['memcache_options'] = array(
    Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
  );

These are as follows:

 * Turn on consistent distribution, which allows you to add/remove servers
   easily

Other options you could experiment with:
 + Memcached::OPT_COMPRESSION => FALSE,
    * This disables compression in the Memcached extension. This may save some
      CPU cost, but can result in significantly more data being transmitted and
      stored. See: https://www.drupal.org/project/memcache/issues/2958403

 + Memcached::OPT_BINARY_PROTOCOL => TRUE,
    * This enables the Memcache binary protocol (only available in Memcached
      1.4 and later). Note that some users have reported SLOWER performance
      with this feature enabled. It should only be enabled on extremely high
      traffic networks where memcache network traffic is a bottleneck.
      Additional reading about the binary protocol:
        http://code.google.com/p/memcached/wiki/MemcacheBinaryProtocol

 + Memcached::OPT_TCP_NODELAY => TRUE,
    * This enables the no-delay feature for connecting sockets; it's been
      reported that this can speed up the Binary protocol (see above). This
      tells the TCP stack to send packets immediately and without waiting for
      a full payload, reducing per-packet network latency (disabling "Nagling").

### Authentication

#### Binary Protocol SASL Authentication

SASL authentication can be enabled as documented here:
  http://php.net/manual/en/memcached.setsaslauthdata.php
  https://code.google.com/p/memcached/wiki/SASLHowto

SASL authentication requires a memcached server with SASL support (version 1.4.3
or greater built with --enable-sasl and started with the -S flag) and the PECL
memcached client version 2.0.0 or greater also built with SASL support. Once
these requirements are satisfied you can then enable SASL support in the Drupal
memcache module by enabling the binary protocol and setting
memcache_sasl_username and memcache_sasl_password in settings.php. For example:

  $conf['memcache_options'] = array(
    Memcached::OPT_BINARY_PROTOCOL => TRUE,
  );
  $conf['memcache_sasl_username'] = 'yourSASLUsername';
  $conf['memcache_sasl_password'] = 'yourSASLPassword';

#### ASCII Protocol Authentication

If you do not want to enable the binary protocol, you can instead enable
token authentication with the default ASCII protocol.

ASCII protocol authentication requires Memcached version 1.5.15 or greater
started with the -Y flag, and the PECL memcached client. It was originally
documented in the memcached 1.5.15 release notes:
  https://github.com/memcached/memcached/wiki/ReleaseNotes1515

While it will work with 1.5.15 or greater, it's strongly recommended you
use memcached 1.6.4 or greater due to the following bug fix:
  https://github.com/memcached/memcached/wiki/ReleaseNotes164

Additional detail about this feature can be found in the protocol documentation:
  https://github.com/memcached/memcached/blob/master/doc/protocol.txt

All your memcached servers need to be started with the -Y option to specify
a local path to an authfile which can contain up to 8 "username:pasword"
pairs, any of which can be used for authentication. For example, a simple
authfile may look as follows:

  foo:bar

You can then configure your website to authenticate with this username and
password as follows:

  $conf['memcache_ascii_auth'] = 'foo bar';

Enabling ASCII protocol authentication during load testing resulted in less than
1% overhead.

## Amazon Elasticache

You can use the Drupal Memcache module to talk with Amazon Elasticache, but to
enable Automatic Discovery you must use Amazon's forked version of the PECL
Memcached extension with Dynamic Client Mode enabled.

Their PECL Memcached fork is maintained on GitHub:
 - https://github.com/awslabs/aws-elasticache-cluster-client-memcached-for-php

If you are using PHP 7 you need to select the php7 branch of their project.

Once the extension is installed, you can enable Dynamic Client Mode as follows:

  $conf['memcache_options'] = array(
    Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
    Memcached::OPT_CLIENT_MODE  => Memcached::DYNAMIC_CLIENT_MODE,
  );

You then configure the module normally. Amazon explains:
  "If you use Automatic Discovery, you can use the cluster's Configuration
   Endpoint to configure your Memcached client."

The Configuration Endpoint must have 'cfg' in the name or it won't work. Further
documentation can be found here:
http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/Endpoints.html

If you don't want to use Automatic Discovery you don't need to install the
forked PECL extension, Amazon explains:
  "If you don't use Automatic Discovery, you must configure your client to use
   the individual node endpoints for reads and writes. You must also keep track
   of them as you add and remove nodes."

File

README.txt
View source
  1. ## IMPORTANT NOTE ##
  2. This file contains installation instructions for the 7.x-1.x version of the
  3. Drupal Memcache module. Configuration differs between 7.x and 6.x versions
  4. of the module, so be sure to follow the 6.x instructions if you are configuring
  5. the 6.x-1.x version of this module!
  6. ## REQUIREMENTS ##
  7. - PHP 5.1 or greater
  8. - Availability of a memcached daemon: http://memcached.org/
  9. - One of the two PECL memcache packages:
  10. - http://pecl.php.net/package/memcache (recommended)
  11. - http://pecl.php.net/package/memcached (latest versions require PHP 5.2 or
  12. greater)
  13. ## INSTALLATION ##
  14. These are the steps you need to take in order to use this software. Order
  15. is important.
  16. 1. Install the memcached binaries on your server and start the memcached
  17. service. Follow best practices for securing the service; for example,
  18. lock it down so only your web servers can make connections. Find community
  19. maintained documentation with a number of walk-throughs for various
  20. operating systems at https://www.drupal.org/node/1131458.
  21. 2. Install your chosen PECL memcache extension -- this is the memcache client
  22. library which will be used by the Drupal memcache module to interact with
  23. the memcached server(s). Generally PECL memcache (3.0.6+) is recommended,
  24. but PECL memcached (2.0.1+) also works well for some people. There are
  25. known issues with older versions. Refer to the community maintained
  26. documentation referenced above for more information.
  27. 3. Put your site into offline mode.
  28. 4. Download and install the memcache module.
  29. 5. If you have previously been running the memcache module, run update.php.
  30. 6. Optionally edit settings.php to configure the servers, clusters and bins
  31. for memcache to use. If you skip this step the Drupal module will attempt to
  32. talk to the memcache server on port 11211 on the local host, storing all
  33. data in a single bin. This is sufficient for most smaller, single-server
  34. installations.
  35. 7. Edit settings.php to make memcache the default cache class, for example:
  36. $conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
  37. $conf['cache_default_class'] = 'MemCacheDrupal';
  38. The cache_backends path needs to be adjusted based on where you installed
  39. the module.
  40. 8. Make sure the following line also exists, to ensure that the special
  41. cache_form bin is assigned to non-volatile storage:
  42. $conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
  43. 9. Optionally also add the following two lines to tell Drupal not to bootstrap
  44. the database when serving cached pages to anonymous visitors:
  45. $conf['page_cache_without_database'] = TRUE;
  46. $conf['page_cache_invoke_hooks'] = FALSE;
  47. If setting page_cache_without_database to TRUE, you also have to set
  48. page_cache_invoke_hooks to FALSE or you'll see an error like "Fatal error:
  49. Call to undefined function module_list()".
  50. 10. Bring your site back online.
  51. ## DRUSH ##
  52. Enable the memcache module at admin/modules or with 'drush en memcache', then
  53. rebuild the drush cache by running 'drush cc drush'. This will enable the
  54. following drush commands:
  55. memcache-flush (mcf) Flush all Memcached objects in a bin.
  56. memcache-stats (mcs) Retrieve statistics from Memcached.
  57. For more information about each command, use 'drush help'. For example:
  58. drush help mcf
  59. Or:
  60. drush help mcs
  61. ## ADVANCED CONFIGURATION ##
  62. This module is capable of working with one memcached instance or with multiple
  63. memcached instances run across one or more servers. The default is to use one
  64. server accessible on localhost port 11211. If that meets your needs, then the
  65. configuration settings outlined above are sufficient for the module to work.
  66. If you want to use multiple memcached instances, or if you are connecting to a
  67. memcached instance located on a remote machine, further configuration is
  68. required.
  69. The available memcached servers are specified in $conf in settings.php. If you
  70. do not specify any servers, memcache.inc assumes that you have a memcached
  71. instance running on localhost:11211. If this is true, and it is the only
  72. memcached instance you wish to use, no further configuration is required.
  73. If you have more than one memcached instance running, you need to add two arrays
  74. to $conf; memcache_servers and memcache_bins. The arrays follow this pattern:
  75. 'memcache_servers' => array(
  76. server1:port => cluster1,
  77. server2:port => cluster2,
  78. serverN:port => clusterN,
  79. 'unix:///path/to/socket' => clusterS
  80. )
  81. 'memcache_bins' => array(
  82. bin1 => cluster1,
  83. bin2 => cluster2,
  84. binN => clusterN,
  85. binS => clusterS
  86. )
  87. You can optionally assign a weight to each server, favoring one server more than
  88. another. For example, to make it 10 times more likely to store an item on
  89. server1 versus server2:
  90. 'memcache_servers' => array(
  91. server1:port => array('cluster' => cluster1, 'weight' => 10),
  92. server2:port => array('cluster' => cluster2, 'weight' => 1'),
  93. )
  94. The bin/cluster/server model can be described as follows:
  95. - Servers are memcached instances identified by host:port.
  96. - Clusters are groups of servers that act as a memory pool. Each cluster can
  97. contain one or more servers.
  98. - Bins are groups of data that get cached together and map 1:1 to the $table
  99. parameter of cache_set(). Examples from Drupal core are cache_filter and
  100. cache_menu. The default is 'cache'.
  101. - Multiple bins can be assigned to a cluster.
  102. - The default cluster is 'default'.
  103. ## LOCKING ##
  104. The memcache-lock.inc file included with this module can be used as a drop-in
  105. replacement for the database-mediated locking mechanism provided by Drupal
  106. core. To enable, define the following in your settings.php:
  107. $conf['lock_inc'] = 'sites/all/modules/memcache/memcache-lock.inc';
  108. Locks are written in the 'semaphore' table, which will map to the 'default'
  109. memcache cluster unless you explicitly configure a 'semaphore' cluster.
  110. ## STAMPEDE PROTECTION ##
  111. Memcache includes stampede protection for rebuilding expired cache items. To
  112. enable stampede protection, define the following in settings.php:
  113. $conf['memcache_stampede_protection'] = TRUE;
  114. To avoid lock stampedes, it is important that you enable the memcache lock
  115. implementation when enabling stampede protection -- enabling stampede protection
  116. without enabling the Memcache lock implementation can cause worse performance and
  117. can result in dropped locks due to key-length truncation.
  118. Memcache stampede protection is primarily designed to benefit the following
  119. caching pattern: a miss on a cache_get() for a specific cid is immediately
  120. followed by a cache_set() for that cid. Of course, this is not the only caching
  121. pattern used in Drupal, so stampede protection can be selectively disabled for
  122. optimal performance. For example, a cache miss in Drupal core's
  123. module_implements() won't execute a cache_set until drupal_page_footer()
  124. calls module_implements_write_cache() which can occur much later in page
  125. generation. To avoid long hanging locks, stampede protection should be
  126. disabled for these delayed caching patterns.
  127. Memcache stampede protection can be disabled for entire bins, specific cid's in
  128. specific bins, or cid's starting with a specific prefix in specific bins. For
  129. example:
  130. $conf['memcache_stampede_protection_ignore'] = array(
  131. // Ignore some cids in 'cache_bootstrap'.
  132. 'cache_bootstrap' => array(
  133. 'module_implements',
  134. 'variables',
  135. 'lookup_cache',
  136. 'schema:runtime:*',
  137. 'theme_registry:runtime:*',
  138. '_drupal_file_scan_cache',
  139. ),
  140. // Ignore all cids in the 'cache' bin starting with 'i18n:string:'
  141. 'cache' => array(
  142. 'i18n:string:*',
  143. ),
  144. // Disable stampede protection for the entire 'cache_path' and 'cache_rules'
  145. // bins.
  146. 'cache_path',
  147. 'cache_rules',
  148. );
  149. Only change the following stampede protection tunables if you're sure you know
  150. what you're doing, which requires first reading the memcache.inc code.
  151. The value passed to lock_acquire, defaults to '15':
  152. $conf['memcache_stampede_semaphore'] = 15;
  153. The value passed to lock_wait, defaults to 5:
  154. $conf['memcache_stampede_wait_time'] = 5;
  155. The maximum number of calls to lock_wait() due to stampede protection during a
  156. single request, defaults to 3:
  157. $conf['memcache_stampede_wait_limit'] = 3;
  158. When adjusting these variables, be aware that:
  159. - there is unlikely to be a good use case for setting wait_time higher
  160. than stampede_semaphore;
  161. - wait_time * wait_limit is designed to default to a number less than
  162. standard web server timeouts (i.e. 15 seconds vs. apache's default of
  163. 30 seconds).
  164. ## CACHE LIFETIME ##
  165. Memcache respects Drupal core's minimum cache lifetime configuration. This
  166. setting affects all cached items, not just pages. In some cases, it may
  167. be desirable to cache different types of items for different amounts of time.
  168. You can override the minimum cache lifetime on a per-bin basis in settings.php.
  169. For example:
  170. // Cache pages for 60 seconds.
  171. $conf['cache_lifetime_cache_page'] = 60;
  172. // Cache menus for 10 minutes.
  173. $conf['cache_lifetime_menu'] = 600;
  174. ## CACHE HEADER ##
  175. Drupal core indicates whether or not a page was served out of the cache by
  176. setting the 'X-Drupal-Cache' response header with a value of HIT or MISS. If
  177. you'd like to confirm whether pages are actually being retreived from Memcache
  178. and not another backend, you can enable the following option:
  179. $conf['memcache_pagecache_header'] = TRUE;
  180. When enabled, the Memcache module will add its own 'Drupal-PageCache-Memcache'
  181. header. When cached pages are served out of the cache the header will include an
  182. 'age=' value indicating how many seconds ago the page was stored in the cache.
  183. ## PERSISTENT CONNECTIONS ##
  184. As of 7.x-1.6, the memcache module uses peristent connections by default. If
  185. this causes you problems you can disable persistent connections by adding the
  186. following to your settings.php:
  187. $conf['memcache_persistent'] = FALSE;
  188. ## STRICT COMPATIBILITY WITH DB CACHE EXPIRATIONS ##
  189. Due to the way database caching works, the native Drupal cache will return
  190. expired cache objects which were set to expire in the future even after their
  191. expiration timestamp, because it doesn't clean up cache entries until the
  192. cache bins are garbage collected (normally during a cron.php run's general
  193. cache wipe). However, memcache can expire cached items at the specific time
  194. requested. Therefore the default behavior of the memcache module does not
  195. match the Drupal API for cache_set, which states that cache items set to
  196. expire in the future are kept at least until the given timestamp, after which
  197. they behave like CACHE_TEMPORARY (removed at the next general cache wipe).
  198. If you wish to return to the behavior described in the cache_set API, and
  199. allow expired entries to appear valid until a general cache wipe, define the
  200. following in settings.php:
  201. $conf['memcache_expire_wait_gc'] = TRUE;
  202. This setting works independently from stampede support, though it changes the
  203. time at which timestamp-cached items are considered expired, and therefore
  204. affects the time at which stampede behavior happens (if enabled).
  205. ## EXAMPLES ##
  206. Example 1:
  207. First, the most basic configuration which consists of one memcached instance
  208. running on localhost port 11211 and all caches except for cache_form being
  209. stored in memcache. We also enable stampede protection, and the memcache
  210. locking mechanism. Finally, we tell Drupal to not bootstrap the database when
  211. serving cached pages to anonymous visitors.
  212. $conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
  213. $conf['lock_inc'] = 'sites/all/modules/memcache/memcache-lock.inc';
  214. $conf['memcache_stampede_protection'] = TRUE;
  215. $conf['cache_default_class'] = 'MemCacheDrupal';
  216. // The 'cache_form' bin must be assigned to non-volatile storage.
  217. $conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
  218. // Don't bootstrap the database when serving pages from the cache.
  219. $conf['page_cache_without_database'] = TRUE;
  220. $conf['page_cache_invoke_hooks'] = FALSE;
  221. Note that no servers or bins are defined. The default server and bin
  222. configuration which is used in this case is equivalant to setting:
  223. $conf['memcache_servers'] = array('localhost:11211' => 'default');
  224. Example 2:
  225. In this example we define three memcached instances, two accessed over the
  226. network, and one on a Unix socket -- please note this is only an illustration of
  227. what is possible, and is not a recommended configuration as it's highly unlikely
  228. you'd want to configure memcache to use both sockets and network addresses like
  229. this, instead you'd consistently use one or the other.
  230. The instance on port 11211 belongs to the 'default' cluster where everything
  231. gets cached that isn't otherwise defined. (We refer to it as a "cluster", but in
  232. this example our "clusters" involve only one instance.) The instance on port
  233. 11212 belongs to the 'pages' cluster, with the 'cache_page' table mapped to
  234. it -- so the Drupal page cache is stored in this cluster. Finally, the instance
  235. listening on a socket is part of the 'blocks' cluster, with the 'cache_block'
  236. table mapped to it -- so the Drupal block cache is stored here. Note that
  237. sockets do not have ports.
  238. $conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
  239. $conf['lock_inc'] = 'sites/all/modules/memcache/memcache-lock.inc';
  240. $conf['memcache_stampede_protection'] = TRUE;
  241. $conf['cache_default_class'] = 'MemCacheDrupal';
  242. // The 'cache_form' bin must be assigned no non-volatile storage.
  243. $conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
  244. // Don't bootstrap the database when serving pages from the cache.
  245. $conf['page_cache_without_database'] = TRUE;
  246. $conf['page_cache_invoke_hooks'] = FALSE;
  247. // Important to define a default cluster in both the servers
  248. // and in the bins. This links them together.
  249. $conf['memcache_servers'] = array('10.1.1.1:11211' => 'default',
  250. '10.1.1.1:11212' => 'pages',
  251. 'unix:///path/to/socket' => 'blocks');
  252. $conf['memcache_bins'] = array('cache' => 'default',
  253. 'cache_page' => 'pages',
  254. 'cache_block' => 'blocks');
  255. Example 3:
  256. Here is an example configuration that has two clusters, 'default' and
  257. 'cluster2'. Five memcached instances running on four different servers are
  258. divided up between the two clusters. The 'cache_filter' and 'cache_menu' bins
  259. go to 'cluster2'. All other bins go to 'default'.
  260. $conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
  261. $conf['lock_inc'] = 'sites/all/modules/memcache/memcache-lock.inc';
  262. $conf['memcache_stampede_protection'] = TRUE;
  263. $conf['cache_default_class'] = 'MemCacheDrupal';
  264. // The 'cache_form' bin must be assigned no non-volatile storage.
  265. $conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
  266. // Don't bootstrap the database when serving pages from the cache.
  267. $conf['page_cache_without_database'] = TRUE;
  268. $conf['page_cache_invoke_hooks'] = FALSE;
  269. $conf['memcache_servers'] = array('10.1.1.6:11211' => 'default',
  270. '10.1.1.6:11212' => 'default',
  271. '10.1.1.7:11211' => 'default',
  272. '10.1.1.8:11211' => 'cluster2',
  273. '10.1.1.9:11211' => 'cluster2');
  274. $conf['memcache_bins'] = array('cache' => 'default',
  275. 'cache_filter' => 'cluster2',
  276. 'cache_menu' => 'cluster2');
  277. );
  278. ## PREFIXING ##
  279. If you want to have multiple Drupal installations share memcached instances,
  280. you need to include a unique prefix for each Drupal installation in the $conf
  281. array of settings.php. This can be a single string prefix, or a keyed array of
  282. bin => prefix pairs:
  283. $conf['memcache_key_prefix'] = 'something_unique';
  284. Using a per-bin prefix:
  285. $conf['memcache_key_prefix'] = array(
  286. 'default' => 'something_unique',
  287. 'cache_page' => 'something_else_unique'
  288. );
  289. In the above example, the 'something_unique' prefix will be used for all bins
  290. except for the 'cache_page' bin which will use the 'something_else_unique'
  291. prefix. Note that if using a keyed array for specifying prefix, you must specify
  292. the 'default' prefix.
  293. It is also possible to specify multiple prefixes per bin. Only the first prefix
  294. will be used when setting/getting cache items, but all prefixes will be cleared
  295. when deleting cache items. This provides support for more complicated
  296. configurations such as a live instance and an administrative instance each with
  297. their own prefixes and therefore their own unique caches. Any time a cache item
  298. is deleted on either instance, it gets flushed on both -- thus, should an admin
  299. do something that flushes the page cache, it will appropriately get flushed on
  300. both instances. (For more discussion see the issue where support was added,
  301. https://www.drupal.org/node/1084448.) This feature is enabled when you configure
  302. prefixes as arrays within arrays. For example:
  303. // Live instance.
  304. $conf['memcache_key_prefix'] = array(
  305. 'default' => array(
  306. 'live_unique', // live cache prefix
  307. 'admin_unique', // admin cache prefix
  308. ),
  309. );
  310. The above would be the configuration of your live instance. Then, on your
  311. administrative instance you would flip the keys:
  312. // Administrative instance.
  313. $conf['memcache_key_prefix'] = array(
  314. 'default' => array(
  315. 'admin_unique', // admin cache prefix
  316. 'live_unique', // live cache prefix
  317. ),
  318. );
  319. ## EXPERIMENTAL - ALTERNATIVE SERIALIZE ##
  320. This is a new experimental feature added to the memcache module in version
  321. 7.x-1.6 and should be tested carefully before utilizing in production.
  322. To optimize how data is serialized before it is written to memcache, you can
  323. enable either the igbinary or msgpack PECL extension. Both switch from using
  324. PHP's own human-readable serialized data strucutres to more compact binary
  325. formats.
  326. No specicial configuration is required. If both extensions are enabled,
  327. memcache will automatically use the igbinary extension. If only one extension
  328. is enabled, memcache will automatically use that extension.
  329. You can optionally specify which extension is used by adding one of the
  330. following to your settings.php:
  331. // Force memcache to use PHP's core serialize functions
  332. $conf['memcache_serialize'] = 'serialize';
  333. // Force memcache to use the igbinary serialize functions (if available)
  334. $conf['memcache_serialize'] = 'igbinary';
  335. // Force memcache to use the msgpack serialize functions (if available)
  336. $conf['memcache_serialize'] = 'msgpack';
  337. To review which serialize function is being used, enable the memcache_admin
  338. module and visit admin/reports/memcache.
  339. IGBINARY:
  340. The igbinary project is maintained on GitHub:
  341. - https://github.com/phadej/igbinary
  342. The official igbinary PECL extension can be found at:
  343. - https://pecl.php.net/package/igbinary
  344. Version 2.0.1 or greater is recommended.
  345. MSGPACK:
  346. The msgpack project is maintained at:
  347. - https://msgpack.org
  348. The official msgpack PECL extension can be found at:
  349. - https://pecl.php.net/package/msgpack
  350. Version 2.0.2 or greater is recommended.
  351. ## MAXIMUM LENGTHS ##
  352. If the length of your prefix + key + bin combine to be more than 250 characters,
  353. they will be automatically hashed. Memcache only supports key lengths up to 250
  354. bytes. You can optionally configure the hashing algorithm used, however sha1 was
  355. selected as the default because it performs quickly with minimal collisions.
  356. Visit http://www.php.net/manual/en/function.hash-algos.php to learn more about
  357. which hash algorithms are available.
  358. $conf['memcache_key_hash_algorithm'] = 'sha1';
  359. You can also tune the maximum key length BUT BE AWARE this doesn't affect
  360. memcached's server-side limitations -- this value is primarily exposed to allow
  361. you to further shrink the length of keys to optimize network performance.
  362. Specifying a length larger than 250 will almost certainly lead to problems
  363. unless you know what you're doing.
  364. $conf['memcache_key_max_length'] = 250;
  365. By default, the memcached server can store objects up to 1 MiB in size. It's
  366. possible to increase the memcached page size to support larger objects, but this
  367. can also lead to wasted memory. Alternatively, the Drupal memcache module splits
  368. these large objects into smaller pieces. By default, the Drupal memcache module
  369. splits objects into 1 MiB sized pieces. You can modify this with the following
  370. tunable to match any special server configuration you may have. NOTE: Increasing
  371. this value without making changes to your memcached server can result in
  372. failures to cache large items.
  373. (Note: 1 MiB = 1024 x 1024 = 1048576.)
  374. $conf['memcache_data_max_length'] = 1048576;
  375. It is generally undesirable to store excessively large objects in memcache as
  376. this can result in a performance penalty. Because of this, by default the Drupal
  377. memcache module logs any time an object is cached that has to be split into
  378. multiple pieces. If this is generating too many watchdog logs, you should first
  379. understand why these objects are so large and if anything can be done to make
  380. them smaller. If you determine that the large size is valid and is not causing
  381. you any unnecessary performance penalty, you can tune the following variable to
  382. minimize or disable this logging. Set the value to a positive integer to only
  383. log when an object is split into this many or more pieces. For example, if
  384. memcache_data_max_length is set to 1048576 and memcache_log_data_pieces is set
  385. to 5, watchdog logs will only be written when an object is split into 5 or more
  386. pieces (objects >4 MiB in size). Or, to to completely disable logging set
  387. memcache_log_data_pieces to 0 or FALSE.
  388. $conf['memcache_log_data_pieces'] = 2;
  389. ## MULTIPLE SERVERS ##
  390. To use this module with multiple memcached servers, it is important that you set
  391. the hash strategy to consistent. This is controlled in the PHP extension, not
  392. the Drupal module.
  393. If using PECL memcache:
  394. Edit /etc/php.d/memcache.ini (path may changed based on package/distribution)
  395. and set the following:
  396. memcache.hash_strategy=consistent
  397. You need to reload apache httpd after making that change.
  398. If using PECL memcached:
  399. Memcached options can be controlled in settings.php. The following setting is
  400. needed:
  401. $conf['memcache_options'] = array(
  402. Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
  403. );
  404. ## DEBUG LOGGING ##
  405. You can optionally enable debug logging by adding the following to your
  406. settings.php:
  407. $conf['memcache_debug_log'] = '/path/to/file.log';
  408. By default, only the following memcache actions are logged: 'set', 'add',
  409. 'delete', and 'flush'. If you'd like to also log 'get' and 'getMulti' actions,
  410. enble verbose logging:
  411. $conf['memcache_debug_verbose'] = TRUE;
  412. This file needs to be writable by the web server (and/or by drush) or you will
  413. see lots of watchdog errors. You are responsible for ensuring that the debug log
  414. doesn't get too large. By default, enabling debug logging will write logs
  415. looking something like:
  416. 1484719570|add|semaphore|semaphore-memcache_system_list%3Acache_bootstrap|1
  417. 1484719570|set|cache_bootstrap|cache_bootstrap-system_list|1
  418. 1484719570|delete|semaphore|semaphore-memcache_system_list%3Acache_bootstrap|1
  419. The default log format is pipe delineated, containing the following fields:
  420. timestamp|action|bin|cid|return code
  421. You can specify a custom log format by setting the memcache_debug_log_format
  422. variable. Supported variables that will be replaced in your format are:
  423. '!timestamp', '!action', '!bin', '!cid', and '!rc'.
  424. For example, the default log format (note that it includes a new line at the
  425. end) is:
  426. $conf['memcache_debug_log_format'] = "!timestamp|!action|!bin|!cid|!rc\n";
  427. You can change the timestamp format by specifying a PHP date() format string in
  428. the memcache_debug_time_format variable. PHP date() formats are documented at
  429. http://php.net/manual/en/function.date.php. By default timestamps are written as
  430. a unix timestamp. For example:
  431. $conf['memcache_debug_time_format'] = 'U';
  432. ## TROUBLESHOOTING ##
  433. PROBLEM:
  434. Error:
  435. Failed to load required file memcache/dmemcache.inc
  436. Or:
  437. cache_backends not properly configured in settings.php, failed to load
  438. required file memcache.inc
  439. SOLUTION:
  440. You need to enable memcache in settings.php. Search for "Example 1" above
  441. for a basic configuration example.
  442. PROBLEM:
  443. Error:
  444. PECL !extension version %version is unsupported. Please update to
  445. %recommended or newer.
  446. SOLUTION:
  447. Upgrade to the latest available PECL extension release. Older PECL extensions
  448. have known bugs and cause a variety of problems when using the memcache module.
  449. PROBLEM:
  450. Error:
  451. Failed to connect to memcached server instance at .
  452. SOLUTION:
  453. Verify that the memcached daemon is running at the specified IP and PORT. To
  454. debug you can try to telnet directly to the memcache server from your web
  455. servers, example:
  456. telnet localhost 11211
  457. PROBLEM:
  458. Error:
  459. Failed to store to then retrieve data from memcache.
  460. SOLUTION:
  461. Carefully review your settings.php configuration against the above
  462. documentation. This error simply does a cache_set followed by a cache_get
  463. and confirms that what is written to the cache can then be read back again.
  464. This test was added in the 7.x-1.1 release.
  465. The following code is what performs this test -- you can wrap this in a
  466. tag and execute as a script with 'drush scr' to perform further debugging.
  467. $cid = 'memcache_requirements_test';
  468. $value = 'OK';
  469. // Temporarily store a test value in memcache.
  470. cache_set($cid, $value);
  471. // Retreive the test value from memcache.
  472. $data = cache_get($cid);
  473. if (!isset($data->data) || $data->data !== $value) {
  474. echo t('Failed to store to then retrieve data from memcache.');
  475. }
  476. else {
  477. // Test a delete as well.
  478. cache_clear_all($cid, 'cache');
  479. }
  480. PROBLEM:
  481. Error:
  482. Unexpected failure when testing memcache configuration.
  483. SOLUTION:
  484. Be sure the memcache module is properly installed, and that your settings.php
  485. configuration is correct. This error means an exception was thrown when
  486. attempting to write to and then read from memcache.
  487. PROBLEM:
  488. Error:
  489. Failed to set key: Failed to set key: cache_page-......
  490. SOLUTION:
  491. Upgrade your PECL library to PECL package (2.2.1) (or higher).
  492. WARNING:
  493. Zlib compression at the php.ini level and Memcache conflict.
  494. See http://drupal.org/node/273824
  495. ## MEMCACHE ADMIN ##
  496. A module offering a UI for memcache is included. It provides aggregated and
  497. per-page statistics for memcache.
  498. ## Memcached PECL Extension Support
  499. We also support the Memcached PECL extension. This extension backends
  500. to libmemcached and allows you to use some of the newer advanced features in
  501. memcached 1.4.
  502. NOTE: It is important to realize that the memcache php.ini options do not impact
  503. the memcached extension, this new extension doesn't read in options that way.
  504. Instead, it takes options directly from Drupal. Because of this, you must
  505. configure memcached in settings.php. Please look here for possible options:
  506. http://us2.php.net/manual/en/memcached.constants.php
  507. An example configuration block is below, this block also illustrates our
  508. default options (selected through performance testing). These options will be
  509. set unless overridden in settings.php.
  510. $conf['memcache_options'] = array(
  511. Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
  512. );
  513. These are as follows:
  514. * Turn on consistent distribution, which allows you to add/remove servers
  515. easily
  516. Other options you could experiment with:
  517. + Memcached::OPT_COMPRESSION => FALSE,
  518. * This disables compression in the Memcached extension. This may save some
  519. CPU cost, but can result in significantly more data being transmitted and
  520. stored. See: https://www.drupal.org/project/memcache/issues/2958403
  521. + Memcached::OPT_BINARY_PROTOCOL => TRUE,
  522. * This enables the Memcache binary protocol (only available in Memcached
  523. 1.4 and later). Note that some users have reported SLOWER performance
  524. with this feature enabled. It should only be enabled on extremely high
  525. traffic networks where memcache network traffic is a bottleneck.
  526. Additional reading about the binary protocol:
  527. http://code.google.com/p/memcached/wiki/MemcacheBinaryProtocol
  528. + Memcached::OPT_TCP_NODELAY => TRUE,
  529. * This enables the no-delay feature for connecting sockets; it's been
  530. reported that this can speed up the Binary protocol (see above). This
  531. tells the TCP stack to send packets immediately and without waiting for
  532. a full payload, reducing per-packet network latency (disabling "Nagling").
  533. ### Authentication
  534. #### Binary Protocol SASL Authentication
  535. SASL authentication can be enabled as documented here:
  536. http://php.net/manual/en/memcached.setsaslauthdata.php
  537. https://code.google.com/p/memcached/wiki/SASLHowto
  538. SASL authentication requires a memcached server with SASL support (version 1.4.3
  539. or greater built with --enable-sasl and started with the -S flag) and the PECL
  540. memcached client version 2.0.0 or greater also built with SASL support. Once
  541. these requirements are satisfied you can then enable SASL support in the Drupal
  542. memcache module by enabling the binary protocol and setting
  543. memcache_sasl_username and memcache_sasl_password in settings.php. For example:
  544. $conf['memcache_options'] = array(
  545. Memcached::OPT_BINARY_PROTOCOL => TRUE,
  546. );
  547. $conf['memcache_sasl_username'] = 'yourSASLUsername';
  548. $conf['memcache_sasl_password'] = 'yourSASLPassword';
  549. #### ASCII Protocol Authentication
  550. If you do not want to enable the binary protocol, you can instead enable
  551. token authentication with the default ASCII protocol.
  552. ASCII protocol authentication requires Memcached version 1.5.15 or greater
  553. started with the -Y flag, and the PECL memcached client. It was originally
  554. documented in the memcached 1.5.15 release notes:
  555. https://github.com/memcached/memcached/wiki/ReleaseNotes1515
  556. While it will work with 1.5.15 or greater, it's strongly recommended you
  557. use memcached 1.6.4 or greater due to the following bug fix:
  558. https://github.com/memcached/memcached/wiki/ReleaseNotes164
  559. Additional detail about this feature can be found in the protocol documentation:
  560. https://github.com/memcached/memcached/blob/master/doc/protocol.txt
  561. All your memcached servers need to be started with the -Y option to specify
  562. a local path to an authfile which can contain up to 8 "username:pasword"
  563. pairs, any of which can be used for authentication. For example, a simple
  564. authfile may look as follows:
  565. foo:bar
  566. You can then configure your website to authenticate with this username and
  567. password as follows:
  568. $conf['memcache_ascii_auth'] = 'foo bar';
  569. Enabling ASCII protocol authentication during load testing resulted in less than
  570. 1% overhead.
  571. ## Amazon Elasticache
  572. You can use the Drupal Memcache module to talk with Amazon Elasticache, but to
  573. enable Automatic Discovery you must use Amazon's forked version of the PECL
  574. Memcached extension with Dynamic Client Mode enabled.
  575. Their PECL Memcached fork is maintained on GitHub:
  576. - https://github.com/awslabs/aws-elasticache-cluster-client-memcached-for-php
  577. If you are using PHP 7 you need to select the php7 branch of their project.
  578. Once the extension is installed, you can enable Dynamic Client Mode as follows:
  579. $conf['memcache_options'] = array(
  580. Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
  581. Memcached::OPT_CLIENT_MODE => Memcached::DYNAMIC_CLIENT_MODE,
  582. );
  583. You then configure the module normally. Amazon explains:
  584. "If you use Automatic Discovery, you can use the cluster's Configuration
  585. Endpoint to configure your Memcached client."
  586. The Configuration Endpoint must have 'cfg' in the name or it won't work. Further
  587. documentation can be found here:
  588. http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/Endpoints.html
  589. If you don't want to use Automatic Discovery you don't need to install the
  590. forked PECL extension, Amazon explains:
  591. "If you don't use Automatic Discovery, you must configure your client to use
  592. the individual node endpoints for reads and writes. You must also keep track
  593. of them as you add and remove nodes."