Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / memcache / src / Connection / MemcacheConnection.php
1 <?php
2
3 namespace Drupal\memcache\Connection;
4
5 /**
6  * Class MemcacheConnection.
7  */
8 class MemcacheConnection implements MemcacheConnectionInterface {
9
10   /**
11    * The memcache object.
12    *
13    * @var \Memcache
14    */
15   protected $memcache;
16
17   /**
18    * Constructs a MemcacheConnection object.
19    */
20   public function __construct() {
21     $this->memcache = new \Memcache();
22   }
23
24   /**
25    * {@inheritdoc}
26    */
27   public function addServer($server_path, $persistent = FALSE) {
28     list($host, $port) = explode(':', $server_path);
29
30     // Support unix sockets in the format 'unix:///path/to/socket'.
31     if ($host == 'unix') {
32       // When using unix sockets with Memcache use the full path for $host.
33       $host = $server_path;
34       // Port is always 0 for unix sockets.
35       $port = 0;
36     }
37
38     // When using the PECL memcache extension, we must use ->(p)connect
39     // for the first connection.
40     return $this->connect($host, $port, $persistent);
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function getMemcache() {
47     return $this->memcache;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function close() {
54     $this->memcache->close();
55   }
56
57   /**
58    * Connects to a memcache server.
59    *
60    * @param string $host
61    *   The server path without port.
62    * @param int $port
63    *   The server port.
64    * @param bool $persistent
65    *   Whether this server connection is persistent or not.
66    *
67    * @return \Memcache|bool
68    *   A Memcache object for a successful persistent connection. TRUE for a
69    *   successful non-persistent connection. FALSE when the server fails to
70    *   connect.
71    */
72   protected function connect($host, $port, $persistent) {
73     if ($persistent) {
74       return @$this->memcache->pconnect($host, $port);
75     }
76     else {
77       return @$this->memcache->connect($host, $port);
78     }
79   }
80
81 }