You are here

public function MemcachedConnection::__construct in Memcache API and Integration 8.2

Constructs a MemcachedConnection object.

Parameters

\Drupal\memcache\MemcacheSettings $settings: The memcache config object.

File

src/Connection/MemcachedConnection.php, line 25

Class

MemcachedConnection
Class MemcachedConnection.

Namespace

Drupal\memcache\Connection

Code

public function __construct(MemcacheSettings $settings) {
  $this->memcache = new \Memcached();
  $default_opts = [
    \Memcached::OPT_COMPRESSION => TRUE,
    \Memcached::OPT_DISTRIBUTION => \Memcached::DISTRIBUTION_CONSISTENT,
  ];
  foreach ($default_opts as $key => $value) {
    $this->memcache
      ->setOption($key, $value);
  }

  // See README.txt for setting custom Memcache options when using the
  // memcached PECL extension.
  foreach ($settings
    ->get('options', []) as $key => $value) {
    $this->memcache
      ->setOption($key, $value);
  }

  // SASL configuration to authenticate with Memcached.
  // Note: this only affects the Memcached PECL extension.
  if ($sasl_config = $settings
    ->get('sasl', [])) {
    $this->memcache
      ->setSaslAuthData($sasl_config['username'], $sasl_config['password']);
  }
}