|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BFW\Memcache; |
|
4
|
|
|
|
|
5
|
|
|
use \Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class to manage connection to memcache(d) server with memcached lib |
|
9
|
|
|
*/ |
|
10
|
|
|
class Memcached extends \Memcached |
|
11
|
|
|
{ |
|
12
|
|
|
//Include traits to add some methods |
|
13
|
|
|
use \BFW\Traits\Memcache; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var \BFW\Application $app Instance of BFW Application |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $app; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array $config Config define on bfw config file for memcache(d) |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $config; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Constructor. |
|
27
|
|
|
* Call parent constructor with the persistentId if declared in config |
|
28
|
|
|
* Connect to servers. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \BFW\Application $app The BFW Application instance |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(\BFW\Application $app) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->app = $app; |
|
35
|
|
|
$this->config = $this->app->getConfig('memcached'); |
|
36
|
|
|
|
|
37
|
|
|
if (!empty($this->config['persistentId'])) { |
|
38
|
|
|
parent::__construct($this->config['persistentId']); |
|
39
|
|
|
} else { |
|
40
|
|
|
parent::__construct(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this->connectToServers(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get the list of server already connected (persistent) |
|
48
|
|
|
* Loop on server declared in config. |
|
49
|
|
|
* Connect to server if not already connected |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function connectToServers() |
|
54
|
|
|
{ |
|
55
|
|
|
//Array to have the list of server to connect |
|
56
|
|
|
$addServers = []; |
|
57
|
|
|
|
|
58
|
|
|
//Get all server already connected (persistent) |
|
59
|
|
|
$serversList = $this->generateServerList(); |
|
60
|
|
|
|
|
61
|
|
|
//Loop on server declared and config and search server not connected |
|
62
|
|
|
foreach ($this->config['server'] as $server) { |
|
63
|
|
|
$this->getServerInfos($server); |
|
64
|
|
|
|
|
65
|
|
|
$host = $server['host']; |
|
66
|
|
|
$port = $server['port']; |
|
67
|
|
|
$weight = $server['weight']; |
|
68
|
|
|
|
|
69
|
|
|
//not check if port = (int) 0; Doc said to define to 0 for socket. |
|
70
|
|
|
if (empty($host) || $port === null) { |
|
71
|
|
|
continue; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
//search if the reading server is not already connected |
|
75
|
|
|
if (in_array($host.':'.$port, $serversList)) { |
|
76
|
|
|
continue; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
//If not, add the server at the list to connect |
|
80
|
|
|
$addServers[] = [$host, $port, $weight]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
//Connect server in the list |
|
84
|
|
|
$this->addServers($addServers); |
|
85
|
|
|
|
|
86
|
|
|
//Check if connect is successfull |
|
87
|
|
|
$this->testConnect(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* addServer not created the connection. It's created at the first call |
|
92
|
|
|
* to the memcached servers. |
|
93
|
|
|
* |
|
94
|
|
|
* So, we run the connect to all server declared |
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function testConnect() |
|
99
|
|
|
{ |
|
100
|
|
|
$stats = $this->getStats(); |
|
101
|
|
|
|
|
102
|
|
|
if (!is_array($stats)) { |
|
103
|
|
|
throw new Exception('No memcached server connected.'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
foreach ($stats as $serverName => $serverStat) { |
|
107
|
|
|
if ($serverStat['uptime'] < 1) { |
|
108
|
|
|
throw new Exception( |
|
109
|
|
|
'Memcached server '.$serverName.' not connected' |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get the list of servers we already connected |
|
117
|
|
|
* |
|
118
|
|
|
* @return string[] |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function generateServerList() |
|
121
|
|
|
{ |
|
122
|
|
|
$serversList = $this->getServerList(); |
|
123
|
|
|
$servers = []; |
|
124
|
|
|
|
|
125
|
|
|
foreach ($serversList as $serverInfos) { |
|
126
|
|
|
$servers[] = $serverInfos['host'].':'.$serverInfos['port']; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $servers; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|