Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / memcache / src / Connection / MemcachedConnection.php
1 <?php
2
3 namespace Drupal\memcache\Connection;
4
5 use Drupal\memcache\MemcacheSettings;
6
7 /**
8  * Class MemcachedConnection.
9  */
10 class MemcachedConnection implements MemcacheConnectionInterface {
11
12   /**
13    * The memcache object.
14    *
15    * @var \Memcached
16    */
17   protected $memcache;
18
19   /**
20    * Constructs a MemcachedConnection object.
21    *
22    * @param \Drupal\memcache\MemcacheSettings $settings
23    *   The memcache config object.
24    */
25   public function __construct(MemcacheSettings $settings) {
26     $this->memcache = new \Memcached();
27
28     $default_opts = [
29       \Memcached::OPT_COMPRESSION => TRUE,
30       \Memcached::OPT_DISTRIBUTION => \Memcached::DISTRIBUTION_CONSISTENT,
31     ];
32     foreach ($default_opts as $key => $value) {
33       $this->memcache->setOption($key, $value);
34     }
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);
39     }
40
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']);
45     }
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function addServer($server_path, $persistent = FALSE) {
52     list($host, $port) = explode(':', $server_path);
53
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.
58       $port = 0;
59     }
60
61     return $this->memcache->addServer($host, $port, $persistent);
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function getMemcache() {
68     return $this->memcache;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function close() {
75     $this->memcache->quit();
76   }
77
78 }