You are here

function dmemcache_connect in Memcache API and Integration 7

Same name and namespace in other branches
  1. 6 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".

integer $weight: Weighted probability of talking to this server.

Return value

bool TRUE or FALSE if connection was successful.

3 calls to dmemcache_connect()
dmemcache_object in ./dmemcache.inc
Return a Memcache object for the specified bin.
memcache_enable in ./memcache.install
Implements hook_enable().
memcache_requirements in ./memcache.install
Implements hook_requirements().

File

./dmemcache.inc, line 854
A memcache API for Drupal.

Code

function dmemcache_connect($memcache, $server, $weight) {
  static $memcache_persistent = NULL;
  static $memcache_ascii_auth = 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 (!isset($memcache_persistent)) {
    $memcache_persistent = variable_get('memcache_persistent', TRUE);
  }
  if (!isset($memcache_ascii_auth)) {
    $memcache_ascii_auth = _dmemcache_use_ascii_auth();
  }
  $port_error = FALSE;
  if ($extension == 'Memcache') {

    // 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)) {
        $port_error = TRUE;
      }
    }
  }
  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)) {
        $port_error = TRUE;
      }
    }
  }
  if ($port_error) {
    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);
  }
  if ($extension == 'Memcache') {
    $rc = $memcache
      ->addServer($host, $port, $memcache_persistent, $weight);
  }
  elseif ($extension == 'Memcached') {
    $match = FALSE;
    if ($memcache_persistent) {
      $servers = $memcache
        ->getServerList();
      foreach ($servers as $s) {
        if ($s['host'] == $host && $s['port'] == $port) {
          $match = TRUE;
          break;
        }
      }
    }
    if (!$match) {
      $rc = $memcache
        ->addServer($host, $port);
      if ($rc && $memcache_ascii_auth) {
        $rc = _dmemcache_ascii_authenticate_server($host, $port, $memcache);
      }
    }
    else {
      $rc = TRUE;
    }
  }
  else {
    $rc = FALSE;
  }
  return $rc;
}