You are here

function dmemcache_instance in Memcache API and Integration 7

Same name and namespace in other branches
  1. 6 dmemcache.inc \dmemcache_instance()

Return a new memcache instance.

3 calls to dmemcache_instance()
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 801
A memcache API for Drupal.

Code

function dmemcache_instance($bin = 'cache') {
  static $error = FALSE;
  $extension = dmemcache_extension();
  if ($extension == 'Memcache') {
    return new Memcache();
  }
  elseif ($extension == 'Memcached') {
    if (variable_get('memcache_persistent', TRUE)) {
      $memcache = new Memcached($bin);
    }
    else {
      $memcache = new Memcached();
    }
    $default_opts = array(
      Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
    );
    foreach ($default_opts as $key => $value) {
      $memcache
        ->setOption($key, $value);
    }

    // See README.txt for setting custom Memcache options when using the
    // memcached PECL extension.
    $memconf = variable_get('memcache_options', array());
    foreach ($memconf as $key => $value) {
      $memcache
        ->setOption($key, $value);
    }
    if (($sasl_username = variable_get('memcache_sasl_username', '')) && ($sasl_password = variable_get('memcache_sasl_password', ''))) {
      $memcache
        ->setSaslAuthData($sasl_username, $sasl_password);
    }
    return $memcache;
  }
  else {
    if (!$error) {
      register_shutdown_function('watchdog', 'memcache', 'You must enable the PHP <a href="http://php.net/manual/en/book.memcache.php">memcache</a> (recommended) or <a href="http://php.net/manual/en/book.memcached.php">memcached</a> extension to use memcache.inc.', array(), WATCHDOG_ERROR);
      $error = TRUE;
    }
  }
  return FALSE;
}