Index: trunk/phase3/includes/memcached-client.php |
— | — | @@ -98,985 +98,985 @@ |
99 | 99 | // }}} |
100 | 100 | |
101 | 101 | |
102 | | - /** |
103 | | - * Command statistics |
104 | | - * |
105 | | - * @var array |
106 | | - * @access public |
107 | | - */ |
108 | | - var $stats; |
| 102 | + /** |
| 103 | + * Command statistics |
| 104 | + * |
| 105 | + * @var array |
| 106 | + * @access public |
| 107 | + */ |
| 108 | + var $stats; |
109 | 109 | |
110 | | - // }}} |
111 | | - // {{{ private |
| 110 | + // }}} |
| 111 | + // {{{ private |
112 | 112 | |
113 | | - /** |
114 | | - * Cached Sockets that are connected |
115 | | - * |
116 | | - * @var array |
117 | | - * @access private |
118 | | - */ |
119 | | - var $_cache_sock; |
| 113 | + /** |
| 114 | + * Cached Sockets that are connected |
| 115 | + * |
| 116 | + * @var array |
| 117 | + * @access private |
| 118 | + */ |
| 119 | + var $_cache_sock; |
120 | 120 | |
121 | | - /** |
122 | | - * Current debug status; 0 - none to 9 - profiling |
123 | | - * |
124 | | - * @var boolean |
125 | | - * @access private |
126 | | - */ |
127 | | - var $_debug; |
| 121 | + /** |
| 122 | + * Current debug status; 0 - none to 9 - profiling |
| 123 | + * |
| 124 | + * @var boolean |
| 125 | + * @access private |
| 126 | + */ |
| 127 | + var $_debug; |
128 | 128 | |
129 | | - /** |
130 | | - * Dead hosts, assoc array, 'host'=>'unixtime when ok to check again' |
131 | | - * |
132 | | - * @var array |
133 | | - * @access private |
134 | | - */ |
135 | | - var $_host_dead; |
| 129 | + /** |
| 130 | + * Dead hosts, assoc array, 'host'=>'unixtime when ok to check again' |
| 131 | + * |
| 132 | + * @var array |
| 133 | + * @access private |
| 134 | + */ |
| 135 | + var $_host_dead; |
136 | 136 | |
137 | | - /** |
138 | | - * Is compression available? |
139 | | - * |
140 | | - * @var boolean |
141 | | - * @access private |
142 | | - */ |
143 | | - var $_have_zlib; |
| 137 | + /** |
| 138 | + * Is compression available? |
| 139 | + * |
| 140 | + * @var boolean |
| 141 | + * @access private |
| 142 | + */ |
| 143 | + var $_have_zlib; |
144 | 144 | |
145 | | - /** |
146 | | - * Do we want to use compression? |
147 | | - * |
148 | | - * @var boolean |
149 | | - * @access private |
150 | | - */ |
151 | | - var $_compress_enable; |
| 145 | + /** |
| 146 | + * Do we want to use compression? |
| 147 | + * |
| 148 | + * @var boolean |
| 149 | + * @access private |
| 150 | + */ |
| 151 | + var $_compress_enable; |
152 | 152 | |
153 | | - /** |
154 | | - * At how many bytes should we compress? |
155 | | - * |
156 | | - * @var integer |
157 | | - * @access private |
158 | | - */ |
159 | | - var $_compress_threshold; |
| 153 | + /** |
| 154 | + * At how many bytes should we compress? |
| 155 | + * |
| 156 | + * @var integer |
| 157 | + * @access private |
| 158 | + */ |
| 159 | + var $_compress_threshold; |
160 | 160 | |
161 | | - /** |
162 | | - * Are we using persistant links? |
163 | | - * |
164 | | - * @var boolean |
165 | | - * @access private |
166 | | - */ |
167 | | - var $_persistant; |
| 161 | + /** |
| 162 | + * Are we using persistant links? |
| 163 | + * |
| 164 | + * @var boolean |
| 165 | + * @access private |
| 166 | + */ |
| 167 | + var $_persistant; |
168 | 168 | |
169 | | - /** |
170 | | - * If only using one server; contains ip:port to connect to |
171 | | - * |
172 | | - * @var string |
173 | | - * @access private |
174 | | - */ |
175 | | - var $_single_sock; |
| 169 | + /** |
| 170 | + * If only using one server; contains ip:port to connect to |
| 171 | + * |
| 172 | + * @var string |
| 173 | + * @access private |
| 174 | + */ |
| 175 | + var $_single_sock; |
176 | 176 | |
177 | | - /** |
178 | | - * Array containing ip:port or array(ip:port, weight) |
179 | | - * |
180 | | - * @var array |
181 | | - * @access private |
182 | | - */ |
183 | | - var $_servers; |
| 177 | + /** |
| 178 | + * Array containing ip:port or array(ip:port, weight) |
| 179 | + * |
| 180 | + * @var array |
| 181 | + * @access private |
| 182 | + */ |
| 183 | + var $_servers; |
184 | 184 | |
185 | | - /** |
186 | | - * Our bit buckets |
187 | | - * |
188 | | - * @var array |
189 | | - * @access private |
190 | | - */ |
191 | | - var $_buckets; |
| 185 | + /** |
| 186 | + * Our bit buckets |
| 187 | + * |
| 188 | + * @var array |
| 189 | + * @access private |
| 190 | + */ |
| 191 | + var $_buckets; |
192 | 192 | |
193 | | - /** |
194 | | - * Total # of bit buckets we have |
195 | | - * |
196 | | - * @var integer |
197 | | - * @access private |
198 | | - */ |
199 | | - var $_bucketcount; |
| 193 | + /** |
| 194 | + * Total # of bit buckets we have |
| 195 | + * |
| 196 | + * @var integer |
| 197 | + * @access private |
| 198 | + */ |
| 199 | + var $_bucketcount; |
200 | 200 | |
201 | | - /** |
202 | | - * # of total servers we have |
203 | | - * |
204 | | - * @var integer |
205 | | - * @access private |
206 | | - */ |
207 | | - var $_active; |
| 201 | + /** |
| 202 | + * # of total servers we have |
| 203 | + * |
| 204 | + * @var integer |
| 205 | + * @access private |
| 206 | + */ |
| 207 | + var $_active; |
208 | 208 | |
209 | | - /** |
210 | | - * Stream timeout in seconds. Applies for example to fread() |
211 | | - * |
212 | | - * @var integer |
213 | | - * @access private |
214 | | - */ |
215 | | - var $_timeout_seconds; |
| 209 | + /** |
| 210 | + * Stream timeout in seconds. Applies for example to fread() |
| 211 | + * |
| 212 | + * @var integer |
| 213 | + * @access private |
| 214 | + */ |
| 215 | + var $_timeout_seconds; |
216 | 216 | |
217 | | - /** |
218 | | - * Stream timeout in microseconds |
219 | | - * |
220 | | - * @var integer |
221 | | - * @access private |
222 | | - */ |
223 | | - var $_timeout_microseconds; |
| 217 | + /** |
| 218 | + * Stream timeout in microseconds |
| 219 | + * |
| 220 | + * @var integer |
| 221 | + * @access private |
| 222 | + */ |
| 223 | + var $_timeout_microseconds; |
224 | 224 | |
225 | | - /** |
226 | | - * Connect timeout in seconds |
227 | | - */ |
228 | | - var $_connect_timeout; |
| 225 | + /** |
| 226 | + * Connect timeout in seconds |
| 227 | + */ |
| 228 | + var $_connect_timeout; |
229 | 229 | |
230 | | - /** |
231 | | - * Number of connection attempts for each server |
232 | | - */ |
233 | | - var $_connect_attempts; |
| 230 | + /** |
| 231 | + * Number of connection attempts for each server |
| 232 | + */ |
| 233 | + var $_connect_attempts; |
234 | 234 | |
235 | | - // }}} |
236 | | - // }}} |
237 | | - // {{{ methods |
238 | | - // {{{ public functions |
239 | | - // {{{ memcached() |
| 235 | + // }}} |
| 236 | + // }}} |
| 237 | + // {{{ methods |
| 238 | + // {{{ public functions |
| 239 | + // {{{ memcached() |
240 | 240 | |
241 | | - /** |
242 | | - * Memcache initializer |
243 | | - * |
244 | | - * @param array $args Associative array of settings |
245 | | - * |
246 | | - * @return mixed |
247 | | - * @access public |
248 | | - */ |
249 | | - function memcached ($args) |
250 | | - { |
251 | | - $this->set_servers(@$args['servers']); |
252 | | - $this->_debug = @$args['debug']; |
253 | | - $this->stats = array(); |
254 | | - $this->_compress_threshold = @$args['compress_threshold']; |
255 | | - $this->_persistant = array_key_exists('persistant', $args) ? (@$args['persistant']) : false; |
256 | | - $this->_compress_enable = true; |
257 | | - $this->_have_zlib = function_exists("gzcompress"); |
| 241 | + /** |
| 242 | + * Memcache initializer |
| 243 | + * |
| 244 | + * @param array $args Associative array of settings |
| 245 | + * |
| 246 | + * @return mixed |
| 247 | + * @access public |
| 248 | + */ |
| 249 | + function memcached ($args) |
| 250 | + { |
| 251 | + $this->set_servers(@$args['servers']); |
| 252 | + $this->_debug = @$args['debug']; |
| 253 | + $this->stats = array(); |
| 254 | + $this->_compress_threshold = @$args['compress_threshold']; |
| 255 | + $this->_persistant = array_key_exists('persistant', $args) ? (@$args['persistant']) : false; |
| 256 | + $this->_compress_enable = true; |
| 257 | + $this->_have_zlib = function_exists("gzcompress"); |
258 | 258 | |
259 | | - $this->_cache_sock = array(); |
260 | | - $this->_host_dead = array(); |
| 259 | + $this->_cache_sock = array(); |
| 260 | + $this->_host_dead = array(); |
261 | 261 | |
262 | | - $this->_timeout_seconds = 0; |
263 | | - $this->_timeout_microseconds = 50000; |
| 262 | + $this->_timeout_seconds = 0; |
| 263 | + $this->_timeout_microseconds = 50000; |
264 | 264 | |
265 | | - $this->_connect_timeout = 0.01; |
266 | | - $this->_connect_attempts = 2; |
267 | | - } |
| 265 | + $this->_connect_timeout = 0.01; |
| 266 | + $this->_connect_attempts = 2; |
| 267 | + } |
268 | 268 | |
269 | | - // }}} |
270 | | - // {{{ add() |
| 269 | + // }}} |
| 270 | + // {{{ add() |
271 | 271 | |
272 | | - /** |
273 | | - * Adds a key/value to the memcache server if one isn't already set with |
274 | | - * that key |
275 | | - * |
276 | | - * @param string $key Key to set with data |
277 | | - * @param mixed $val Value to store |
278 | | - * @param integer $exp (optional) Time to expire data at |
279 | | - * |
280 | | - * @return boolean |
281 | | - * @access public |
282 | | - */ |
283 | | - function add ($key, $val, $exp = 0) |
284 | | - { |
285 | | - return $this->_set('add', $key, $val, $exp); |
286 | | - } |
| 272 | + /** |
| 273 | + * Adds a key/value to the memcache server if one isn't already set with |
| 274 | + * that key |
| 275 | + * |
| 276 | + * @param string $key Key to set with data |
| 277 | + * @param mixed $val Value to store |
| 278 | + * @param integer $exp (optional) Time to expire data at |
| 279 | + * |
| 280 | + * @return boolean |
| 281 | + * @access public |
| 282 | + */ |
| 283 | + function add ($key, $val, $exp = 0) |
| 284 | + { |
| 285 | + return $this->_set('add', $key, $val, $exp); |
| 286 | + } |
287 | 287 | |
288 | | - // }}} |
289 | | - // {{{ decr() |
| 288 | + // }}} |
| 289 | + // {{{ decr() |
290 | 290 | |
291 | | - /** |
292 | | - * Decriment a value stored on the memcache server |
293 | | - * |
294 | | - * @param string $key Key to decriment |
295 | | - * @param integer $amt (optional) Amount to decriment |
296 | | - * |
297 | | - * @return mixed FALSE on failure, value on success |
298 | | - * @access public |
299 | | - */ |
300 | | - function decr ($key, $amt=1) |
301 | | - { |
302 | | - return $this->_incrdecr('decr', $key, $amt); |
303 | | - } |
| 291 | + /** |
| 292 | + * Decriment a value stored on the memcache server |
| 293 | + * |
| 294 | + * @param string $key Key to decriment |
| 295 | + * @param integer $amt (optional) Amount to decriment |
| 296 | + * |
| 297 | + * @return mixed FALSE on failure, value on success |
| 298 | + * @access public |
| 299 | + */ |
| 300 | + function decr ($key, $amt=1) |
| 301 | + { |
| 302 | + return $this->_incrdecr('decr', $key, $amt); |
| 303 | + } |
304 | 304 | |
305 | | - // }}} |
306 | | - // {{{ delete() |
| 305 | + // }}} |
| 306 | + // {{{ delete() |
307 | 307 | |
308 | | - /** |
309 | | - * Deletes a key from the server, optionally after $time |
310 | | - * |
311 | | - * @param string $key Key to delete |
312 | | - * @param integer $time (optional) How long to wait before deleting |
313 | | - * |
314 | | - * @return boolean TRUE on success, FALSE on failure |
315 | | - * @access public |
316 | | - */ |
317 | | - function delete ($key, $time = 0) |
318 | | - { |
319 | | - if (!$this->_active) |
320 | | - return false; |
| 308 | + /** |
| 309 | + * Deletes a key from the server, optionally after $time |
| 310 | + * |
| 311 | + * @param string $key Key to delete |
| 312 | + * @param integer $time (optional) How long to wait before deleting |
| 313 | + * |
| 314 | + * @return boolean TRUE on success, FALSE on failure |
| 315 | + * @access public |
| 316 | + */ |
| 317 | + function delete ($key, $time = 0) |
| 318 | + { |
| 319 | + if (!$this->_active) |
| 320 | + return false; |
321 | 321 | |
322 | | - $sock = $this->get_sock($key); |
323 | | - if (!is_resource($sock)) |
324 | | - return false; |
| 322 | + $sock = $this->get_sock($key); |
| 323 | + if (!is_resource($sock)) |
| 324 | + return false; |
325 | 325 | |
326 | | - $key = is_array($key) ? $key[1] : $key; |
| 326 | + $key = is_array($key) ? $key[1] : $key; |
327 | 327 | |
328 | | - @$this->stats['delete']++; |
329 | | - $cmd = "delete $key $time\r\n"; |
330 | | - if(!$this->_safe_fwrite($sock, $cmd, strlen($cmd))) |
331 | | - { |
332 | | - $this->_dead_sock($sock); |
333 | | - return false; |
334 | | - } |
335 | | - $res = trim(fgets($sock)); |
| 328 | + @$this->stats['delete']++; |
| 329 | + $cmd = "delete $key $time\r\n"; |
| 330 | + if(!$this->_safe_fwrite($sock, $cmd, strlen($cmd))) |
| 331 | + { |
| 332 | + $this->_dead_sock($sock); |
| 333 | + return false; |
| 334 | + } |
| 335 | + $res = trim(fgets($sock)); |
336 | 336 | |
337 | | - if ($this->_debug) |
338 | | - $this->_debugprint(sprintf("MemCache: delete %s (%s)\n", $key, $res)); |
| 337 | + if ($this->_debug) |
| 338 | + $this->_debugprint(sprintf("MemCache: delete %s (%s)\n", $key, $res)); |
339 | 339 | |
340 | | - if ($res == "DELETED") |
341 | | - return true; |
342 | | - return false; |
343 | | - } |
| 340 | + if ($res == "DELETED") |
| 341 | + return true; |
| 342 | + return false; |
| 343 | + } |
344 | 344 | |
345 | | - // }}} |
346 | | - // {{{ disconnect_all() |
| 345 | + // }}} |
| 346 | + // {{{ disconnect_all() |
347 | 347 | |
348 | | - /** |
349 | | - * Disconnects all connected sockets |
350 | | - * |
351 | | - * @access public |
352 | | - */ |
353 | | - function disconnect_all () |
354 | | - { |
355 | | - foreach ($this->_cache_sock as $sock) |
356 | | - fclose($sock); |
| 348 | + /** |
| 349 | + * Disconnects all connected sockets |
| 350 | + * |
| 351 | + * @access public |
| 352 | + */ |
| 353 | + function disconnect_all () |
| 354 | + { |
| 355 | + foreach ($this->_cache_sock as $sock) |
| 356 | + fclose($sock); |
357 | 357 | |
358 | | - $this->_cache_sock = array(); |
359 | | - } |
| 358 | + $this->_cache_sock = array(); |
| 359 | + } |
360 | 360 | |
361 | | - // }}} |
362 | | - // {{{ enable_compress() |
| 361 | + // }}} |
| 362 | + // {{{ enable_compress() |
363 | 363 | |
364 | | - /** |
365 | | - * Enable / Disable compression |
366 | | - * |
367 | | - * @param boolean $enable TRUE to enable, FALSE to disable |
368 | | - * |
369 | | - * @access public |
370 | | - */ |
371 | | - function enable_compress ($enable) |
372 | | - { |
373 | | - $this->_compress_enable = $enable; |
374 | | - } |
| 364 | + /** |
| 365 | + * Enable / Disable compression |
| 366 | + * |
| 367 | + * @param boolean $enable TRUE to enable, FALSE to disable |
| 368 | + * |
| 369 | + * @access public |
| 370 | + */ |
| 371 | + function enable_compress ($enable) |
| 372 | + { |
| 373 | + $this->_compress_enable = $enable; |
| 374 | + } |
375 | 375 | |
376 | | - // }}} |
377 | | - // {{{ forget_dead_hosts() |
| 376 | + // }}} |
| 377 | + // {{{ forget_dead_hosts() |
378 | 378 | |
379 | | - /** |
380 | | - * Forget about all of the dead hosts |
381 | | - * |
382 | | - * @access public |
383 | | - */ |
384 | | - function forget_dead_hosts () |
385 | | - { |
386 | | - $this->_host_dead = array(); |
387 | | - } |
| 379 | + /** |
| 380 | + * Forget about all of the dead hosts |
| 381 | + * |
| 382 | + * @access public |
| 383 | + */ |
| 384 | + function forget_dead_hosts () |
| 385 | + { |
| 386 | + $this->_host_dead = array(); |
| 387 | + } |
388 | 388 | |
389 | | - // }}} |
390 | | - // {{{ get() |
| 389 | + // }}} |
| 390 | + // {{{ get() |
391 | 391 | |
392 | | - /** |
393 | | - * Retrieves the value associated with the key from the memcache server |
394 | | - * |
395 | | - * @param string $key Key to retrieve |
396 | | - * |
397 | | - * @return mixed |
398 | | - * @access public |
399 | | - */ |
400 | | - function get ($key) |
401 | | - { |
402 | | - $fname = 'memcached::get'; |
403 | | - wfProfileIn( $fname ); |
| 392 | + /** |
| 393 | + * Retrieves the value associated with the key from the memcache server |
| 394 | + * |
| 395 | + * @param string $key Key to retrieve |
| 396 | + * |
| 397 | + * @return mixed |
| 398 | + * @access public |
| 399 | + */ |
| 400 | + function get ($key) |
| 401 | + { |
| 402 | + $fname = 'memcached::get'; |
| 403 | + wfProfileIn( $fname ); |
404 | 404 | |
405 | | - if ( $this->_debug ) { |
406 | | - $this->_debugprint( "get($key)\n" ); |
407 | | - } |
| 405 | + if ( $this->_debug ) { |
| 406 | + $this->_debugprint( "get($key)\n" ); |
| 407 | + } |
408 | 408 | |
409 | | - if (!$this->_active) { |
410 | | - wfProfileOut( $fname ); |
411 | | - return false; |
412 | | - } |
| 409 | + if (!$this->_active) { |
| 410 | + wfProfileOut( $fname ); |
| 411 | + return false; |
| 412 | + } |
413 | 413 | |
414 | | - $sock = $this->get_sock($key); |
| 414 | + $sock = $this->get_sock($key); |
415 | 415 | |
416 | | - if (!is_resource($sock)) { |
417 | | - wfProfileOut( $fname ); |
418 | | - return false; |
419 | | - } |
| 416 | + if (!is_resource($sock)) { |
| 417 | + wfProfileOut( $fname ); |
| 418 | + return false; |
| 419 | + } |
420 | 420 | |
421 | | - @$this->stats['get']++; |
| 421 | + @$this->stats['get']++; |
422 | 422 | |
423 | | - $cmd = "get $key\r\n"; |
424 | | - if (!$this->_safe_fwrite($sock, $cmd, strlen($cmd))) |
425 | | - { |
426 | | - $this->_dead_sock($sock); |
427 | | - wfProfileOut( $fname ); |
428 | | - return false; |
429 | | - } |
| 423 | + $cmd = "get $key\r\n"; |
| 424 | + if (!$this->_safe_fwrite($sock, $cmd, strlen($cmd))) |
| 425 | + { |
| 426 | + $this->_dead_sock($sock); |
| 427 | + wfProfileOut( $fname ); |
| 428 | + return false; |
| 429 | + } |
430 | 430 | |
431 | | - $val = array(); |
432 | | - $this->_load_items($sock, $val); |
| 431 | + $val = array(); |
| 432 | + $this->_load_items($sock, $val); |
433 | 433 | |
434 | | - if ($this->_debug) |
435 | | - foreach ($val as $k => $v) |
436 | | - $this->_debugprint(sprintf("MemCache: sock %s got %s\n", serialize($sock), $k)); |
| 434 | + if ($this->_debug) |
| 435 | + foreach ($val as $k => $v) |
| 436 | + $this->_debugprint(sprintf("MemCache: sock %s got %s\n", serialize($sock), $k)); |
437 | 437 | |
438 | | - wfProfileOut( $fname ); |
439 | | - return @$val[$key]; |
440 | | - } |
| 438 | + wfProfileOut( $fname ); |
| 439 | + return @$val[$key]; |
| 440 | + } |
441 | 441 | |
442 | | - // }}} |
443 | | - // {{{ get_multi() |
| 442 | + // }}} |
| 443 | + // {{{ get_multi() |
444 | 444 | |
445 | | - /** |
446 | | - * Get multiple keys from the server(s) |
447 | | - * |
448 | | - * @param array $keys Keys to retrieve |
449 | | - * |
450 | | - * @return array |
451 | | - * @access public |
452 | | - */ |
453 | | - function get_multi ($keys) |
454 | | - { |
455 | | - if (!$this->_active) |
456 | | - return false; |
| 445 | + /** |
| 446 | + * Get multiple keys from the server(s) |
| 447 | + * |
| 448 | + * @param array $keys Keys to retrieve |
| 449 | + * |
| 450 | + * @return array |
| 451 | + * @access public |
| 452 | + */ |
| 453 | + function get_multi ($keys) |
| 454 | + { |
| 455 | + if (!$this->_active) |
| 456 | + return false; |
457 | 457 | |
458 | | - @$this->stats['get_multi']++; |
459 | | - $sock_keys = array(); |
| 458 | + @$this->stats['get_multi']++; |
| 459 | + $sock_keys = array(); |
460 | 460 | |
461 | | - foreach ($keys as $key) |
462 | | - { |
463 | | - $sock = $this->get_sock($key); |
464 | | - if (!is_resource($sock)) continue; |
465 | | - $key = is_array($key) ? $key[1] : $key; |
466 | | - if (!isset($sock_keys[$sock])) |
467 | | - { |
468 | | - $sock_keys[$sock] = array(); |
469 | | - $socks[] = $sock; |
470 | | - } |
471 | | - $sock_keys[$sock][] = $key; |
472 | | - } |
| 461 | + foreach ($keys as $key) |
| 462 | + { |
| 463 | + $sock = $this->get_sock($key); |
| 464 | + if (!is_resource($sock)) continue; |
| 465 | + $key = is_array($key) ? $key[1] : $key; |
| 466 | + if (!isset($sock_keys[$sock])) |
| 467 | + { |
| 468 | + $sock_keys[$sock] = array(); |
| 469 | + $socks[] = $sock; |
| 470 | + } |
| 471 | + $sock_keys[$sock][] = $key; |
| 472 | + } |
473 | 473 | |
474 | | - // Send out the requests |
475 | | - foreach ($socks as $sock) |
476 | | - { |
477 | | - $cmd = "get"; |
478 | | - foreach ($sock_keys[$sock] as $key) |
479 | | - { |
480 | | - $cmd .= " ". $key; |
481 | | - } |
482 | | - $cmd .= "\r\n"; |
| 474 | + // Send out the requests |
| 475 | + foreach ($socks as $sock) |
| 476 | + { |
| 477 | + $cmd = "get"; |
| 478 | + foreach ($sock_keys[$sock] as $key) |
| 479 | + { |
| 480 | + $cmd .= " ". $key; |
| 481 | + } |
| 482 | + $cmd .= "\r\n"; |
483 | 483 | |
484 | | - if ($this->_safe_fwrite($sock, $cmd, strlen($cmd))) |
485 | | - { |
486 | | - $gather[] = $sock; |
487 | | - } else |
488 | | - { |
489 | | - $this->_dead_sock($sock); |
490 | | - } |
491 | | - } |
| 484 | + if ($this->_safe_fwrite($sock, $cmd, strlen($cmd))) |
| 485 | + { |
| 486 | + $gather[] = $sock; |
| 487 | + } else |
| 488 | + { |
| 489 | + $this->_dead_sock($sock); |
| 490 | + } |
| 491 | + } |
492 | 492 | |
493 | | - // Parse responses |
494 | | - $val = array(); |
495 | | - foreach ($gather as $sock) |
496 | | - { |
497 | | - $this->_load_items($sock, $val); |
498 | | - } |
| 493 | + // Parse responses |
| 494 | + $val = array(); |
| 495 | + foreach ($gather as $sock) |
| 496 | + { |
| 497 | + $this->_load_items($sock, $val); |
| 498 | + } |
499 | 499 | |
500 | | - if ($this->_debug) |
501 | | - foreach ($val as $k => $v) |
502 | | - $this->_debugprint(sprintf("MemCache: got %s\n", $k)); |
| 500 | + if ($this->_debug) |
| 501 | + foreach ($val as $k => $v) |
| 502 | + $this->_debugprint(sprintf("MemCache: got %s\n", $k)); |
503 | 503 | |
504 | | - return $val; |
505 | | - } |
| 504 | + return $val; |
| 505 | + } |
506 | 506 | |
507 | | - // }}} |
508 | | - // {{{ incr() |
| 507 | + // }}} |
| 508 | + // {{{ incr() |
509 | 509 | |
510 | | - /** |
511 | | - * Increments $key (optionally) by $amt |
512 | | - * |
513 | | - * @param string $key Key to increment |
514 | | - * @param integer $amt (optional) amount to increment |
515 | | - * |
516 | | - * @return integer New key value? |
517 | | - * @access public |
518 | | - */ |
519 | | - function incr ($key, $amt=1) |
520 | | - { |
521 | | - return $this->_incrdecr('incr', $key, $amt); |
522 | | - } |
| 510 | + /** |
| 511 | + * Increments $key (optionally) by $amt |
| 512 | + * |
| 513 | + * @param string $key Key to increment |
| 514 | + * @param integer $amt (optional) amount to increment |
| 515 | + * |
| 516 | + * @return integer New key value? |
| 517 | + * @access public |
| 518 | + */ |
| 519 | + function incr ($key, $amt=1) |
| 520 | + { |
| 521 | + return $this->_incrdecr('incr', $key, $amt); |
| 522 | + } |
523 | 523 | |
524 | | - // }}} |
525 | | - // {{{ replace() |
| 524 | + // }}} |
| 525 | + // {{{ replace() |
526 | 526 | |
527 | | - /** |
528 | | - * Overwrites an existing value for key; only works if key is already set |
529 | | - * |
530 | | - * @param string $key Key to set value as |
531 | | - * @param mixed $value Value to store |
532 | | - * @param integer $exp (optional) Experiation time |
533 | | - * |
534 | | - * @return boolean |
535 | | - * @access public |
536 | | - */ |
| 527 | + /** |
| 528 | + * Overwrites an existing value for key; only works if key is already set |
| 529 | + * |
| 530 | + * @param string $key Key to set value as |
| 531 | + * @param mixed $value Value to store |
| 532 | + * @param integer $exp (optional) Experiation time |
| 533 | + * |
| 534 | + * @return boolean |
| 535 | + * @access public |
| 536 | + */ |
537 | 537 | function replace ($key, $value, $exp=0) |
538 | | - { |
539 | | - return $this->_set('replace', $key, $value, $exp); |
540 | | - } |
| 538 | + { |
| 539 | + return $this->_set('replace', $key, $value, $exp); |
| 540 | + } |
541 | 541 | |
542 | | - // }}} |
543 | | - // {{{ run_command() |
| 542 | + // }}} |
| 543 | + // {{{ run_command() |
544 | 544 | |
545 | | - /** |
546 | | - * Passes through $cmd to the memcache server connected by $sock; returns |
547 | | - * output as an array (null array if no output) |
548 | | - * |
549 | | - * NOTE: due to a possible bug in how PHP reads while using fgets(), each |
550 | | - * line may not be terminated by a \r\n. More specifically, my testing |
551 | | - * has shown that, on FreeBSD at least, each line is terminated only |
552 | | - * with a \n. This is with the PHP flag auto_detect_line_endings set |
553 | | - * to falase (the default). |
554 | | - * |
555 | | - * @param resource $sock Socket to send command on |
556 | | - * @param string $cmd Command to run |
557 | | - * |
558 | | - * @return array Output array |
559 | | - * @access public |
560 | | - */ |
561 | | - function run_command ($sock, $cmd) |
562 | | - { |
563 | | - if (!is_resource($sock)) |
564 | | - return array(); |
| 545 | + /** |
| 546 | + * Passes through $cmd to the memcache server connected by $sock; returns |
| 547 | + * output as an array (null array if no output) |
| 548 | + * |
| 549 | + * NOTE: due to a possible bug in how PHP reads while using fgets(), each |
| 550 | + * line may not be terminated by a \r\n. More specifically, my testing |
| 551 | + * has shown that, on FreeBSD at least, each line is terminated only |
| 552 | + * with a \n. This is with the PHP flag auto_detect_line_endings set |
| 553 | + * to falase (the default). |
| 554 | + * |
| 555 | + * @param resource $sock Socket to send command on |
| 556 | + * @param string $cmd Command to run |
| 557 | + * |
| 558 | + * @return array Output array |
| 559 | + * @access public |
| 560 | + */ |
| 561 | + function run_command ($sock, $cmd) |
| 562 | + { |
| 563 | + if (!is_resource($sock)) |
| 564 | + return array(); |
565 | 565 | |
566 | | - if (!$this->_safe_fwrite($sock, $cmd, strlen($cmd))) |
567 | | - return array(); |
| 566 | + if (!$this->_safe_fwrite($sock, $cmd, strlen($cmd))) |
| 567 | + return array(); |
568 | 568 | |
569 | | - while (true) |
570 | | - { |
571 | | - $res = fgets($sock); |
572 | | - $ret[] = $res; |
573 | | - if (preg_match('/^END/', $res)) |
574 | | - break; |
575 | | - if (strlen($res) == 0) |
576 | | - break; |
577 | | - } |
578 | | - return $ret; |
579 | | - } |
| 569 | + while (true) |
| 570 | + { |
| 571 | + $res = fgets($sock); |
| 572 | + $ret[] = $res; |
| 573 | + if (preg_match('/^END/', $res)) |
| 574 | + break; |
| 575 | + if (strlen($res) == 0) |
| 576 | + break; |
| 577 | + } |
| 578 | + return $ret; |
| 579 | + } |
580 | 580 | |
581 | | - // }}} |
582 | | - // {{{ set() |
| 581 | + // }}} |
| 582 | + // {{{ set() |
583 | 583 | |
584 | | - /** |
585 | | - * Unconditionally sets a key to a given value in the memcache. Returns true |
586 | | - * if set successfully. |
587 | | - * |
588 | | - * @param string $key Key to set value as |
589 | | - * @param mixed $value Value to set |
590 | | - * @param integer $exp (optional) Experiation time |
591 | | - * |
592 | | - * @return boolean TRUE on success |
593 | | - * @access public |
594 | | - */ |
| 584 | + /** |
| 585 | + * Unconditionally sets a key to a given value in the memcache. Returns true |
| 586 | + * if set successfully. |
| 587 | + * |
| 588 | + * @param string $key Key to set value as |
| 589 | + * @param mixed $value Value to set |
| 590 | + * @param integer $exp (optional) Experiation time |
| 591 | + * |
| 592 | + * @return boolean TRUE on success |
| 593 | + * @access public |
| 594 | + */ |
595 | 595 | function set ($key, $value, $exp=0) |
596 | | - { |
597 | | - return $this->_set('set', $key, $value, $exp); |
598 | | - } |
| 596 | + { |
| 597 | + return $this->_set('set', $key, $value, $exp); |
| 598 | + } |
599 | 599 | |
600 | | - // }}} |
601 | | - // {{{ set_compress_threshold() |
| 600 | + // }}} |
| 601 | + // {{{ set_compress_threshold() |
602 | 602 | |
603 | | - /** |
604 | | - * Sets the compression threshold |
605 | | - * |
606 | | - * @param integer $thresh Threshold to compress if larger than |
607 | | - * |
608 | | - * @access public |
609 | | - */ |
| 603 | + /** |
| 604 | + * Sets the compression threshold |
| 605 | + * |
| 606 | + * @param integer $thresh Threshold to compress if larger than |
| 607 | + * |
| 608 | + * @access public |
| 609 | + */ |
610 | 610 | function set_compress_threshold ($thresh) |
611 | | - { |
612 | | - $this->_compress_threshold = $thresh; |
613 | | - } |
| 611 | + { |
| 612 | + $this->_compress_threshold = $thresh; |
| 613 | + } |
614 | 614 | |
615 | | - // }}} |
616 | | - // {{{ set_debug() |
| 615 | + // }}} |
| 616 | + // {{{ set_debug() |
617 | 617 | |
618 | | - /** |
619 | | - * Sets the debug flag |
620 | | - * |
621 | | - * @param boolean $dbg TRUE for debugging, FALSE otherwise |
622 | | - * |
623 | | - * @access public |
624 | | - * |
625 | | - * @see memcahced::memcached |
626 | | - */ |
627 | | - function set_debug ($dbg) |
628 | | - { |
629 | | - $this->_debug = $dbg; |
630 | | - } |
| 618 | + /** |
| 619 | + * Sets the debug flag |
| 620 | + * |
| 621 | + * @param boolean $dbg TRUE for debugging, FALSE otherwise |
| 622 | + * |
| 623 | + * @access public |
| 624 | + * |
| 625 | + * @see memcahced::memcached |
| 626 | + */ |
| 627 | + function set_debug ($dbg) |
| 628 | + { |
| 629 | + $this->_debug = $dbg; |
| 630 | + } |
631 | 631 | |
632 | | - // }}} |
633 | | - // {{{ set_servers() |
| 632 | + // }}} |
| 633 | + // {{{ set_servers() |
634 | 634 | |
635 | | - /** |
636 | | - * Sets the server list to distribute key gets and puts between |
637 | | - * |
638 | | - * @param array $list Array of servers to connect to |
639 | | - * |
640 | | - * @access public |
641 | | - * |
642 | | - * @see memcached::memcached() |
643 | | - */ |
644 | | - function set_servers ($list) |
645 | | - { |
646 | | - $this->_servers = $list; |
647 | | - $this->_active = count($list); |
648 | | - $this->_buckets = null; |
649 | | - $this->_bucketcount = 0; |
| 635 | + /** |
| 636 | + * Sets the server list to distribute key gets and puts between |
| 637 | + * |
| 638 | + * @param array $list Array of servers to connect to |
| 639 | + * |
| 640 | + * @access public |
| 641 | + * |
| 642 | + * @see memcached::memcached() |
| 643 | + */ |
| 644 | + function set_servers ($list) |
| 645 | + { |
| 646 | + $this->_servers = $list; |
| 647 | + $this->_active = count($list); |
| 648 | + $this->_buckets = null; |
| 649 | + $this->_bucketcount = 0; |
650 | 650 | |
651 | | - $this->_single_sock = null; |
652 | | - if ($this->_active == 1) |
653 | | - $this->_single_sock = $this->_servers[0]; |
654 | | - } |
| 651 | + $this->_single_sock = null; |
| 652 | + if ($this->_active == 1) |
| 653 | + $this->_single_sock = $this->_servers[0]; |
| 654 | + } |
655 | 655 | |
656 | | - /** |
657 | | - * Sets the timeout for new connections |
658 | | - * |
659 | | - * @param integer $seconds Number of seconds |
660 | | - * @param integer $microseconds Number of microseconds |
661 | | - * |
662 | | - * @access public |
663 | | - */ |
664 | | - function set_timeout ($seconds, $microseconds) |
665 | | - { |
666 | | - $this->_timeout_seconds = $seconds; |
667 | | - $this->_timeout_microseconds = $microseconds; |
668 | | - } |
| 656 | + /** |
| 657 | + * Sets the timeout for new connections |
| 658 | + * |
| 659 | + * @param integer $seconds Number of seconds |
| 660 | + * @param integer $microseconds Number of microseconds |
| 661 | + * |
| 662 | + * @access public |
| 663 | + */ |
| 664 | + function set_timeout ($seconds, $microseconds) |
| 665 | + { |
| 666 | + $this->_timeout_seconds = $seconds; |
| 667 | + $this->_timeout_microseconds = $microseconds; |
| 668 | + } |
669 | 669 | |
670 | | - // }}} |
671 | | - // }}} |
672 | | - // {{{ private methods |
673 | | - // {{{ _close_sock() |
| 670 | + // }}} |
| 671 | + // }}} |
| 672 | + // {{{ private methods |
| 673 | + // {{{ _close_sock() |
674 | 674 | |
675 | | - /** |
676 | | - * Close the specified socket |
677 | | - * |
678 | | - * @param string $sock Socket to close |
679 | | - * |
680 | | - * @access private |
681 | | - */ |
682 | | - function _close_sock ($sock) |
683 | | - { |
684 | | - $host = array_search($sock, $this->_cache_sock); |
685 | | - fclose($this->_cache_sock[$host]); |
686 | | - unset($this->_cache_sock[$host]); |
687 | | - } |
| 675 | + /** |
| 676 | + * Close the specified socket |
| 677 | + * |
| 678 | + * @param string $sock Socket to close |
| 679 | + * |
| 680 | + * @access private |
| 681 | + */ |
| 682 | + function _close_sock ($sock) |
| 683 | + { |
| 684 | + $host = array_search($sock, $this->_cache_sock); |
| 685 | + fclose($this->_cache_sock[$host]); |
| 686 | + unset($this->_cache_sock[$host]); |
| 687 | + } |
688 | 688 | |
689 | | - // }}} |
690 | | - // {{{ _connect_sock() |
| 689 | + // }}} |
| 690 | + // {{{ _connect_sock() |
691 | 691 | |
692 | | - /** |
693 | | - * Connects $sock to $host, timing out after $timeout |
694 | | - * |
695 | | - * @param integer $sock Socket to connect |
696 | | - * @param string $host Host:IP to connect to |
697 | | - * |
698 | | - * @return boolean |
699 | | - * @access private |
700 | | - */ |
701 | | - function _connect_sock (&$sock, $host) |
702 | | - { |
703 | | - list ($ip, $port) = explode(":", $host); |
704 | | - $sock = false; |
705 | | - $timeout = $this->_connect_timeout; |
706 | | - $errno = $errstr = null; |
707 | | - for ($i = 0; !$sock && $i < $this->_connect_attempts; $i++) { |
708 | | - if ($this->_persistant == 1) |
709 | | - { |
710 | | - $sock = @pfsockopen($ip, $port, $errno, $errstr, $timeout); |
711 | | - } else |
712 | | - { |
713 | | - $sock = @fsockopen($ip, $port, $errno, $errstr, $timeout); |
714 | | - } |
715 | | - } |
716 | | - if (!$sock) { |
717 | | - if ($this->_debug) |
718 | | - $this->_debugprint( "Error connecting to $host: $errstr\n" ); |
719 | | - return false; |
720 | | - } |
| 692 | + /** |
| 693 | + * Connects $sock to $host, timing out after $timeout |
| 694 | + * |
| 695 | + * @param integer $sock Socket to connect |
| 696 | + * @param string $host Host:IP to connect to |
| 697 | + * |
| 698 | + * @return boolean |
| 699 | + * @access private |
| 700 | + */ |
| 701 | + function _connect_sock (&$sock, $host) |
| 702 | + { |
| 703 | + list ($ip, $port) = explode(":", $host); |
| 704 | + $sock = false; |
| 705 | + $timeout = $this->_connect_timeout; |
| 706 | + $errno = $errstr = null; |
| 707 | + for ($i = 0; !$sock && $i < $this->_connect_attempts; $i++) { |
| 708 | + if ($this->_persistant == 1) |
| 709 | + { |
| 710 | + $sock = @pfsockopen($ip, $port, $errno, $errstr, $timeout); |
| 711 | + } else |
| 712 | + { |
| 713 | + $sock = @fsockopen($ip, $port, $errno, $errstr, $timeout); |
| 714 | + } |
| 715 | + } |
| 716 | + if (!$sock) { |
| 717 | + if ($this->_debug) |
| 718 | + $this->_debugprint( "Error connecting to $host: $errstr\n" ); |
| 719 | + return false; |
| 720 | + } |
721 | 721 | |
722 | | - // Initialise timeout |
723 | | - stream_set_timeout($sock, $this->_timeout_seconds, $this->_timeout_microseconds); |
| 722 | + // Initialise timeout |
| 723 | + stream_set_timeout($sock, $this->_timeout_seconds, $this->_timeout_microseconds); |
724 | 724 | |
725 | | - return true; |
726 | | - } |
| 725 | + return true; |
| 726 | + } |
727 | 727 | |
728 | | - // }}} |
729 | | - // {{{ _dead_sock() |
| 728 | + // }}} |
| 729 | + // {{{ _dead_sock() |
730 | 730 | |
731 | | - /** |
732 | | - * Marks a host as dead until 30-40 seconds in the future |
733 | | - * |
734 | | - * @param string $sock Socket to mark as dead |
735 | | - * |
736 | | - * @access private |
737 | | - */ |
738 | | - function _dead_sock ($sock) |
739 | | - { |
740 | | - $host = array_search($sock, $this->_cache_sock); |
741 | | - $this->_dead_host($host); |
742 | | - } |
| 731 | + /** |
| 732 | + * Marks a host as dead until 30-40 seconds in the future |
| 733 | + * |
| 734 | + * @param string $sock Socket to mark as dead |
| 735 | + * |
| 736 | + * @access private |
| 737 | + */ |
| 738 | + function _dead_sock ($sock) |
| 739 | + { |
| 740 | + $host = array_search($sock, $this->_cache_sock); |
| 741 | + $this->_dead_host($host); |
| 742 | + } |
743 | 743 | |
744 | | - function _dead_host ($host) |
745 | | - { |
746 | | - @list ($ip, /* $port */) = explode(":", $host); |
747 | | - $this->_host_dead[$ip] = time() + 30 + intval(rand(0, 10)); |
748 | | - $this->_host_dead[$host] = $this->_host_dead[$ip]; |
749 | | - unset($this->_cache_sock[$host]); |
750 | | - } |
| 744 | + function _dead_host ($host) |
| 745 | + { |
| 746 | + @list ($ip, /* $port */) = explode(":", $host); |
| 747 | + $this->_host_dead[$ip] = time() + 30 + intval(rand(0, 10)); |
| 748 | + $this->_host_dead[$host] = $this->_host_dead[$ip]; |
| 749 | + unset($this->_cache_sock[$host]); |
| 750 | + } |
751 | 751 | |
752 | | - // }}} |
753 | | - // {{{ get_sock() |
| 752 | + // }}} |
| 753 | + // {{{ get_sock() |
754 | 754 | |
755 | | - /** |
756 | | - * get_sock |
757 | | - * |
758 | | - * @param string $key Key to retrieve value for; |
759 | | - * |
760 | | - * @return mixed resource on success, false on failure |
761 | | - * @access private |
762 | | - */ |
763 | | - function get_sock ($key) |
764 | | - { |
765 | | - if (!$this->_active) |
766 | | - return false; |
| 755 | + /** |
| 756 | + * get_sock |
| 757 | + * |
| 758 | + * @param string $key Key to retrieve value for; |
| 759 | + * |
| 760 | + * @return mixed resource on success, false on failure |
| 761 | + * @access private |
| 762 | + */ |
| 763 | + function get_sock ($key) |
| 764 | + { |
| 765 | + if (!$this->_active) |
| 766 | + return false; |
767 | 767 | |
768 | | - if ($this->_single_sock !== null) { |
769 | | - $this->_flush_read_buffer($this->_single_sock); |
770 | | - return $this->sock_to_host($this->_single_sock); |
771 | | - } |
| 768 | + if ($this->_single_sock !== null) { |
| 769 | + $this->_flush_read_buffer($this->_single_sock); |
| 770 | + return $this->sock_to_host($this->_single_sock); |
| 771 | + } |
772 | 772 | |
773 | | - $hv = is_array($key) ? intval($key[0]) : $this->_hashfunc($key); |
| 773 | + $hv = is_array($key) ? intval($key[0]) : $this->_hashfunc($key); |
774 | 774 | |
775 | | - if ($this->_buckets === null) |
776 | | - { |
777 | | - foreach ($this->_servers as $v) |
778 | | - { |
779 | | - if (is_array($v)) |
780 | | - { |
781 | | - for ($i=0; $i<$v[1]; $i++) |
782 | | - $bu[] = $v[0]; |
783 | | - } else |
784 | | - { |
785 | | - $bu[] = $v; |
786 | | - } |
787 | | - } |
788 | | - $this->_buckets = $bu; |
789 | | - $this->_bucketcount = count($bu); |
790 | | - } |
| 775 | + if ($this->_buckets === null) |
| 776 | + { |
| 777 | + foreach ($this->_servers as $v) |
| 778 | + { |
| 779 | + if (is_array($v)) |
| 780 | + { |
| 781 | + for ($i=0; $i<$v[1]; $i++) |
| 782 | + $bu[] = $v[0]; |
| 783 | + } else |
| 784 | + { |
| 785 | + $bu[] = $v; |
| 786 | + } |
| 787 | + } |
| 788 | + $this->_buckets = $bu; |
| 789 | + $this->_bucketcount = count($bu); |
| 790 | + } |
791 | 791 | |
792 | | - $realkey = is_array($key) ? $key[1] : $key; |
793 | | - for ($tries = 0; $tries<20; $tries++) |
794 | | - { |
795 | | - $host = $this->_buckets[$hv % $this->_bucketcount]; |
796 | | - $sock = $this->sock_to_host($host); |
797 | | - if (is_resource($sock)) { |
798 | | - $this->_flush_read_buffer($sock); |
799 | | - return $sock; |
| 792 | + $realkey = is_array($key) ? $key[1] : $key; |
| 793 | + for ($tries = 0; $tries<20; $tries++) |
| 794 | + { |
| 795 | + $host = $this->_buckets[$hv % $this->_bucketcount]; |
| 796 | + $sock = $this->sock_to_host($host); |
| 797 | + if (is_resource($sock)) { |
| 798 | + $this->_flush_read_buffer($sock); |
| 799 | + return $sock; |
800 | 800 | } |
801 | | - $hv = $this->_hashfunc( $hv . $realkey ); |
802 | | - } |
| 801 | + $hv = $this->_hashfunc( $hv . $realkey ); |
| 802 | + } |
803 | 803 | |
804 | | - return false; |
805 | | - } |
| 804 | + return false; |
| 805 | + } |
806 | 806 | |
807 | | - // }}} |
808 | | - // {{{ _hashfunc() |
| 807 | + // }}} |
| 808 | + // {{{ _hashfunc() |
809 | 809 | |
810 | | - /** |
811 | | - * Creates a hash integer based on the $key |
812 | | - * |
813 | | - * @param string $key Key to hash |
814 | | - * |
815 | | - * @return integer Hash value |
816 | | - * @access private |
817 | | - */ |
818 | | - function _hashfunc ($key) |
819 | | - { |
820 | | - # Hash function must on [0,0x7ffffff] |
821 | | - # We take the first 31 bits of the MD5 hash, which unlike the hash |
822 | | - # function used in a previous version of this client, works |
823 | | - return hexdec(substr(md5($key),0,8)) & 0x7fffffff; |
824 | | - } |
| 810 | + /** |
| 811 | + * Creates a hash integer based on the $key |
| 812 | + * |
| 813 | + * @param string $key Key to hash |
| 814 | + * |
| 815 | + * @return integer Hash value |
| 816 | + * @access private |
| 817 | + */ |
| 818 | + function _hashfunc ($key) |
| 819 | + { |
| 820 | + # Hash function must on [0,0x7ffffff] |
| 821 | + # We take the first 31 bits of the MD5 hash, which unlike the hash |
| 822 | + # function used in a previous version of this client, works |
| 823 | + return hexdec(substr(md5($key),0,8)) & 0x7fffffff; |
| 824 | + } |
825 | 825 | |
826 | | - // }}} |
827 | | - // {{{ _incrdecr() |
| 826 | + // }}} |
| 827 | + // {{{ _incrdecr() |
828 | 828 | |
829 | | - /** |
830 | | - * Perform increment/decriment on $key |
831 | | - * |
832 | | - * @param string $cmd Command to perform |
833 | | - * @param string $key Key to perform it on |
834 | | - * @param integer $amt Amount to adjust |
835 | | - * |
836 | | - * @return integer New value of $key |
837 | | - * @access private |
838 | | - */ |
839 | | - function _incrdecr ($cmd, $key, $amt=1) |
840 | | - { |
841 | | - if (!$this->_active) |
842 | | - return null; |
| 829 | + /** |
| 830 | + * Perform increment/decriment on $key |
| 831 | + * |
| 832 | + * @param string $cmd Command to perform |
| 833 | + * @param string $key Key to perform it on |
| 834 | + * @param integer $amt Amount to adjust |
| 835 | + * |
| 836 | + * @return integer New value of $key |
| 837 | + * @access private |
| 838 | + */ |
| 839 | + function _incrdecr ($cmd, $key, $amt=1) |
| 840 | + { |
| 841 | + if (!$this->_active) |
| 842 | + return null; |
843 | 843 | |
844 | | - $sock = $this->get_sock($key); |
845 | | - if (!is_resource($sock)) |
846 | | - return null; |
| 844 | + $sock = $this->get_sock($key); |
| 845 | + if (!is_resource($sock)) |
| 846 | + return null; |
847 | 847 | |
848 | | - $key = is_array($key) ? $key[1] : $key; |
849 | | - @$this->stats[$cmd]++; |
850 | | - if (!$this->_safe_fwrite($sock, "$cmd $key $amt\r\n")) |
851 | | - return $this->_dead_sock($sock); |
| 848 | + $key = is_array($key) ? $key[1] : $key; |
| 849 | + @$this->stats[$cmd]++; |
| 850 | + if (!$this->_safe_fwrite($sock, "$cmd $key $amt\r\n")) |
| 851 | + return $this->_dead_sock($sock); |
852 | 852 | |
853 | | - $line = fgets($sock); |
854 | | - $match = array(); |
855 | | - if (!preg_match('/^(\d+)/', $line, $match)) |
856 | | - return null; |
857 | | - return $match[1]; |
858 | | - } |
| 853 | + $line = fgets($sock); |
| 854 | + $match = array(); |
| 855 | + if (!preg_match('/^(\d+)/', $line, $match)) |
| 856 | + return null; |
| 857 | + return $match[1]; |
| 858 | + } |
859 | 859 | |
860 | | - // }}} |
861 | | - // {{{ _load_items() |
| 860 | + // }}} |
| 861 | + // {{{ _load_items() |
862 | 862 | |
863 | | - /** |
864 | | - * Load items into $ret from $sock |
865 | | - * |
866 | | - * @param resource $sock Socket to read from |
867 | | - * @param array $ret Returned values |
868 | | - * |
869 | | - * @access private |
870 | | - */ |
871 | | - function _load_items ($sock, &$ret) |
872 | | - { |
873 | | - while (1) |
874 | | - { |
875 | | - $decl = fgets($sock); |
876 | | - if ($decl == "END\r\n") |
877 | | - { |
878 | | - return true; |
879 | | - } elseif (preg_match('/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match)) |
880 | | - { |
881 | | - list($rkey, $flags, $len) = array($match[1], $match[2], $match[3]); |
882 | | - $bneed = $len+2; |
883 | | - $offset = 0; |
| 863 | + /** |
| 864 | + * Load items into $ret from $sock |
| 865 | + * |
| 866 | + * @param resource $sock Socket to read from |
| 867 | + * @param array $ret Returned values |
| 868 | + * |
| 869 | + * @access private |
| 870 | + */ |
| 871 | + function _load_items ($sock, &$ret) |
| 872 | + { |
| 873 | + while (1) |
| 874 | + { |
| 875 | + $decl = fgets($sock); |
| 876 | + if ($decl == "END\r\n") |
| 877 | + { |
| 878 | + return true; |
| 879 | + } elseif (preg_match('/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match)) |
| 880 | + { |
| 881 | + list($rkey, $flags, $len) = array($match[1], $match[2], $match[3]); |
| 882 | + $bneed = $len+2; |
| 883 | + $offset = 0; |
884 | 884 | |
885 | | - while ($bneed > 0) |
886 | | - { |
887 | | - $data = fread($sock, $bneed); |
888 | | - $n = strlen($data); |
889 | | - if ($n == 0) |
890 | | - break; |
891 | | - $offset += $n; |
892 | | - $bneed -= $n; |
893 | | - @$ret[$rkey] .= $data; |
894 | | - } |
| 885 | + while ($bneed > 0) |
| 886 | + { |
| 887 | + $data = fread($sock, $bneed); |
| 888 | + $n = strlen($data); |
| 889 | + if ($n == 0) |
| 890 | + break; |
| 891 | + $offset += $n; |
| 892 | + $bneed -= $n; |
| 893 | + @$ret[$rkey] .= $data; |
| 894 | + } |
895 | 895 | |
896 | | - if ($offset != $len+2) |
897 | | - { |
898 | | - // Something is borked! |
899 | | - if ($this->_debug) |
900 | | - $this->_debugprint(sprintf("Something is borked! key %s expecting %d got %d length\n", $rkey, $len+2, $offset)); |
| 896 | + if ($offset != $len+2) |
| 897 | + { |
| 898 | + // Something is borked! |
| 899 | + if ($this->_debug) |
| 900 | + $this->_debugprint(sprintf("Something is borked! key %s expecting %d got %d length\n", $rkey, $len+2, $offset)); |
901 | 901 | |
902 | | - unset($ret[$rkey]); |
903 | | - $this->_close_sock($sock); |
904 | | - return false; |
905 | | - } |
| 902 | + unset($ret[$rkey]); |
| 903 | + $this->_close_sock($sock); |
| 904 | + return false; |
| 905 | + } |
906 | 906 | |
907 | | - if ($this->_have_zlib && $flags & memcached::COMPRESSED) |
908 | | - $ret[$rkey] = gzuncompress($ret[$rkey]); |
| 907 | + if ($this->_have_zlib && $flags & memcached::COMPRESSED) |
| 908 | + $ret[$rkey] = gzuncompress($ret[$rkey]); |
909 | 909 | |
910 | | - $ret[$rkey] = rtrim($ret[$rkey]); |
| 910 | + $ret[$rkey] = rtrim($ret[$rkey]); |
911 | 911 | |
912 | | - if ($flags & memcached::SERIALIZED) |
913 | | - $ret[$rkey] = unserialize($ret[$rkey]); |
| 912 | + if ($flags & memcached::SERIALIZED) |
| 913 | + $ret[$rkey] = unserialize($ret[$rkey]); |
914 | 914 | |
915 | | - } else |
916 | | - { |
917 | | - $this->_debugprint("Error parsing memcached response\n"); |
918 | | - return 0; |
919 | | - } |
920 | | - } |
921 | | - } |
| 915 | + } else |
| 916 | + { |
| 917 | + $this->_debugprint("Error parsing memcached response\n"); |
| 918 | + return 0; |
| 919 | + } |
| 920 | + } |
| 921 | + } |
922 | 922 | |
923 | | - // }}} |
924 | | - // {{{ _set() |
| 923 | + // }}} |
| 924 | + // {{{ _set() |
925 | 925 | |
926 | | - /** |
927 | | - * Performs the requested storage operation to the memcache server |
928 | | - * |
929 | | - * @param string $cmd Command to perform |
930 | | - * @param string $key Key to act on |
931 | | - * @param mixed $val What we need to store |
932 | | - * @param integer $exp When it should expire |
933 | | - * |
934 | | - * @return boolean |
935 | | - * @access private |
936 | | - */ |
937 | | - function _set ($cmd, $key, $val, $exp) |
938 | | - { |
939 | | - if (!$this->_active) |
940 | | - return false; |
| 926 | + /** |
| 927 | + * Performs the requested storage operation to the memcache server |
| 928 | + * |
| 929 | + * @param string $cmd Command to perform |
| 930 | + * @param string $key Key to act on |
| 931 | + * @param mixed $val What we need to store |
| 932 | + * @param integer $exp When it should expire |
| 933 | + * |
| 934 | + * @return boolean |
| 935 | + * @access private |
| 936 | + */ |
| 937 | + function _set ($cmd, $key, $val, $exp) |
| 938 | + { |
| 939 | + if (!$this->_active) |
| 940 | + return false; |
941 | 941 | |
942 | | - $sock = $this->get_sock($key); |
943 | | - if (!is_resource($sock)) |
944 | | - return false; |
| 942 | + $sock = $this->get_sock($key); |
| 943 | + if (!is_resource($sock)) |
| 944 | + return false; |
945 | 945 | |
946 | | - @$this->stats[$cmd]++; |
| 946 | + @$this->stats[$cmd]++; |
947 | 947 | |
948 | | - $flags = 0; |
| 948 | + $flags = 0; |
949 | 949 | |
950 | | - if (!is_scalar($val)) |
951 | | - { |
952 | | - $val = serialize($val); |
953 | | - $flags |= memcached::SERIALIZED; |
954 | | - if ($this->_debug) |
955 | | - $this->_debugprint(sprintf("client: serializing data as it is not scalar\n")); |
956 | | - } |
| 950 | + if (!is_scalar($val)) |
| 951 | + { |
| 952 | + $val = serialize($val); |
| 953 | + $flags |= memcached::SERIALIZED; |
| 954 | + if ($this->_debug) |
| 955 | + $this->_debugprint(sprintf("client: serializing data as it is not scalar\n")); |
| 956 | + } |
957 | 957 | |
958 | | - $len = strlen($val); |
| 958 | + $len = strlen($val); |
959 | 959 | |
960 | | - if ($this->_have_zlib && $this->_compress_enable && |
961 | | - $this->_compress_threshold && $len >= $this->_compress_threshold) |
962 | | - { |
963 | | - $c_val = gzcompress($val, 9); |
964 | | - $c_len = strlen($c_val); |
| 960 | + if ($this->_have_zlib && $this->_compress_enable && |
| 961 | + $this->_compress_threshold && $len >= $this->_compress_threshold) |
| 962 | + { |
| 963 | + $c_val = gzcompress($val, 9); |
| 964 | + $c_len = strlen($c_val); |
965 | 965 | |
966 | | - if ($c_len < $len*(1 - memcached::COMPRESSION_SAVINGS)) |
967 | | - { |
968 | | - if ($this->_debug) |
969 | | - $this->_debugprint(sprintf("client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len)); |
970 | | - $val = $c_val; |
971 | | - $len = $c_len; |
972 | | - $flags |= memcached::COMPRESSED; |
973 | | - } |
974 | | - } |
975 | | - if (!$this->_safe_fwrite($sock, "$cmd $key $flags $exp $len\r\n$val\r\n")) |
976 | | - return $this->_dead_sock($sock); |
| 966 | + if ($c_len < $len*(1 - memcached::COMPRESSION_SAVINGS)) |
| 967 | + { |
| 968 | + if ($this->_debug) |
| 969 | + $this->_debugprint(sprintf("client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len)); |
| 970 | + $val = $c_val; |
| 971 | + $len = $c_len; |
| 972 | + $flags |= memcached::COMPRESSED; |
| 973 | + } |
| 974 | + } |
| 975 | + if (!$this->_safe_fwrite($sock, "$cmd $key $flags $exp $len\r\n$val\r\n")) |
| 976 | + return $this->_dead_sock($sock); |
977 | 977 | |
978 | | - $line = trim(fgets($sock)); |
| 978 | + $line = trim(fgets($sock)); |
979 | 979 | |
980 | | - if ($this->_debug) |
981 | | - { |
982 | | - $this->_debugprint(sprintf("%s %s (%s)\n", $cmd, $key, $line)); |
983 | | - } |
984 | | - if ($line == "STORED") |
985 | | - return true; |
986 | | - return false; |
987 | | - } |
| 980 | + if ($this->_debug) |
| 981 | + { |
| 982 | + $this->_debugprint(sprintf("%s %s (%s)\n", $cmd, $key, $line)); |
| 983 | + } |
| 984 | + if ($line == "STORED") |
| 985 | + return true; |
| 986 | + return false; |
| 987 | + } |
988 | 988 | |
989 | | - // }}} |
990 | | - // {{{ sock_to_host() |
| 989 | + // }}} |
| 990 | + // {{{ sock_to_host() |
991 | 991 | |
992 | | - /** |
993 | | - * Returns the socket for the host |
994 | | - * |
995 | | - * @param string $host Host:IP to get socket for |
996 | | - * |
997 | | - * @return mixed IO Stream or false |
998 | | - * @access private |
999 | | - */ |
1000 | | - function sock_to_host ($host) |
1001 | | - { |
1002 | | - if (isset($this->_cache_sock[$host])) |
1003 | | - return $this->_cache_sock[$host]; |
| 992 | + /** |
| 993 | + * Returns the socket for the host |
| 994 | + * |
| 995 | + * @param string $host Host:IP to get socket for |
| 996 | + * |
| 997 | + * @return mixed IO Stream or false |
| 998 | + * @access private |
| 999 | + */ |
| 1000 | + function sock_to_host ($host) |
| 1001 | + { |
| 1002 | + if (isset($this->_cache_sock[$host])) |
| 1003 | + return $this->_cache_sock[$host]; |
1004 | 1004 | |
1005 | | - $sock = null; |
1006 | | - $now = time(); |
1007 | | - list ($ip, /* $port */) = explode (":", $host); |
1008 | | - if (isset($this->_host_dead[$host]) && $this->_host_dead[$host] > $now || |
1009 | | - isset($this->_host_dead[$ip]) && $this->_host_dead[$ip] > $now) |
1010 | | - return null; |
| 1005 | + $sock = null; |
| 1006 | + $now = time(); |
| 1007 | + list ($ip, /* $port */) = explode (":", $host); |
| 1008 | + if (isset($this->_host_dead[$host]) && $this->_host_dead[$host] > $now || |
| 1009 | + isset($this->_host_dead[$ip]) && $this->_host_dead[$ip] > $now) |
| 1010 | + return null; |
1011 | 1011 | |
1012 | | - if (!$this->_connect_sock($sock, $host)) |
1013 | | - return $this->_dead_host($host); |
| 1012 | + if (!$this->_connect_sock($sock, $host)) |
| 1013 | + return $this->_dead_host($host); |
1014 | 1014 | |
1015 | | - // Do not buffer writes |
1016 | | - stream_set_write_buffer($sock, 0); |
| 1015 | + // Do not buffer writes |
| 1016 | + stream_set_write_buffer($sock, 0); |
1017 | 1017 | |
1018 | | - $this->_cache_sock[$host] = $sock; |
| 1018 | + $this->_cache_sock[$host] = $sock; |
1019 | 1019 | |
1020 | | - return $this->_cache_sock[$host]; |
1021 | | - } |
| 1020 | + return $this->_cache_sock[$host]; |
| 1021 | + } |
1022 | 1022 | |
1023 | | - function _debugprint($str){ |
1024 | | - print($str); |
1025 | | - } |
| 1023 | + function _debugprint($str){ |
| 1024 | + print($str); |
| 1025 | + } |
1026 | 1026 | |
1027 | | - /** |
1028 | | - * Write to a stream, timing out after the correct amount of time |
1029 | | - * |
1030 | | - * @return bool false on failure, true on success |
1031 | | - */ |
1032 | | - /* |
1033 | | - function _safe_fwrite($f, $buf, $len = false) { |
1034 | | - stream_set_blocking($f, 0); |
| 1027 | + /** |
| 1028 | + * Write to a stream, timing out after the correct amount of time |
| 1029 | + * |
| 1030 | + * @return bool false on failure, true on success |
| 1031 | + */ |
| 1032 | + /* |
| 1033 | + function _safe_fwrite($f, $buf, $len = false) { |
| 1034 | + stream_set_blocking($f, 0); |
1035 | 1035 | |
1036 | | - if ($len === false) { |
1037 | | - wfDebug("Writing " . strlen( $buf ) . " bytes\n"); |
1038 | | - $bytesWritten = fwrite($f, $buf); |
1039 | | - } else { |
1040 | | - wfDebug("Writing $len bytes\n"); |
1041 | | - $bytesWritten = fwrite($f, $buf, $len); |
1042 | | - } |
1043 | | - $n = stream_select($r=NULL, $w = array($f), $e = NULL, 10, 0); |
1044 | | - # $this->_timeout_seconds, $this->_timeout_microseconds); |
| 1036 | + if ($len === false) { |
| 1037 | + wfDebug("Writing " . strlen( $buf ) . " bytes\n"); |
| 1038 | + $bytesWritten = fwrite($f, $buf); |
| 1039 | + } else { |
| 1040 | + wfDebug("Writing $len bytes\n"); |
| 1041 | + $bytesWritten = fwrite($f, $buf, $len); |
| 1042 | + } |
| 1043 | + $n = stream_select($r=NULL, $w = array($f), $e = NULL, 10, 0); |
| 1044 | + # $this->_timeout_seconds, $this->_timeout_microseconds); |
1045 | 1045 | |
1046 | | - wfDebug("stream_select returned $n\n"); |
1047 | | - stream_set_blocking($f, 1); |
1048 | | - return $n == 1; |
1049 | | - return $bytesWritten; |
1050 | | - }*/ |
| 1046 | + wfDebug("stream_select returned $n\n"); |
| 1047 | + stream_set_blocking($f, 1); |
| 1048 | + return $n == 1; |
| 1049 | + return $bytesWritten; |
| 1050 | + }*/ |
1051 | 1051 | |
1052 | | - /** |
1053 | | - * Original behaviour |
1054 | | - */ |
1055 | | - function _safe_fwrite($f, $buf, $len = false) { |
1056 | | - if ($len === false) { |
1057 | | - $bytesWritten = fwrite($f, $buf); |
1058 | | - } else { |
1059 | | - $bytesWritten = fwrite($f, $buf, $len); |
1060 | | - } |
1061 | | - return $bytesWritten; |
1062 | | - } |
| 1052 | + /** |
| 1053 | + * Original behaviour |
| 1054 | + */ |
| 1055 | + function _safe_fwrite($f, $buf, $len = false) { |
| 1056 | + if ($len === false) { |
| 1057 | + $bytesWritten = fwrite($f, $buf); |
| 1058 | + } else { |
| 1059 | + $bytesWritten = fwrite($f, $buf, $len); |
| 1060 | + } |
| 1061 | + return $bytesWritten; |
| 1062 | + } |
1063 | 1063 | |
1064 | | - /** |
1065 | | - * Flush the read buffer of a stream |
1066 | | - */ |
1067 | | - function _flush_read_buffer($f) { |
1068 | | - if (!is_resource($f)) { |
1069 | | - return; |
1070 | | - } |
1071 | | - $n = stream_select($r=array($f), $w = NULL, $e = NULL, 0, 0); |
1072 | | - while ($n == 1 && !feof($f)) { |
1073 | | - fread($f, 1024); |
1074 | | - $n = stream_select($r=array($f), $w = NULL, $e = NULL, 0, 0); |
1075 | | - } |
1076 | | - } |
| 1064 | + /** |
| 1065 | + * Flush the read buffer of a stream |
| 1066 | + */ |
| 1067 | + function _flush_read_buffer($f) { |
| 1068 | + if (!is_resource($f)) { |
| 1069 | + return; |
| 1070 | + } |
| 1071 | + $n = stream_select($r=array($f), $w = NULL, $e = NULL, 0, 0); |
| 1072 | + while ($n == 1 && !feof($f)) { |
| 1073 | + fread($f, 1024); |
| 1074 | + $n = stream_select($r=array($f), $w = NULL, $e = NULL, 0, 0); |
| 1075 | + } |
| 1076 | + } |
1077 | 1077 | |
1078 | | - // }}} |
1079 | | - // }}} |
1080 | | - // }}} |
| 1078 | + // }}} |
| 1079 | + // }}} |
| 1080 | + // }}} |
1081 | 1081 | } |
1082 | 1082 | |
1083 | 1083 | // vim: sts=3 sw=3 et |