MemcachedConnection.php in Memcache API and Integration 8.2
File
src/Connection/MemcachedConnection.php
View source
<?php
namespace Drupal\memcache\Connection;
use Drupal\memcache\MemcacheSettings;
class MemcachedConnection implements MemcacheConnectionInterface {
protected $memcache;
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);
}
foreach ($settings
->get('options', []) as $key => $value) {
$this->memcache
->setOption($key, $value);
}
if ($sasl_config = $settings
->get('sasl', [])) {
$this->memcache
->setSaslAuthData($sasl_config['username'], $sasl_config['password']);
}
}
public function addServer($server_path, $persistent = FALSE) {
list($host, $port) = explode(':', $server_path);
if ($host == 'unix') {
$host = substr($server_path, 7);
$port = 0;
}
return $this->memcache
->addServer($host, $port, $persistent);
}
public function getMemcache() {
return $this->memcache;
}
public function close() {
$this->memcache
->quit();
}
}