1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BFW\Traits; |
4
|
|
|
|
5
|
|
|
use \Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait to regroup memcache(d) methods |
9
|
|
|
*/ |
10
|
|
|
trait Memcache |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Read the server information and add not existing keys |
14
|
|
|
* |
15
|
|
|
* @param array &$infos Server informations |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
* |
19
|
|
|
* @throw \Exception If informations datas is not an array |
20
|
|
|
*/ |
21
|
|
|
protected function getServerInfos(&$infos) |
22
|
|
|
{ |
23
|
|
|
if (!is_array($infos)) { |
24
|
|
|
throw new Exception( |
25
|
|
|
'Memcache(d) server information is not an array.', |
26
|
|
|
$this::ERR_SERVER_INFOS_FORMAT |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$infosKeyDefaultValues = [ |
31
|
|
|
'host' => null, |
32
|
|
|
'port' => null, |
33
|
|
|
'weight' => 0, |
34
|
|
|
'timeout' => null, |
35
|
|
|
'persistent' => false |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
foreach ($infosKeyDefaultValues as $infosKey => $defaultValue) { |
39
|
|
|
if (!isset($infos[$infosKey])) { |
40
|
|
|
$infos[$infosKey] = $defaultValue; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* addServer not created the connection. It's created at the first call |
47
|
|
|
* to the memcached servers. |
48
|
|
|
* |
49
|
|
|
* So, we run the connect to all server declared |
50
|
|
|
* |
51
|
|
|
* @throws \Exception If a server is not connected |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
protected function testConnect() |
56
|
|
|
{ |
57
|
|
|
if ($this instanceof \BFW\Memcache\Memcache) { |
58
|
|
|
//With Memcache getStats not return stats for all connected server. |
59
|
|
|
$stats = $this->getExtendedStats(); |
60
|
|
|
} else { |
61
|
|
|
$stats = $this->getStats(); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (!is_array($stats)) { |
65
|
|
|
throw new Exception( |
66
|
|
|
'No memcached server connected.', |
67
|
|
|
$this::ERR_NO_SERVER_CONNECTED |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
foreach ($stats as $serverName => $serverStat) { |
72
|
|
|
if ($serverStat['uptime'] < 1) { |
73
|
|
|
throw new Exception( |
74
|
|
|
'Memcached server '.$serverName.' not connected', |
75
|
|
|
$this::ERR_A_SERVER_IS_NOT_CONNECTED |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Check if a key exists into memcache(d) |
83
|
|
|
* /!\ Not work if the correct value is the boolean false /!\ |
84
|
|
|
* |
85
|
|
|
* @param string $key The memcache(d) key to check |
86
|
|
|
* |
87
|
|
|
* @return boolean |
88
|
|
|
* |
89
|
|
|
* @throws Exception If the key is not a string |
90
|
|
|
*/ |
91
|
|
|
public function ifExists($key) |
92
|
|
|
{ |
93
|
|
|
$verifParams = \BFW\Helpers\Datas::checkType([ |
94
|
|
|
[ |
95
|
|
|
'type' => 'string', |
96
|
|
|
'data' => $key |
97
|
|
|
] |
98
|
|
|
]); |
99
|
|
|
|
100
|
|
|
if (!$verifParams) { |
101
|
|
|
throw new Exception( |
102
|
|
|
'The $key parameters must be a string.' |
103
|
|
|
.' Currently the value is a/an '.gettype($key) |
104
|
|
|
.' and is equal to '.$key, |
105
|
|
|
$this::ERR_IFEXISTS_PARAM_TYPE |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($this->get($key) === false) { |
|
|
|
|
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Update the expire time for a memcache(d) key. |
118
|
|
|
* |
119
|
|
|
* @param string $key The memcache(d) key to update |
120
|
|
|
* @param int $expire The new expire time |
121
|
|
|
* |
122
|
|
|
* @return boolean |
123
|
|
|
* |
124
|
|
|
* @throws Exception |
125
|
|
|
*/ |
126
|
|
|
public function updateExpire($key, $expire) |
127
|
|
|
{ |
128
|
|
|
$verifParams = \BFW\Helpers\Datas::checkType([ |
129
|
|
|
['type' => 'string', 'data' => $key], |
130
|
|
|
['type' => 'int', 'data' => $expire] |
131
|
|
|
]); |
132
|
|
|
|
133
|
|
|
if (!$verifParams) { |
134
|
|
|
throw new Exception( |
135
|
|
|
'Once of parameters $key or $expire not have a correct type.', |
136
|
|
|
$this::ERR_UPDATEEXPIRE_PARAM_TYPE |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (!$this->ifExists($key)) { |
141
|
|
|
throw new Exception( |
142
|
|
|
'The key '.$key.' not exist on memcache(d) server', |
143
|
|
|
$this::ERR_KEY_NOT_EXIST |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
//To change expire time, we need to re-set the value. |
148
|
|
|
$value = $this->get($key); //Get the value |
|
|
|
|
149
|
|
|
|
150
|
|
|
//Re-set the value with new expire time. |
151
|
|
|
if (is_subclass_of($this, '\Memcache')) { |
|
|
|
|
152
|
|
|
return $this->replace($key, $value, 0, $expire); |
|
|
|
|
153
|
|
|
} elseif (is_subclass_of($this, '\Memcached')) { |
|
|
|
|
154
|
|
|
return $this->replace($key, $value, $expire); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.