You are here

public function DrupalMemcachedBase::addServers in Memcache Storage 8

Parameters

$servers: List of services related to the current cluster.

Return value

bool Connection status.

Overrides DrupalMemcachedInterface::addServers

1 call to DrupalMemcachedBase::addServers()
DrupalMemcachedBase::__construct in src/DrupalMemcachedBase.php
Builds a new DrupalMemcache(d) object.

File

src/DrupalMemcachedBase.php, line 146

Class

DrupalMemcachedBase
Class DrupalMemcachedBase

Namespace

Drupal\memcache_storage

Code

public function addServers($servers) {

  // If server list is empty, then something is configured wrong.
  if (empty($servers)) {
    DrupalMemcachedUtils::log(LogLevel::ERROR, 'Could not find servers for the cluster %cluster.', [
      '%cluster' => $this->cluster,
    ]);
    return FALSE;
  }

  // Add each server to pecl memcache(d) object. Point your attention that
  // addServer() method first calls method from DrupalMemcache(d) class.
  foreach ($servers as $server) {
    list($host, $port) = DrupalMemcachedUtils::parseServerInfo($server);
    $this
      ->addServer($host, $port);
  }

  // Get information about connected memcached servers.
  $stats = $this
    ->getStats();
  $failed_connections = [];
  foreach ($stats as $server => $server_stats) {

    // If uptime info is empty - then this server is not available.
    if (empty($server_stats['uptime'])) {
      $failed_connections[] = $server;
      DrupalMemcachedUtils::log(LogLevel::WARNING, 'Could not connect to the server %server.', [
        '%server' => $server,
      ]);
    }
  }

  // If memcached is unable to connect to all servers it means that
  // we have no connections at all, and could not start memcached connection.
  if (sizeof($failed_connections) == sizeof($stats)) {
    DrupalMemcachedUtils::log(LogLevel::ERROR, 'Could not connect to all servers from the cluster %cluster.', [
      '%cluster' => $this->cluster,
    ]);
    return FALSE;
  }

  // Successfully connected to all servers, yay!
  return TRUE;
}