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
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$infosKeyDefaultValues = [ |
30
|
|
|
'host' => null, |
31
|
|
|
'port' => null, |
32
|
|
|
'weight' => 0, |
33
|
|
|
'timeout' => null, |
34
|
|
|
'persistent' => false |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
foreach ($infosKeyDefaultValues as $infosKey => $defaultValue) { |
38
|
|
|
if (!isset($infos[$infosKey])) { |
39
|
|
|
$infos[$infosKey] = $defaultValue; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* addServer not created the connection. It's created at the first call |
46
|
|
|
* to the memcached servers. |
47
|
|
|
* |
48
|
|
|
* So, we run the connect to all server declared |
49
|
|
|
* |
50
|
|
|
* @throws \Exception If a server is not connected |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
protected function testConnect() |
55
|
|
|
{ |
56
|
|
|
if ($this instanceof \BFW\Memcache\Memcache) { |
57
|
|
|
//With Memcache getStats not return stats for all connected server. |
58
|
|
|
$stats = $this->getExtendedStats(); |
59
|
|
|
} else { |
60
|
|
|
$stats = $this->getStats(); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (!is_array($stats)) { |
64
|
|
|
throw new Exception('No memcached server connected.'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
foreach ($stats as $serverName => $serverStat) { |
68
|
|
|
if ($serverStat['uptime'] < 1) { |
69
|
|
|
throw new Exception( |
70
|
|
|
'Memcached server '.$serverName.' not connected' |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Check if a key exists into memcache(d) |
78
|
|
|
* /!\ Not work if the correct value is the boolean false /!\ |
79
|
|
|
* |
80
|
|
|
* @param string $key The memcache(d) key to check |
81
|
|
|
* |
82
|
|
|
* @return boolean |
83
|
|
|
* |
84
|
|
|
* @throws Exception If the key is not a string |
85
|
|
|
*/ |
86
|
|
|
public function ifExists($key) |
87
|
|
|
{ |
88
|
|
|
$verifParams = \BFW\Helpers\Datas::checkType([ |
89
|
|
|
[ |
90
|
|
|
'type' => 'string', |
91
|
|
|
'data' => $key |
92
|
|
|
] |
93
|
|
|
]); |
94
|
|
|
|
95
|
|
|
if (!$verifParams) { |
96
|
|
|
throw new Exception( |
97
|
|
|
'The $key parameters must be a string.' |
98
|
|
|
.' Currently the value is a/an '.gettype($key) |
99
|
|
|
.' and is equal to '.$key |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($this->get($key) === false) { |
|
|
|
|
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Update the expire time for a memcache(d) key. |
112
|
|
|
* |
113
|
|
|
* @param string $key The memcache(d) key to update |
114
|
|
|
* @param int $expire The new expire time |
115
|
|
|
* |
116
|
|
|
* @return boolean |
117
|
|
|
* |
118
|
|
|
* @throws Exception |
119
|
|
|
*/ |
120
|
|
|
public function updateExpire($key, $expire) |
121
|
|
|
{ |
122
|
|
|
$verifParams = \BFW\Helpers\Datas::checkType([ |
123
|
|
|
['type' => 'string', 'data' => $key], |
124
|
|
|
['type' => 'int', 'data' => $expire] |
125
|
|
|
]); |
126
|
|
|
|
127
|
|
|
if (!$verifParams) { |
128
|
|
|
throw new Exception( |
129
|
|
|
'Once of parameters $key or $expire not have a correct type.' |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (!$this->ifExists($key)) { |
134
|
|
|
throw new Exception( |
135
|
|
|
'The key '.$key.' not exist on memcache(d) server' |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
//To change expire time, we need to re-set the value. |
140
|
|
|
$value = $this->get($key); //Get the value |
|
|
|
|
141
|
|
|
|
142
|
|
|
//Re-set the value with new expire time. |
143
|
|
|
if (is_subclass_of($this, '\Memcache')) { |
|
|
|
|
144
|
|
|
return $this->replace($key, $value, 0, $expire); |
|
|
|
|
145
|
|
|
} elseif (is_subclass_of($this, '\Memcached')) { |
|
|
|
|
146
|
|
|
return $this->replace($key, $value, $expire); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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.