3 namespace Drupal\memcache\Connection;
5 use Drupal\memcache\MemcacheSettings;
8 * Class MemcachedConnection.
10 class MemcachedConnection implements MemcacheConnectionInterface {
13 * The memcache object.
20 * Constructs a MemcachedConnection object.
22 * @param \Drupal\memcache\MemcacheSettings $settings
23 * The memcache config object.
25 public function __construct(MemcacheSettings $settings) {
26 $this->memcache = new \Memcached();
29 \Memcached::OPT_COMPRESSION => TRUE,
30 \Memcached::OPT_DISTRIBUTION => \Memcached::DISTRIBUTION_CONSISTENT,
32 foreach ($default_opts as $key => $value) {
33 $this->memcache->setOption($key, $value);
35 // See README.txt for setting custom Memcache options when using the
36 // memcached PECL extension.
37 foreach ($settings->get('options', []) as $key => $value) {
38 $this->memcache->setOption($key, $value);
41 // SASL configuration to authenticate with Memcached.
42 // Note: this only affects the Memcached PECL extension.
43 if ($sasl_config = $settings->get('sasl', [])) {
44 $this->memcache->setSaslAuthData($sasl_config['username'], $sasl_config['password']);
51 public function addServer($server_path, $persistent = FALSE) {
52 list($host, $port) = explode(':', $server_path);
54 if ($host == 'unix') {
55 // Memcached expects just the path to the socket without the protocol.
56 $host = substr($server_path, 7);
57 // Port is always 0 for unix sockets.
61 return $this->memcache->addServer($host, $port, $persistent);
67 public function getMemcache() {
68 return $this->memcache;
74 public function close() {
75 $this->memcache->quit();