Last updated 6 December 2017. Created on 9 March 2015.
Edited by gapple, acbramley, Daemon_Byte, Spleshka. Log in to edit this page.

Memcache Storage module provides some additional settings for advanced users. Note that all configuration changes should be done in settings.php file.

Preferred PECL extension

Memcache Storage supports both PECL Memcache and PECL Memcached extensions. It means that you should choose one of them to use. If your server has only one of two extensions installed, then Memcache Storage will automatically pick it. But if you have installed both, then you may want to specify it:

Drupal 7:

$conf['memcache_extension'] = 'Memcached';

Drupal 8:

$settings['memcache_storage']['extension'] = 'Memcache';

Default value: 'Memcache' for D7 or 'Memcached' for D8.

Persanally I would recommend you to use PECL Memcached extension, because in most cases it is faster.

Debug mode

If you turn on the debug more then at the bottom of each page a user (if he has an appropriate permissions) will see stats about executed memcached commands.

Drupal 7:

$conf['memcache_storage_debug'] = TRUE;

Drupal 8:

$settings['memcache_storage']['debug'] = TRUE;

Default value: FALSE

Key prefix

Add a custom prefix to every memcached key. You may need this configuration if you use the same memcached instance across multiple web sites.

Drupal 7:

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

Drupal 8:

$settings['memcache_storage']['key_prefix'] = 'some_key';

Default value: ''

Be aware that after changing this configuration all cached data will be flushed.

Multiple servers support

If you do not specify any memcached servers in settings then Memcache Storage assumes that you have a memcached instance running on 127.0.0.1:11211. If this is true, and it is the only memcached instance you wantto use - then no further configuration is required.

If you have more than one memcached instance running or the host/port for your memcached instance are different from the defaults, then you need to specify available memcached servers, and in some cases - mapping betweem memcached clusters and cache bins. See the following example:

Drupal 7:

$conf['memcache_servers'] = array(
  'host1:port1' => 'default',
  'host2:port2' => 'cluster_name1',
  'host3:port3' => 'cluster_name2',
);

$conf['memcache_bins'] = array(
  'cache' => 'default', // You can omit this config, because the "default" cluster will be  automatically assigned to all cache bins which don't have specified memcached cluster.
  'cache_page' => 'cluster_name1',
  'cache_bootstrap' => 'cluster_name2',
);

Drupal 8:

$settings['memcache_storage'][servers'] = [
  'host1:port1' => 'default',
  'host2:port2' => 'cluster_name1',
  'host3:port3' => 'cluster_name2',
];

$settings['memcache_storage']['bins'] = [
  'cache' => 'default', // You can omit this config, because the "default" cluster will be used automatically for all cache bins which have no memcached cluster specified.
  'cache_render' => 'cluster_name1',
  'cache_bootstrap' => 'cluster_name2',
];

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

  • Servers are memcached instances identified by host:port.
  • 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_render, cache_menu.
  • Clusters are groups of servers that act as a memory pool.
  • Many bins can be assigned to a cluster.
  • The default cluster is 'default'. Make sure it always specified.

Here is a simple setup that has two memcached instances, both running on localhost. The 11212 instance belongs to the 'pages' cluster and the table cache_page is mapped to the 'pages' cluster. Thus everything that gets cached, with the exception of the page cache (cache_page), will be put into 'default', or the 11211 instance. The page cache will be stored in 123.1.3.4:11212.

Drupal 7:

$conf['memcache_servers'] = array(
  '127.0.0.1:11211' => 'default',
  '127.0.0.1:12211' => 'default',
  '123.1.3.4:11211' => 'pages',
);

$conf['memcache_bins'] = array(
  'cache_page' => 'pages',
);

Drupal 8:

$settings['memcache_storage']['servers'] = [
  '127.0.0.1:11211' => 'default',
  '127.0.0.1:12211' => 'default',
  '123.1.3.4:11211' => 'pages',
];

$settings['memcache_storage']['bins'] = [
  'cache_page' => 'pages',
];

Also you are able to use unix sockets as a host. See example:

Drupal 7:

$conf['memcache_servers'] = array(
  'unix:///path/to/memcached.socket1' => 'default',
  'unix:///path/to/memcached.socket2' => 'bootstrap',
  'unix:///path/to/memcached.socket3' => 'field',
  'unix:///path/to/memcached.socket4' => 'menu',
  'unix:///path/to/memcached.socket5' => 'page',
  'unix:///path/to/memcached.socket6' => 'path',
);

$conf['memcache_bins'] = array(
  'cache'            => 'default',
  'cache_bootstrap'  => 'bootstrap',
  'cache_field'      => 'field',
  'cache_menu'       => 'menu',
  'cache_page'       => 'page',
  'cache_path'       => 'path',
);

Drupal 8:

$settings['memcache_storage']['servers'] = [
  'unix:///tmp/memcached.socket' => 'default',
  'unix:///tmp/memcached2.socket' => 'another_cluster',
];

$settings['memcache_storage']['bins'] = [
  'cache_render' => 'another_cluster',
];

You will find more details here.

All cache bins which don't have specified memcached cluster will use the "default" cluster.

Wildcards invalidation (D7 only)

Memcached doesn't have embedd possibility to delete cache by wildcards. So we have to provide a workaround to solve this issue for Drupal. For wildcards invalidations cache from the memcached is phisically removed in 2 cases:

  1. When invalidated cache item was requested by cache_get() function.
  2. Automatically on bulk wildcards invalidation which executes ever hour (by default).

You may change the invalidation interval if you feel that wildcards array becomes too huge (which actually should not happen). Other example when you might need this is when you want to use wildcards invalidation for cached pages while using external page cache. In this case you need to value of this variable to 0.

$conf['memcache_storage_wildcards_flush_interval'] = 0; // Invalidate cache immediately.

Default value: 3600

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

devsen’s picture

Hi there,
we just found out that using memcache_storage for D8 and changing the settings like

$settings['memcache']['key_prefix'] or $settings['memcache']['servers'] does't take effect.

Debugging the classes we found DrupalMemcachedBase.php is looking for values in $settings['memcache_storage']...

So using

$settings['memcache_storage']['key_prefix'] = '...';
$settings['memcache_storage']['memcached_servers'] = [...]

solved the problems. Is this a bug or did we just misunderstood anything of the basic cache configuration part?

We're now setting up memcache usage in settings.php like this:

$settings['cache']['default'] = 'cache.backend.memcache_storage';
$settings['memcache_storage']['key_prefix'] = '...';
$settings['memcache_storage']['memcached_servers'] = [...]

Thanks in advance and kind regards,

Deniz

acbramley’s picture

I've updated the documentation to reflect the new keys.

bdeclerc’s picture

I'm not sure what version of https://www.drupal.org/project/memcache the previous comment was based, but on the 8.x-2.0-alpha5 release, the settings are in $settings['memcache'], not $settings['memcache_storage']
The README file states the same
http://cgit.drupalcode.org/memcache/tree/README.txt?h=8.x-2.x

Never mind - this page is about the memcache_storage module

Bart Declercq
Fedict Application Architect WCM