/**
  * Constructs the factory object.
  *
  * @param \Drupal\Core\Site\Settings $settings
  *   An object with site settings.
  *
  * @throws \Drupal\memcache_storage\DrupalMemcachedInitializeException
  */
 public function __construct(Settings $settings)
 {
     // Validate pecl extension configuration.
     $this->extension = DrupalMemcachedUtils::getPeclExtension();
     if (!class_exists($this->extension) || !in_array($this->extension, ['Memcache', 'Memcached'])) {
         throw new DrupalMemcachedInitializeException('Could not initialize ' . $this->extension . ' PECL extension');
     }
     // Keep memcache_storage settings.
     $this->settings = $settings->get('memcache_storage', []);
     // Get configuration of cache bins per memcached clusters.
     if (!empty($this->settings['bins_clusters'])) {
         $this->bins_clusters = $this->settings['bins_clusters'];
     }
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 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;
 }