|
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 Memcache trait to add some common methods with Memcache class |
|
13
|
|
|
use \BFW\Traits\Memcache; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var array $config Config define into bfw config file for memcache(d) |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $config; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructor. |
|
22
|
|
|
* Call parent constructor with the persistentId if declared in config |
|
23
|
|
|
* Connect to servers. |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct() |
|
26
|
|
|
{ |
|
27
|
|
|
$app = \BFW\Application::getInstance(); |
|
28
|
|
|
$this->config = $app->getConfig()->getValue('memcached'); |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
if (!empty($this->config['persistentId'])) { |
|
31
|
|
|
parent::__construct($this->config['persistentId']); |
|
32
|
|
|
} else { |
|
33
|
|
|
parent::__construct(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$this->connectToServers(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get the list of server already connected (persistent) |
|
41
|
|
|
* Loop on server declared into the config file. |
|
42
|
|
|
* Connect to server if not already connected |
|
43
|
|
|
* |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function connectToServers() |
|
47
|
|
|
{ |
|
48
|
|
|
//Array for the list of server(s) to connect |
|
49
|
|
|
$addServers = []; |
|
50
|
|
|
|
|
51
|
|
|
//Get all server already connected (persistent) |
|
52
|
|
|
$serversList = $this->generateServerList(); |
|
53
|
|
|
|
|
54
|
|
|
//Loop on server declared into config |
|
55
|
|
|
foreach ($this->config['servers'] as $server) { |
|
56
|
|
|
$this->getServerInfos($server); |
|
57
|
|
|
|
|
58
|
|
|
$host = $server['host']; |
|
59
|
|
|
$port = $server['port']; |
|
60
|
|
|
$weight = $server['weight']; |
|
61
|
|
|
|
|
62
|
|
|
//not check if port = (int) 0; Doc said to define to 0 for socket. |
|
63
|
|
|
if (empty($host) || $port === null) { |
|
64
|
|
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
//search if the reading server is not already connected |
|
68
|
|
|
if (in_array($host.':'.$port, $serversList)) { |
|
69
|
|
|
continue; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
//If not, add the server at the list to connect |
|
73
|
|
|
$addServers[] = [$host, $port, $weight]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
//Connect to server(s) |
|
77
|
|
|
$this->addServers($addServers); |
|
78
|
|
|
|
|
79
|
|
|
//Check if connect is successfull |
|
80
|
|
|
$this->testConnect(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the list of servers where we are already connected (persistent) |
|
85
|
|
|
* |
|
86
|
|
|
* @return string[] |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function generateServerList() |
|
89
|
|
|
{ |
|
90
|
|
|
$serversList = $this->getServerList(); |
|
91
|
|
|
$servers = []; |
|
92
|
|
|
|
|
93
|
|
|
foreach ($serversList as $serverInfos) { |
|
94
|
|
|
$servers[] = $serverInfos['host'].':'.$serverInfos['port']; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $servers; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..