|
1
|
|
|
<?php |
|
2
|
|
|
namespace PHPDaemon\Clients\Memcache; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* @package Network clients |
|
6
|
|
|
* @subpackage MemcacheClient |
|
7
|
|
|
* @author Vasily Zorin <[email protected]> |
|
8
|
|
|
*/ |
|
9
|
|
|
class Pool extends \PHPDaemon\Network\Client |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Gets the key |
|
14
|
|
|
* @param string $key Key |
|
15
|
|
|
* @param callable $onResponse Callback called when response received |
|
16
|
|
|
* @callback $onResponse ( ) |
|
17
|
|
|
* @return void |
|
18
|
|
|
*/ |
|
19
|
|
|
public function get($key, $onResponse) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->requestByKey($key, 'get ' . $this->config->prefix->value . $key . "\r\n", $onResponse); |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Sets the key |
|
26
|
|
|
* @param string $key Key |
|
27
|
|
|
* @param string $value Value |
|
28
|
|
|
* @param integer $exp Lifetime in seconds (0 - immortal) |
|
29
|
|
|
* @param callable $onResponse Callback called when the request complete |
|
|
|
|
|
|
30
|
|
|
* @callback $onResponse ( ) |
|
31
|
|
|
* @return void |
|
32
|
|
|
*/ |
|
33
|
|
View Code Duplication |
public function set($key, $value, $exp = 0, $onResponse = null) |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
$this->getConnectionByKey($key, function ($conn) use ($key, $value, $exp, $onResponse) { |
|
36
|
|
|
if (!$conn->connected) { |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
if ($onResponse !== null) { |
|
40
|
|
|
$conn->onResponse->push($onResponse); |
|
41
|
|
|
$conn->checkFree(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$conn->writeln( |
|
45
|
|
|
'set ' . $this->config->prefix->value . $key . ' 0 ' . $exp . ' ' |
|
|
|
|
|
|
46
|
|
|
. mb_orig_strlen($value) . ($onResponse === null ? ' noreply' : '') . "\r\n" . $value |
|
47
|
|
|
); |
|
48
|
|
|
}); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Adds the key |
|
53
|
|
|
* @param string $key Key |
|
54
|
|
|
* @param string $value Value |
|
55
|
|
|
* @param integer $exp Lifetime in seconds (0 - immortal) |
|
56
|
|
|
* @param callable $onResponse Callback called when the request complete |
|
|
|
|
|
|
57
|
|
|
* @callback $onResponse ( ) |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
public function add($key, $value, $exp = 0, $onResponse = null) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$this->getConnectionByKey($key, function ($conn) use ($key, $value, $exp, $onResponse) { |
|
63
|
|
|
if (!$conn->connected) { |
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
if ($onResponse !== null) { |
|
67
|
|
|
$conn->onResponse->push($onResponse); |
|
68
|
|
|
$conn->checkFree(); |
|
69
|
|
|
} |
|
70
|
|
|
$conn->writeln('add ' . $this->config->prefix->value . $key . ' 0 ' . $exp . ' ' . mb_orig_strlen($value) |
|
|
|
|
|
|
71
|
|
|
. ($onResponse === null ? ' noreply' : '') . "\r\n" . $value); |
|
72
|
|
|
}); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Deletes the key |
|
77
|
|
|
* @param string $key Key |
|
78
|
|
|
* @param callable $onResponse Callback called when the request complete |
|
|
|
|
|
|
79
|
|
|
* @param integer $time Time to block this key |
|
80
|
|
|
* @callback $onResponse ( ) |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
public function delete($key, $onResponse = null, $time = 0) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->getConnectionByKey($key, function ($conn) use ($key, $time, $onResponse) { |
|
86
|
|
|
if (!$conn->connected) { |
|
87
|
|
|
return; |
|
88
|
|
|
} |
|
89
|
|
|
if ($onResponse !== null) { |
|
90
|
|
|
$conn->onResponse->push($onResponse); |
|
91
|
|
|
$conn->checkFree(); |
|
92
|
|
|
} |
|
93
|
|
|
$conn->writeln('delete ' . $this->config->prefix->value . $key . ' ' . $time); |
|
|
|
|
|
|
94
|
|
|
}); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Replaces the key |
|
99
|
|
|
* @param string $key Key |
|
100
|
|
|
* @param string $value Value |
|
101
|
|
|
* @param integer $exp Lifetime in seconds (0 - immortal) |
|
102
|
|
|
* @param callable $onResponse Callback called when the request complete |
|
|
|
|
|
|
103
|
|
|
* @callback $onResponse ( ) |
|
104
|
|
|
* @return void |
|
105
|
|
|
*/ |
|
106
|
|
View Code Duplication |
public function replace($key, $value, $exp = 0, $onResponse = null) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
$this->getConnectionByKey($key, function ($conn) use ($key, $value, $exp, $onResponse) { |
|
109
|
|
|
if (!$conn->connected) { |
|
110
|
|
|
return; |
|
111
|
|
|
} |
|
112
|
|
|
if ($onResponse !== null) { |
|
113
|
|
|
$conn->onResponse->push($onResponse); |
|
114
|
|
|
$conn->checkFree(); |
|
115
|
|
|
} |
|
116
|
|
|
$conn->writeln('replace ' . $this->config->prefix->value . $key . ' 0 ' . $exp . ' ' . mb_orig_strlen($value) |
|
|
|
|
|
|
117
|
|
|
. ($onResponse === null ? ' noreply' : '') . "\r\n" . $value); |
|
118
|
|
|
}); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Appends a string to the key's value |
|
123
|
|
|
* @param string $key Key |
|
124
|
|
|
* @param string $value Value |
|
125
|
|
|
* @param integer $exp Lifetime in seconds (0 - immortal) |
|
126
|
|
|
* @param callable $onResponse Callback called when the request complete |
|
|
|
|
|
|
127
|
|
|
* @callback $onResponse ( ) |
|
128
|
|
|
* @return void |
|
129
|
|
|
*/ |
|
130
|
|
View Code Duplication |
public function append($key, $value, $exp = 0, $onResponse = null) |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
$this->getConnectionByKey($key, function ($conn) use ($key, $value, $exp, $onResponse) { |
|
133
|
|
|
if (!$conn->connected) { |
|
134
|
|
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
if ($onResponse !== null) { |
|
137
|
|
|
$conn->onResponse->push($onResponse); |
|
138
|
|
|
$conn->checkFree(); |
|
139
|
|
|
} |
|
140
|
|
|
$conn->writeln('replace ' . $this->config->prefix->value . $key . ' 0 ' . $exp . ' ' . mb_orig_strlen($value) |
|
|
|
|
|
|
141
|
|
|
. ($onResponse === null ? ' noreply' : '') . "\r\n" . $value); |
|
142
|
|
|
}); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Prepends a string to the key's value |
|
147
|
|
|
* @param string $key Key |
|
148
|
|
|
* @param string $value Value |
|
149
|
|
|
* @param integer $exp Lifetime in seconds (0 - immortal) |
|
150
|
|
|
* @param callable $onResponse Callback called when the request complete |
|
|
|
|
|
|
151
|
|
|
* @callback $onResponse ( ) |
|
152
|
|
|
* @return void |
|
153
|
|
|
*/ |
|
154
|
|
View Code Duplication |
public function prepend($key, $value, $exp = 0, $onResponse = null) |
|
|
|
|
|
|
155
|
|
|
{ |
|
156
|
|
|
$this->getConnectionByKey($key, function ($conn) use ($key, $value, $exp, $onResponse) { |
|
157
|
|
|
if (!$conn->connected) { |
|
158
|
|
|
return; |
|
159
|
|
|
} |
|
160
|
|
|
if ($onResponse !== null) { |
|
161
|
|
|
$conn->onResponse->push($onResponse); |
|
162
|
|
|
$conn->setFree(false); |
|
163
|
|
|
} |
|
164
|
|
|
$conn->writeln('prepend ' . $this->config->prefix->value . $key . ' 0 ' . $exp . ' ' . mb_orig_strlen($value) |
|
|
|
|
|
|
165
|
|
|
. ($onResponse === null ? ' noreply' : '') . "\r\n" . $value); |
|
166
|
|
|
}); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Gets a statistics |
|
171
|
|
|
* @param callable $onResponse Callback called when the request complete |
|
172
|
|
|
* @param string $server Server |
|
|
|
|
|
|
173
|
|
|
* @return void |
|
174
|
|
|
*/ |
|
175
|
|
|
public function stats($onResponse, $server = null) |
|
176
|
|
|
{ |
|
177
|
|
|
$this->requestByServer($server, 'stats' . "\r\n", $onResponse); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Setting default config options |
|
182
|
|
|
* Overriden from NetworkClient::getConfigDefaults |
|
183
|
|
|
* @return array|bool |
|
184
|
|
|
*/ |
|
185
|
|
|
protected function getConfigDefaults() |
|
186
|
|
|
{ |
|
187
|
|
|
return [ |
|
188
|
|
|
/* [string|array] Default servers */ |
|
189
|
|
|
'servers' => 'tcp://127.0.0.1', |
|
190
|
|
|
|
|
191
|
|
|
/* [integer] Default port */ |
|
192
|
|
|
'port' => 11211, |
|
193
|
|
|
|
|
194
|
|
|
/* [integer] Maximum connections per server */ |
|
195
|
|
|
'maxconnperserv' => 32, |
|
196
|
|
|
|
|
197
|
|
|
'prefix' => '', |
|
198
|
|
|
]; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.