You are here

function memcache_enable in Memcache API and Integration 7

Same name and namespace in other branches
  1. 6 memcache.install \memcache_enable()

Implements hook_enable().

File

./memcache.install, line 17
Install, update and uninstall functions for the memcache module.

Code

function memcache_enable() {
  $error = FALSE;
  $warning = FALSE;
  $severity = 'status';
  $memcache = extension_loaded('memcache');
  $memcached = extension_loaded('memcached');
  if (!$memcache && !$memcached) {
    $error = TRUE;
  }
  if (!function_exists('dmemcache_object')) {

    // dmemcache.inc isn't loaded.
    $error = TRUE;
  }
  else {
    if (!_memcache_pecl_version_valid()) {
      $warning = TRUE;
    }

    // If ASCII protocol authentication is enabled, perform extra tests.
    if (_dmemcache_use_ascii_auth()) {

      // Verify minimum required memcached version for ASCII protocol authentication is installed.
      $version = _memcached_ascii_auth_version_valid(MEMCACHED_ASCII_AUTH_MINIMUM);

      // TRUE means version valid, FALSE means we failed to connect and will throw a different error.
      if ($version !== TRUE && $version !== FALSE) {
        $error = TRUE;
      }
      else {
        $version = _memcached_ascii_auth_version_valid(MEMCACHED_ASCII_AUTH_RECOMMENDED);

        // TRUE means version valid, FALSE means we failed to connect and will throw a different error.
        if ($version !== TRUE && $version !== FALSE) {
          $warning = TRUE;
        }
      }
    }

    // Make a test connection to all configured memcache servers.
    $memcache_servers = variable_get('memcache_servers', array(
      '127.0.0.1:11211' => 'default',
    ));
    foreach ($memcache_servers as $server => $bin) {
      if ($cluster = dmemcache_object_cluster($bin)) {
        $memcache = dmemcache_instance($cluster['cluster']);
        if (dmemcache_connect($memcache, $server, $cluster['weight']) === FALSE) {
          $error = TRUE;
        }
        else {
          if (!variable_get('memcache_persistent', TRUE)) {
            dmemcache_close($memcache);
          }
        }
      }
    }
  }
  if ($error) {
    $severity = 'error';
  }
  elseif ($warning) {
    $severity = 'warning';
  }
  if ($error || $warning) {
    drupal_set_message(t('There are problems with your Memcache configuration. Please review %readme and visit the Drupal admin !status page for more information.', array(
      '%readme' => 'README.txt',
      '!status' => l(t('status report'), 'admin/reports/status'),
    )), $severity);
  }
}