You are here

public function DrupalMemcacheFactory::get in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 modules/memcache/src/DrupalMemcacheFactory.php \Drupal\memcache\DrupalMemcacheFactory::get()

Returns a Memcache object based on settings and the bin requested.

Parameters

string $bin: The bin which is to be used.

bool $flush: Rebuild the bin/server/cache mapping.

Return value

\Drupal\memcache\DrupalMemcacheInterface A Memcache object.

File

modules/memcache/src/DrupalMemcacheFactory.php, line 76
Contains \Drupal\memcache\DrupalMemcacheFactory.

Class

DrupalMemcacheFactory
Factory class for creation of Memcache objects.

Namespace

Drupal\memcache

Code

public function get($bin = NULL, $flush = FALSE) {
  if ($flush) {
    $this
      ->memcacheFlush();
  }
  if (empty($this->memcacheCache) || empty($this->memcacheCache[$bin])) {

    // If there is no cluster for this bin in $memcache_bins, cluster is
    // 'default'.
    $cluster = empty($this->memcacheBins[$bin]) ? 'default' : $this->memcacheBins[$bin];

    // If this bin isn't in our $memcacheBins configuration array, and the
    // 'default' cluster is already initialized, map the bin to 'default'
    // because we always map the 'default' bin to the 'default' cluster.
    if (empty($this->memcacheBins[$bin]) && !empty($this->memcacheCache['default'])) {
      $this->memcacheCache[$bin] =& $this->memcacheCache['default'];
    }
    else {

      // Create a new Memcache object. Each cluster gets its own Memcache
      // object.
      // @todo Can't add a custom memcache class here yet.
      if ($this->extension == 'Memcached') {
        $memcache = new DrupalMemcached($bin, $this->settings);
      }
      elseif ($this->extension == 'Memcache') {
        $memcache = new DrupalMemcache($bin, $this->settings);
      }

      // A variable to track whether we've connected to the first server.
      $init = FALSE;

      // Link all the servers to this cluster.
      foreach ($this->memcacheServers as $s => $c) {
        if ($c == $cluster && !isset($this->failedConnectionCache[$s])) {
          if ($memcache
            ->addServer($s, $this->memcachePersistent) && !$init) {
            $init = TRUE;
          }
          if (!$init) {

            // We can't use watchdog because this happens in a bootstrap phase
            // where watchdog is non-functional. Register a shutdown handler
            // instead so it gets recorded at the end of page load.
            register_shutdown_function('memcache_log_warning', LogLevel::ERROR, 'Failed to connect to memcache server: !server', array(
              '!server' => $s,
            ));
            $this->failedConnectionCache[$s] = FALSE;
          }
        }
      }
      if ($init) {

        // Map the current bin with the new Memcache object.
        $this->memcacheCache[$bin] = $memcache;

        // Now that all the servers have been mapped to this cluster, look for
        // other bins that belong to the cluster and map them too.
        foreach ($this->memcacheBins as $b => $c) {
          if ($c == $cluster && $b != $bin) {

            // Map this bin and cluster by reference.
            $this->memcacheCache[$b] =& $this->memcacheCache[$bin];
          }
        }
      }
    }
  }
  return empty($this->memcacheCache[$bin]) ? FALSE : $this->memcacheCache[$bin];
}