4

I'm trying to setup memcache on my drupal 7 site. I have a memcache server setup and working and I've used setsebool -P httpd_can_network_memcache 1 to allow apache to communicate with it. I've also confirmed that port 11211 tcp is open in ip tables.

I've installed the memecache drupal extension, the PECL extension in PHP and when I run phpinfo(); I see that memcache is loaded.

In settings.php on my drupal web server I have the following:

/**************************************************
 * MEMCACHE
 **************************************************/
$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
$conf['cache_default_class'] = 'MemCacheDrupal';
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
$conf['memcache_servers'] = array(
        'memcacheserver.ourdomain.local:11211' => 'cluster'
);

$conf['memcache_bins'] = array(
        'bin1' => 'cluster'
);

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

However when I go to the status reports page I see the following:

Memcache integration Memcache integration is not currently loaded. Check README.txt and ensure that memcache.inc is configured correctly in settings.php

I've read through the readme.txt file repeatedly and of course you can see I have memcache.inc specified above.

I thought perhaps I had a conflict with the varnish module, or the apc module as those also set cache_backends - but its an array so I think thats ok. I also commented them out and I'm still seeing the error above.

I also confirmed that sites/all/modules/memcache/memcache.inc exists.

Why isn't drupal loading/finding memcache.inc?

Thanks Brad

2
  • I've setuop a test memcache page as follows which works: <? $memcache = new Memcache; $memcache->addServer('memcacheserver.ourdomain.local', 11211) or die ("Could not connect"); $memcache->addServer('memcacheserver.ourdomain.local', 11211) or die ("Could not connect"); $memcache->set('mytestvariable', "this is the data in my test variable", false, 60) or die ("Unable to save the data to the server"); echo "Data has been stored in the cache<br />"; $result = $memcache->get('mytestvariable'); echo "Retrieved data from the server:<br/>"; var_dump($result); ?>
    – Brad
    Apr 2, 2013 at 12:40
  • Please edit rather than comment if you have some clarifications to add.
    – Mołot
    Jan 9, 2014 at 8:30

4 Answers 4

1

You've got to add these lines to your settings.php file to run memcache proper:

$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
$conf['cache_default_class'] = 'MemCacheDrupal';
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';

If you're on top of a multisite installation also don't forget to add this one:

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

More information about configuration and setting memcache for drupal you can find here.

0

To get this to work I had to comment out the following line:

//$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';

And add:

// the path to the core cache file
include_once('./includes/cache.inc');
// the path to the memcache cache file
include_once('./sites/all/modules/memcache/memcache.inc');

I'd love to know why this is necessary and what the difference is but at the moment it seems to be working.

0

You need to flush APC cache somehow to make the command work. Restarting Apache will do just fine. No need to manually use include_once() to load the fields.

In my case, this was cased by the APC setting apc.ttl that was set to 0 to prevent the cache from expiring until Apache was restarted.

0

Please check the following example:

/**
 * Memcached Configuration
 */
if (class_exists('Memcache') || class_exists('Memcached')) {

  // Make memcache the default cache class.
  $conf['cache_backends'][] = 'sites/all/modules/contrib/memcache/memcache.inc'; // NOTE: Just verify path where you've memcache module placed.
  $conf['cache_default_class'] = 'MemCacheDrupal';
  $conf['memcache_key_prefix'] = 'drupal_';

  // Ensure that the special cache_form bin is assigned to non-volatile storage.
  $conf['cache_class_cache_form'] = 'DrupalDatabaseCache';

  // Support for Memcached PECL extension
  // This new extension backends to libmemcached
  // and allows you to use some of the newer advanced features in memcached 1.4.
  if (class_exists('Memcached')) {
    // Enable the binary protocol, which is more advanced and faster.
    $conf['memcache_options'] = array(
        Memcached::OPT_BINARY_PROTOCOL => TRUE,
    );

    // Memcached takes options directly from Drupal.
    $conf['memcache_options'] = array(
        Memcached::OPT_COMPRESSION => TRUE, // Turn off compression, as this takes more CPU cycles than its worth for most users.
        Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT, // Allows you to add/remove servers easily.
    );
  }
}

It will automatically detect which memcache extension are you using and it'll load classes only it's present. In addition it has some common useful stuff.


Troubleshooting:

  • check that your memcache module is enabled,
  • go to /admin/reports/status/php and verify that you've section called memcache or memcached (if not, you've to properly install it, check your logs),
  • when using command line, check your memcache via: php -m | grep memcache
  • check at /admin/reports/status if you still having some issues,
  • make sure that your memcached is up and running,
1
  • How can we do this setting for drupal 8 memcache Jun 23, 2020 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.