You are here

class MemcachedConnection in Memcache API and Integration 8.2

Class MemcachedConnection.

Hierarchy

Expanded class hierarchy of MemcachedConnection

1 file declares its use of MemcachedConnection
MemcacheDriverFactory.php in src/Driver/MemcacheDriverFactory.php

File

src/Connection/MemcachedConnection.php, line 10

Namespace

Drupal\memcache\Connection
View source
class MemcachedConnection implements MemcacheConnectionInterface {

  /**
   * The memcache object.
   *
   * @var \Memcached
   */
  protected $memcache;

  /**
   * Constructs a MemcachedConnection object.
   *
   * @param \Drupal\memcache\MemcacheSettings $settings
   *   The memcache config object.
   */
  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']);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function addServer($server_path, $persistent = FALSE) {
    list($host, $port) = explode(':', $server_path);
    if ($host == 'unix') {

      // Memcached expects just the path to the socket without the protocol.
      $host = substr($server_path, 7);

      // Port is always 0 for unix sockets.
      $port = 0;
    }
    return $this->memcache
      ->addServer($host, $port, $persistent);
  }

  /**
   * {@inheritdoc}
   */
  public function getMemcache() {
    return $this->memcache;
  }

  /**
   * {@inheritdoc}
   */
  public function close() {
    $this->memcache
      ->quit();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MemcachedConnection::$memcache protected property The memcache object.
MemcachedConnection::addServer public function Adds a memcache server. Overrides MemcacheConnectionInterface::addServer
MemcachedConnection::close public function Closes the memcache instance connection. Overrides MemcacheConnectionInterface::close
MemcachedConnection::getMemcache public function Returns the internal memcache object. Overrides MemcacheConnectionInterface::getMemcache
MemcachedConnection::__construct public function Constructs a MemcachedConnection object.