You are here

function dmemcache_connect in Memcache API and Integration 6

Same name and namespace in other branches
  1. 7 dmemcache.inc \dmemcache_connect()

Initiate a connection to memcache.

Parameters

object $memcache: A memcache instance obtained through dmemcache_instance.

string $server: A server string of the format "localhost:11211" or "unix:///path/to/socket".

bool $connection: TRUE or FALSE, whether the $memcache instance already has at least one open connection.

Return value

bool TRUE or FALSE if connection was successful.

3 calls to dmemcache_connect()
dmemcache_object in ./dmemcache.inc
Returns an Memcache object based on the bin requested. Note that there is nothing preventing developers from calling this function directly to get the Memcache object. Do this if you need functionality not provided by this API or if you need to use…
memcache_enable in ./memcache.install
Implements hook_enable().
memcache_requirements in ./memcache.install
Implements hook_requirements().

File

./dmemcache.inc, line 578

Code

function dmemcache_connect($memcache, $server, $connection) {
  static $memcache_persistent = NULL;
  $extension = dmemcache_extension();
  @(list($host, $port) = explode(':', trim($server)));
  if (empty($host)) {
    register_shutdown_function('watchdog', 'memcache', 'You have specified an invalid address of "!server" in settings.php. Please review README.txt for proper configuration.', array(
      '!server' => $server,
      '!ip' => t('127.0.0.1:11211'),
      '!host' => t('localhost:11211'),
      '!socket' => t('unix:///path/to/socket'),
    ), WATCHDOG_WARNING);
  }
  if ($extension == 'Memcache') {

    // Allow persistent connection via Memcache extension -- note that this
    // module currently doesn't support persistent connections with the
    // Memcached extension. See http://drupal.org/node/822316#comment-4427676
    // for details.
    if (!isset($memcache_persistent)) {
      $memcache_persistent = variable_get('memcache_persistent', FALSE);
    }

    // Support unix sockets of the format 'unix:///path/to/socket'.
    if ($host == 'unix') {

      // Use full protocol and path as expected by Memcache extension.
      $host = $server;
      $port = 0;
    }
    else {
      if (!isset($port)) {
        register_shutdown_function('watchdog', 'memcache', 'You have specified an invalid address of "!server" in settings.php which does not include a port. Please review README.txt for proper configuration. You must specify both a server address and port such as "!ip" or "!host", or a unix socket such as "!socket".', array(
          '!server' => $server,
          '!ip' => t('127.0.0.1:11211'),
          '!host' => t('localhost:11211'),
          '!socket' => t('unix:///path/to/socket'),
        ), WATCHDOG_WARNING);
      }
    }

    // When using the PECL memcache extension, we must use ->(p)connect
    // for the first connection.
    if (!$connection) {
      $track_errors = ini_set('track_errors', '1');
      $php_errormsg = '';

      // The Memcache extension requires us to use (p)connect for the first
      // server we connect to.
      if ($memcache_persistent) {
        $rc = @$memcache
          ->pconnect($host, $port);
      }
      else {
        $rc = @$memcache
          ->connect($host, $port);
      }
      if (!empty($php_errormsg)) {
        register_shutdown_function('watchdog', 'memcache', 'Exception caught in dmemcache_connect while connecting to !host:!port: !msg', array(
          '!host' => $host,
          '!port' => $port,
          '!msg' => $php_errormsg,
        ), WATCHDOG_WARNING);
        $php_errormsg = '';
      }
      ini_set('track_errors', $track_errors);
    }
    else {
      $rc = $memcache
        ->addServer($host, $port, $memcache_persistent);
    }
  }
  elseif ($extension == 'Memcached') {

    // Support unix sockets of the format 'unix:///path/to/socket'.
    if ($host == 'unix') {

      // Strip 'unix://' as expected by Memcached extension.
      $host = substr($server, 7);
      $port = 0;
    }
    else {
      if (!isset($port)) {
        register_shutdown_function('watchdog', 'memcache', 'You have specified an invalid address of "!server" in settings.php which does not include a port. Please review README.txt for proper configuration. You must specify both a server address and port such as "!ip" or "!host", or a unix socket such as "!socket".', array(
          '!server' => $server,
          '!ip' => t('127.0.0.1:11211'),
          '!host' => t('localhost:11211'),
          '!socket' => t('unix:///path/to/socket'),
        ), WATCHDOG_WARNING);
      }
    }
    $rc = $memcache
      ->addServer($host, $port);
  }
  else {
    $rc = FALSE;
  }
  return $rc;
}