Index: trunk/phase3/includes/JobQueue.php |
— | — | @@ -46,16 +46,20 @@ |
47 | 47 | * actually find a job; it may be adversely affected by concurrent job |
48 | 48 | * runners. |
49 | 49 | */ |
50 | | - static function pop_type($type) { |
| 50 | + static function pop_type( $type ) { |
51 | 51 | wfProfilein( __METHOD__ ); |
52 | 52 | |
53 | 53 | $dbw = wfGetDB( DB_MASTER ); |
54 | 54 | |
| 55 | + $row = $dbw->selectRow( |
| 56 | + 'job', |
| 57 | + '*', |
| 58 | + array( 'job_cmd' => $type ), |
| 59 | + __METHOD__, |
| 60 | + array( 'LIMIT' => 1 ) |
| 61 | + ); |
55 | 62 | |
56 | | - $row = $dbw->selectRow( 'job', '*', array( 'job_cmd' => $type ), __METHOD__, |
57 | | - array( 'LIMIT' => 1 )); |
58 | | - |
59 | | - if ($row === false) { |
| 63 | + if ( $row === false ) { |
60 | 64 | wfProfileOut( __METHOD__ ); |
61 | 65 | return false; |
62 | 66 | } |
— | — | @@ -64,7 +68,7 @@ |
65 | 69 | $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ ); |
66 | 70 | $affected = $dbw->affectedRows(); |
67 | 71 | |
68 | | - if ($affected == 0) { |
| 72 | + if ( $affected == 0 ) { |
69 | 73 | wfProfileOut( __METHOD__ ); |
70 | 74 | return false; |
71 | 75 | } |
— | — | @@ -75,7 +79,7 @@ |
76 | 80 | $job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ), $row->job_id ); |
77 | 81 | |
78 | 82 | $dbw->delete( 'job', $job->insertFields(), __METHOD__ ); |
79 | | - $dbw->immediateCommit(); |
| 83 | + $dbw->commit(); |
80 | 84 | |
81 | 85 | wfProfileOut( __METHOD__ ); |
82 | 86 | return $job; |
— | — | @@ -84,10 +88,10 @@ |
85 | 89 | /** |
86 | 90 | * Pop a job off the front of the queue |
87 | 91 | * |
88 | | - * @param $offset Number of jobs to skip |
| 92 | + * @param $offset Integer: Number of jobs to skip |
89 | 93 | * @return Job or false if there's no jobs |
90 | 94 | */ |
91 | | - static function pop($offset=0) { |
| 95 | + static function pop( $offset = 0 ) { |
92 | 96 | wfProfileIn( __METHOD__ ); |
93 | 97 | |
94 | 98 | $dbr = wfGetDB( DB_SLAVE ); |
— | — | @@ -100,17 +104,18 @@ |
101 | 105 | */ |
102 | 106 | |
103 | 107 | $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__, |
104 | | - array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 )); |
| 108 | + array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) ); |
105 | 109 | |
106 | 110 | // Refetching without offset is needed as some of job IDs could have had delayed commits |
107 | 111 | // and have lower IDs than jobs already executed, blame concurrency :) |
108 | 112 | // |
109 | | - if ( $row === false) { |
110 | | - if ($offset!=0) |
| 113 | + if ( $row === false ) { |
| 114 | + if ( $offset != 0 ) { |
111 | 115 | $row = $dbr->selectRow( 'job', '*', '', __METHOD__, |
112 | | - array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 )); |
| 116 | + array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) ); |
| 117 | + } |
113 | 118 | |
114 | | - if ($row === false ) { |
| 119 | + if ( $row === false ) { |
115 | 120 | wfProfileOut( __METHOD__ ); |
116 | 121 | return false; |
117 | 122 | } |
— | — | @@ -121,7 +126,7 @@ |
122 | 127 | $dbw = wfGetDB( DB_MASTER ); |
123 | 128 | $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ ); |
124 | 129 | $affected = $dbw->affectedRows(); |
125 | | - $dbw->immediateCommit(); |
| 130 | + $dbw->commit(); |
126 | 131 | |
127 | 132 | if ( !$affected ) { |
128 | 133 | // Failed, someone else beat us to it |
— | — | @@ -135,7 +140,7 @@ |
136 | 141 | } |
137 | 142 | // Get the random row |
138 | 143 | $row = $dbw->selectRow( 'job', '*', |
139 | | - 'job_id >= ' . mt_rand( $row->minjob, $row->maxjob ), __METHOD__ ); |
| 144 | + 'job_id >= ' . mt_rand( $row->minjob, $row->maxjob ), __METHOD__ ); |
140 | 145 | if ( $row === false ) { |
141 | 146 | // Random job gone before we got the chance to select it |
142 | 147 | // Give up |
— | — | @@ -145,7 +150,7 @@ |
146 | 151 | // Delete the random row |
147 | 152 | $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ ); |
148 | 153 | $affected = $dbw->affectedRows(); |
149 | | - $dbw->immediateCommit(); |
| 154 | + $dbw->commit(); |
150 | 155 | |
151 | 156 | if ( !$affected ) { |
152 | 157 | // Random job gone before we exclusively deleted it |
Index: trunk/phase3/includes/memcached-client.php |
— | — | @@ -70,10 +70,9 @@ |
71 | 71 | * @author Ryan T. Dean <rtdean@cytherianage.net> |
72 | 72 | * @ingroup Cache |
73 | 73 | */ |
74 | | -class MWMemcached |
75 | | -{ |
76 | | - // {{{ properties |
77 | | - // {{{ public |
| 74 | +class MWMemcached { |
| 75 | + // {{{ properties |
| 76 | + // {{{ public |
78 | 77 | |
79 | 78 | // {{{ constants |
80 | 79 | // {{{ flags |
— | — | @@ -243,17 +242,15 @@ |
244 | 243 | * @param array $args Associative array of settings |
245 | 244 | * |
246 | 245 | * @return mixed |
247 | | - * @access public |
248 | 246 | */ |
249 | | - function __construct ($args) |
250 | | - { |
251 | | - $this->set_servers(@$args['servers']); |
| 247 | + public function __construct( $args ) { |
| 248 | + $this->set_servers( @$args['servers'] ); |
252 | 249 | $this->_debug = @$args['debug']; |
253 | 250 | $this->stats = array(); |
254 | 251 | $this->_compress_threshold = @$args['compress_threshold']; |
255 | | - $this->_persistant = array_key_exists('persistant', $args) ? (@$args['persistant']) : false; |
| 252 | + $this->_persistant = array_key_exists( 'persistant', $args ) ? ( @$args['persistant'] ) : false; |
256 | 253 | $this->_compress_enable = true; |
257 | | - $this->_have_zlib = function_exists("gzcompress"); |
| 254 | + $this->_have_zlib = function_exists( 'gzcompress' ); |
258 | 255 | |
259 | 256 | $this->_cache_sock = array(); |
260 | 257 | $this->_host_dead = array(); |
— | — | @@ -277,11 +274,9 @@ |
278 | 275 | * @param integer $exp (optional) Time to expire data at |
279 | 276 | * |
280 | 277 | * @return boolean |
281 | | - * @access public |
282 | 278 | */ |
283 | | - function add ($key, $val, $exp = 0) |
284 | | - { |
285 | | - return $this->_set('add', $key, $val, $exp); |
| 279 | + public function add( $key, $val, $exp = 0 ) { |
| 280 | + return $this->_set( 'add', $key, $val, $exp ); |
286 | 281 | } |
287 | 282 | |
288 | 283 | // }}} |
— | — | @@ -294,11 +289,9 @@ |
295 | 290 | * @param integer $amt (optional) Amount to decriment |
296 | 291 | * |
297 | 292 | * @return mixed FALSE on failure, value on success |
298 | | - * @access public |
299 | 293 | */ |
300 | | - function decr ($key, $amt=1) |
301 | | - { |
302 | | - return $this->_incrdecr('decr', $key, $amt); |
| 294 | + public function decr( $key, $amt = 1 ) { |
| 295 | + return $this->_incrdecr( 'decr', $key, $amt ); |
303 | 296 | } |
304 | 297 | |
305 | 298 | // }}} |
— | — | @@ -311,33 +304,34 @@ |
312 | 305 | * @param integer $time (optional) How long to wait before deleting |
313 | 306 | * |
314 | 307 | * @return boolean TRUE on success, FALSE on failure |
315 | | - * @access public |
316 | 308 | */ |
317 | | - function delete ($key, $time = 0) |
318 | | - { |
319 | | - if (!$this->_active) |
| 309 | + public function delete( $key, $time = 0 ) { |
| 310 | + if ( !$this->_active ) { |
320 | 311 | return false; |
| 312 | + } |
321 | 313 | |
322 | | - $sock = $this->get_sock($key); |
323 | | - if (!is_resource($sock)) |
| 314 | + $sock = $this->get_sock( $key ); |
| 315 | + if ( !is_resource( $sock ) ) { |
324 | 316 | return false; |
| 317 | + } |
325 | 318 | |
326 | | - $key = is_array($key) ? $key[1] : $key; |
| 319 | + $key = is_array( $key ) ? $key[1] : $key; |
327 | 320 | |
328 | 321 | @$this->stats['delete']++; |
329 | 322 | $cmd = "delete $key $time\r\n"; |
330 | | - if(!$this->_safe_fwrite($sock, $cmd, strlen($cmd))) |
331 | | - { |
332 | | - $this->_dead_sock($sock); |
| 323 | + if( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) { |
| 324 | + $this->_dead_sock( $sock ); |
333 | 325 | return false; |
334 | 326 | } |
335 | | - $res = trim(fgets($sock)); |
| 327 | + $res = trim( fgets( $sock ) ); |
336 | 328 | |
337 | | - if ($this->_debug) |
338 | | - $this->_debugprint(sprintf("MemCache: delete %s (%s)\n", $key, $res)); |
| 329 | + if ( $this->_debug ) { |
| 330 | + $this->_debugprint( sprintf( "MemCache: delete %s (%s)\n", $key, $res ) ); |
| 331 | + } |
339 | 332 | |
340 | | - if ($res == "DELETED") |
| 333 | + if ( $res == "DELETED" ) { |
341 | 334 | return true; |
| 335 | + } |
342 | 336 | return false; |
343 | 337 | } |
344 | 338 | |
— | — | @@ -346,13 +340,11 @@ |
347 | 341 | |
348 | 342 | /** |
349 | 343 | * Disconnects all connected sockets |
350 | | - * |
351 | | - * @access public |
352 | 344 | */ |
353 | | - function disconnect_all () |
354 | | - { |
355 | | - foreach ($this->_cache_sock as $sock) |
356 | | - fclose($sock); |
| 345 | + public function disconnect_all() { |
| 346 | + foreach ( $this->_cache_sock as $sock ) { |
| 347 | + fclose( $sock ); |
| 348 | + } |
357 | 349 | |
358 | 350 | $this->_cache_sock = array(); |
359 | 351 | } |
— | — | @@ -364,11 +356,8 @@ |
365 | 357 | * Enable / Disable compression |
366 | 358 | * |
367 | 359 | * @param boolean $enable TRUE to enable, FALSE to disable |
368 | | - * |
369 | | - * @access public |
370 | 360 | */ |
371 | | - function enable_compress ($enable) |
372 | | - { |
| 361 | + public function enable_compress( $enable ) { |
373 | 362 | $this->_compress_enable = $enable; |
374 | 363 | } |
375 | 364 | |
— | — | @@ -377,11 +366,8 @@ |
378 | 367 | |
379 | 368 | /** |
380 | 369 | * Forget about all of the dead hosts |
381 | | - * |
382 | | - * @access public |
383 | 370 | */ |
384 | | - function forget_dead_hosts () |
385 | | - { |
| 371 | + public function forget_dead_hosts() { |
386 | 372 | $this->_host_dead = array(); |
387 | 373 | } |
388 | 374 | |
— | — | @@ -394,24 +380,22 @@ |
395 | 381 | * @param string $key Key to retrieve |
396 | 382 | * |
397 | 383 | * @return mixed |
398 | | - * @access public |
399 | 384 | */ |
400 | | - function get ($key) |
401 | | - { |
| 385 | + public function get( $key ) { |
402 | 386 | wfProfileIn( __METHOD__ ); |
403 | 387 | |
404 | 388 | if ( $this->_debug ) { |
405 | 389 | $this->_debugprint( "get($key)\n" ); |
406 | 390 | } |
407 | 391 | |
408 | | - if (!$this->_active) { |
| 392 | + if ( !$this->_active ) { |
409 | 393 | wfProfileOut( __METHOD__ ); |
410 | 394 | return false; |
411 | 395 | } |
412 | 396 | |
413 | | - $sock = $this->get_sock($key); |
| 397 | + $sock = $this->get_sock( $key ); |
414 | 398 | |
415 | | - if (!is_resource($sock)) { |
| 399 | + if ( !is_resource( $sock ) ) { |
416 | 400 | wfProfileOut( __METHOD__ ); |
417 | 401 | return false; |
418 | 402 | } |
— | — | @@ -419,19 +403,20 @@ |
420 | 404 | @$this->stats['get']++; |
421 | 405 | |
422 | 406 | $cmd = "get $key\r\n"; |
423 | | - if (!$this->_safe_fwrite($sock, $cmd, strlen($cmd))) |
424 | | - { |
425 | | - $this->_dead_sock($sock); |
| 407 | + if ( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) { |
| 408 | + $this->_dead_sock( $sock ); |
426 | 409 | wfProfileOut( __METHOD__ ); |
427 | 410 | return false; |
428 | 411 | } |
429 | 412 | |
430 | 413 | $val = array(); |
431 | | - $this->_load_items($sock, $val); |
| 414 | + $this->_load_items( $sock, $val ); |
432 | 415 | |
433 | | - if ($this->_debug) |
434 | | - foreach ($val as $k => $v) |
435 | | - $this->_debugprint(sprintf("MemCache: sock %s got %s\n", serialize($sock), $k)); |
| 416 | + if ( $this->_debug ) { |
| 417 | + foreach ( $val as $k => $v ) { |
| 418 | + $this->_debugprint( sprintf( "MemCache: sock %s got %s\n", serialize( $sock ), $k ) ); |
| 419 | + } |
| 420 | + } |
436 | 421 | |
437 | 422 | wfProfileOut( __METHOD__ ); |
438 | 423 | return @$val[$key]; |
— | — | @@ -446,23 +431,22 @@ |
447 | 432 | * @param array $keys Keys to retrieve |
448 | 433 | * |
449 | 434 | * @return array |
450 | | - * @access public |
451 | 435 | */ |
452 | | - function get_multi ($keys) |
453 | | - { |
454 | | - if (!$this->_active) |
| 436 | + public function get_multi( $keys ) { |
| 437 | + if ( !$this->_active ) { |
455 | 438 | return false; |
| 439 | + } |
456 | 440 | |
457 | 441 | @$this->stats['get_multi']++; |
458 | 442 | $sock_keys = array(); |
459 | 443 | |
460 | | - foreach ($keys as $key) |
461 | | - { |
462 | | - $sock = $this->get_sock($key); |
463 | | - if (!is_resource($sock)) continue; |
464 | | - $key = is_array($key) ? $key[1] : $key; |
465 | | - if (!isset($sock_keys[$sock])) |
466 | | - { |
| 444 | + foreach ( $keys as $key ) { |
| 445 | + $sock = $this->get_sock( $key ); |
| 446 | + if ( !is_resource( $sock ) ) { |
| 447 | + continue; |
| 448 | + } |
| 449 | + $key = is_array( $key ) ? $key[1] : $key; |
| 450 | + if ( !isset( $sock_keys[$sock] ) ) { |
467 | 451 | $sock_keys[$sock] = array(); |
468 | 452 | $socks[] = $sock; |
469 | 453 | } |
— | — | @@ -470,34 +454,31 @@ |
471 | 455 | } |
472 | 456 | |
473 | 457 | // Send out the requests |
474 | | - foreach ($socks as $sock) |
475 | | - { |
476 | | - $cmd = "get"; |
477 | | - foreach ($sock_keys[$sock] as $key) |
478 | | - { |
479 | | - $cmd .= " ". $key; |
| 458 | + foreach ( $socks as $sock ) { |
| 459 | + $cmd = 'get'; |
| 460 | + foreach ( $sock_keys[$sock] as $key ) { |
| 461 | + $cmd .= ' ' . $key; |
480 | 462 | } |
481 | 463 | $cmd .= "\r\n"; |
482 | 464 | |
483 | | - if ($this->_safe_fwrite($sock, $cmd, strlen($cmd))) |
484 | | - { |
| 465 | + if ( $this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) { |
485 | 466 | $gather[] = $sock; |
486 | | - } else |
487 | | - { |
488 | | - $this->_dead_sock($sock); |
| 467 | + } else { |
| 468 | + $this->_dead_sock( $sock ); |
489 | 469 | } |
490 | 470 | } |
491 | 471 | |
492 | 472 | // Parse responses |
493 | 473 | $val = array(); |
494 | | - foreach ($gather as $sock) |
495 | | - { |
496 | | - $this->_load_items($sock, $val); |
| 474 | + foreach ( $gather as $sock ) { |
| 475 | + $this->_load_items( $sock, $val ); |
497 | 476 | } |
498 | 477 | |
499 | | - if ($this->_debug) |
500 | | - foreach ($val as $k => $v) |
501 | | - $this->_debugprint(sprintf("MemCache: got %s\n", $k)); |
| 478 | + if ( $this->_debug ) { |
| 479 | + foreach ( $val as $k => $v ) { |
| 480 | + $this->_debugprint( sprintf( "MemCache: got %s\n", $k ) ); |
| 481 | + } |
| 482 | + } |
502 | 483 | |
503 | 484 | return $val; |
504 | 485 | } |
— | — | @@ -512,11 +493,9 @@ |
513 | 494 | * @param integer $amt (optional) amount to increment |
514 | 495 | * |
515 | 496 | * @return integer New key value? |
516 | | - * @access public |
517 | 497 | */ |
518 | | - function incr ($key, $amt=1) |
519 | | - { |
520 | | - return $this->_incrdecr('incr', $key, $amt); |
| 498 | + public function incr( $key, $amt = 1 ) { |
| 499 | + return $this->_incrdecr( 'incr', $key, $amt ); |
521 | 500 | } |
522 | 501 | |
523 | 502 | // }}} |
— | — | @@ -530,11 +509,9 @@ |
531 | 510 | * @param integer $exp (optional) Experiation time |
532 | 511 | * |
533 | 512 | * @return boolean |
534 | | - * @access public |
535 | 513 | */ |
536 | | - function replace ($key, $value, $exp=0) |
537 | | - { |
538 | | - return $this->_set('replace', $key, $value, $exp); |
| 514 | + public function replace( $key, $value, $exp = 0 ) { |
| 515 | + return $this->_set( 'replace', $key, $value, $exp ); |
539 | 516 | } |
540 | 517 | |
541 | 518 | // }}} |
— | — | @@ -556,22 +533,24 @@ |
557 | 534 | * @return array Output array |
558 | 535 | * @access public |
559 | 536 | */ |
560 | | - function run_command ($sock, $cmd) |
561 | | - { |
562 | | - if (!is_resource($sock)) |
| 537 | + function run_command( $sock, $cmd ) { |
| 538 | + if ( !is_resource( $sock ) ) { |
563 | 539 | return array(); |
| 540 | + } |
564 | 541 | |
565 | | - if (!$this->_safe_fwrite($sock, $cmd, strlen($cmd))) |
| 542 | + if ( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) { |
566 | 543 | return array(); |
| 544 | + } |
567 | 545 | |
568 | | - while (true) |
569 | | - { |
570 | | - $res = fgets($sock); |
| 546 | + while ( true ) { |
| 547 | + $res = fgets( $sock ); |
571 | 548 | $ret[] = $res; |
572 | | - if (preg_match('/^END/', $res)) |
| 549 | + if ( preg_match( '/^END/', $res ) ) { |
573 | 550 | break; |
574 | | - if (strlen($res) == 0) |
| 551 | + } |
| 552 | + if ( strlen( $res ) == 0 ) { |
575 | 553 | break; |
| 554 | + } |
576 | 555 | } |
577 | 556 | return $ret; |
578 | 557 | } |
— | — | @@ -588,11 +567,9 @@ |
589 | 568 | * @param integer $exp (optional) Experiation time |
590 | 569 | * |
591 | 570 | * @return boolean TRUE on success |
592 | | - * @access public |
593 | 571 | */ |
594 | | - function set ($key, $value, $exp=0) |
595 | | - { |
596 | | - return $this->_set('set', $key, $value, $exp); |
| 572 | + public function set( $key, $value, $exp = 0 ) { |
| 573 | + return $this->_set( 'set', $key, $value, $exp ); |
597 | 574 | } |
598 | 575 | |
599 | 576 | // }}} |
— | — | @@ -602,11 +579,8 @@ |
603 | 580 | * Sets the compression threshold |
604 | 581 | * |
605 | 582 | * @param integer $thresh Threshold to compress if larger than |
606 | | - * |
607 | | - * @access public |
608 | 583 | */ |
609 | | - function set_compress_threshold ($thresh) |
610 | | - { |
| 584 | + public function set_compress_threshold( $thresh ) { |
611 | 585 | $this->_compress_threshold = $thresh; |
612 | 586 | } |
613 | 587 | |
— | — | @@ -618,12 +592,9 @@ |
619 | 593 | * |
620 | 594 | * @param boolean $dbg TRUE for debugging, FALSE otherwise |
621 | 595 | * |
622 | | - * @access public |
623 | | - * |
624 | 596 | * @see MWMemcached::__construct |
625 | 597 | */ |
626 | | - function set_debug ($dbg) |
627 | | - { |
| 598 | + public function set_debug( $dbg ) { |
628 | 599 | $this->_debug = $dbg; |
629 | 600 | } |
630 | 601 | |
— | — | @@ -635,20 +606,18 @@ |
636 | 607 | * |
637 | 608 | * @param array $list Array of servers to connect to |
638 | 609 | * |
639 | | - * @access public |
640 | | - * |
641 | 610 | * @see MWMemcached::__construct() |
642 | 611 | */ |
643 | | - function set_servers ($list) |
644 | | - { |
| 612 | + public function set_servers( $list ) { |
645 | 613 | $this->_servers = $list; |
646 | | - $this->_active = count($list); |
| 614 | + $this->_active = count( $list ); |
647 | 615 | $this->_buckets = null; |
648 | 616 | $this->_bucketcount = 0; |
649 | 617 | |
650 | 618 | $this->_single_sock = null; |
651 | | - if ($this->_active == 1) |
| 619 | + if ( $this->_active == 1 ) { |
652 | 620 | $this->_single_sock = $this->_servers[0]; |
| 621 | + } |
653 | 622 | } |
654 | 623 | |
655 | 624 | /** |
— | — | @@ -656,11 +625,8 @@ |
657 | 626 | * |
658 | 627 | * @param integer $seconds Number of seconds |
659 | 628 | * @param integer $microseconds Number of microseconds |
660 | | - * |
661 | | - * @access public |
662 | 629 | */ |
663 | | - function set_timeout ($seconds, $microseconds) |
664 | | - { |
| 630 | + public function set_timeout( $seconds, $microseconds ) { |
665 | 631 | $this->_timeout_seconds = $seconds; |
666 | 632 | $this->_timeout_microseconds = $microseconds; |
667 | 633 | } |
— | — | @@ -677,11 +643,10 @@ |
678 | 644 | * |
679 | 645 | * @access private |
680 | 646 | */ |
681 | | - function _close_sock ($sock) |
682 | | - { |
683 | | - $host = array_search($sock, $this->_cache_sock); |
684 | | - fclose($this->_cache_sock[$host]); |
685 | | - unset($this->_cache_sock[$host]); |
| 647 | + function _close_sock( $sock ) { |
| 648 | + $host = array_search( $sock, $this->_cache_sock ); |
| 649 | + fclose( $this->_cache_sock[$host] ); |
| 650 | + unset( $this->_cache_sock[$host] ); |
686 | 651 | } |
687 | 652 | |
688 | 653 | // }}} |
— | — | @@ -696,29 +661,27 @@ |
697 | 662 | * @return boolean |
698 | 663 | * @access private |
699 | 664 | */ |
700 | | - function _connect_sock (&$sock, $host) |
701 | | - { |
702 | | - list ($ip, $port) = explode(":", $host); |
| 665 | + function _connect_sock( &$sock, $host ) { |
| 666 | + list( $ip, $port ) = explode( ':', $host ); |
703 | 667 | $sock = false; |
704 | 668 | $timeout = $this->_connect_timeout; |
705 | 669 | $errno = $errstr = null; |
706 | | - for ($i = 0; !$sock && $i < $this->_connect_attempts; $i++) { |
707 | | - if ($this->_persistant == 1) |
708 | | - { |
709 | | - $sock = @pfsockopen($ip, $port, $errno, $errstr, $timeout); |
710 | | - } else |
711 | | - { |
712 | | - $sock = @fsockopen($ip, $port, $errno, $errstr, $timeout); |
| 670 | + for( $i = 0; !$sock && $i < $this->_connect_attempts; $i++ ) { |
| 671 | + if ( $this->_persistant == 1 ) { |
| 672 | + $sock = @pfsockopen( $ip, $port, $errno, $errstr, $timeout ); |
| 673 | + } else { |
| 674 | + $sock = @fsockopen( $ip, $port, $errno, $errstr, $timeout ); |
713 | 675 | } |
714 | 676 | } |
715 | | - if (!$sock) { |
716 | | - if ($this->_debug) |
| 677 | + if ( !$sock ) { |
| 678 | + if ( $this->_debug ) { |
717 | 679 | $this->_debugprint( "Error connecting to $host: $errstr\n" ); |
| 680 | + } |
718 | 681 | return false; |
719 | 682 | } |
720 | 683 | |
721 | 684 | // Initialise timeout |
722 | | - stream_set_timeout($sock, $this->_timeout_seconds, $this->_timeout_microseconds); |
| 685 | + stream_set_timeout( $sock, $this->_timeout_seconds, $this->_timeout_microseconds ); |
723 | 686 | |
724 | 687 | return true; |
725 | 688 | } |
— | — | @@ -733,18 +696,16 @@ |
734 | 697 | * |
735 | 698 | * @access private |
736 | 699 | */ |
737 | | - function _dead_sock ($sock) |
738 | | - { |
739 | | - $host = array_search($sock, $this->_cache_sock); |
740 | | - $this->_dead_host($host); |
| 700 | + function _dead_sock( $sock ) { |
| 701 | + $host = array_search( $sock, $this->_cache_sock ); |
| 702 | + $this->_dead_host( $host ); |
741 | 703 | } |
742 | 704 | |
743 | | - function _dead_host ($host) |
744 | | - { |
745 | | - @list ($ip, /* $port */) = explode(":", $host); |
746 | | - $this->_host_dead[$ip] = time() + 30 + intval(rand(0, 10)); |
| 705 | + function _dead_host( $host ) { |
| 706 | + @list( $ip, /* $port */) = explode( ':', $host ); |
| 707 | + $this->_host_dead[$ip] = time() + 30 + intval( rand( 0, 10 ) ); |
747 | 708 | $this->_host_dead[$host] = $this->_host_dead[$ip]; |
748 | | - unset($this->_cache_sock[$host]); |
| 709 | + unset( $this->_cache_sock[$host] ); |
749 | 710 | } |
750 | 711 | |
751 | 712 | // }}} |
— | — | @@ -758,44 +719,40 @@ |
759 | 720 | * @return mixed resource on success, false on failure |
760 | 721 | * @access private |
761 | 722 | */ |
762 | | - function get_sock ($key) |
763 | | - { |
764 | | - if (!$this->_active) |
| 723 | + function get_sock( $key ) { |
| 724 | + if ( !$this->_active ) { |
765 | 725 | return false; |
| 726 | + } |
766 | 727 | |
767 | | - if ($this->_single_sock !== null) { |
768 | | - $this->_flush_read_buffer($this->_single_sock); |
769 | | - return $this->sock_to_host($this->_single_sock); |
| 728 | + if ( $this->_single_sock !== null ) { |
| 729 | + $this->_flush_read_buffer( $this->_single_sock ); |
| 730 | + return $this->sock_to_host( $this->_single_sock ); |
770 | 731 | } |
771 | 732 | |
772 | | - $hv = is_array($key) ? intval($key[0]) : $this->_hashfunc($key); |
| 733 | + $hv = is_array( $key ) ? intval( $key[0] ) : $this->_hashfunc( $key ); |
773 | 734 | |
774 | | - if ($this->_buckets === null) |
775 | | - { |
776 | | - foreach ($this->_servers as $v) |
777 | | - { |
778 | | - if (is_array($v)) |
779 | | - { |
780 | | - for ($i=0; $i<$v[1]; $i++) |
| 735 | + if ( $this->_buckets === null ) { |
| 736 | + foreach ( $this->_servers as $v ) { |
| 737 | + if ( is_array( $v ) ) { |
| 738 | + for( $i = 0; $i < $v[1]; $i++ ) { |
781 | 739 | $bu[] = $v[0]; |
782 | | - } else |
783 | | - { |
| 740 | + } |
| 741 | + } else { |
784 | 742 | $bu[] = $v; |
785 | 743 | } |
786 | 744 | } |
787 | 745 | $this->_buckets = $bu; |
788 | | - $this->_bucketcount = count($bu); |
| 746 | + $this->_bucketcount = count( $bu ); |
789 | 747 | } |
790 | 748 | |
791 | | - $realkey = is_array($key) ? $key[1] : $key; |
792 | | - for ($tries = 0; $tries<20; $tries++) |
793 | | - { |
| 749 | + $realkey = is_array( $key ) ? $key[1] : $key; |
| 750 | + for( $tries = 0; $tries < 20; $tries++ ) { |
794 | 751 | $host = $this->_buckets[$hv % $this->_bucketcount]; |
795 | | - $sock = $this->sock_to_host($host); |
796 | | - if (is_resource($sock)) { |
797 | | - $this->_flush_read_buffer($sock); |
| 752 | + $sock = $this->sock_to_host( $host ); |
| 753 | + if ( is_resource( $sock ) ) { |
| 754 | + $this->_flush_read_buffer( $sock ); |
798 | 755 | return $sock; |
799 | | - } |
| 756 | + } |
800 | 757 | $hv = $this->_hashfunc( $hv . $realkey ); |
801 | 758 | } |
802 | 759 | |
— | — | @@ -813,12 +770,11 @@ |
814 | 771 | * @return integer Hash value |
815 | 772 | * @access private |
816 | 773 | */ |
817 | | - function _hashfunc ($key) |
818 | | - { |
| 774 | + function _hashfunc( $key ) { |
819 | 775 | # Hash function must on [0,0x7ffffff] |
820 | 776 | # We take the first 31 bits of the MD5 hash, which unlike the hash |
821 | 777 | # function used in a previous version of this client, works |
822 | | - return hexdec(substr(md5($key),0,8)) & 0x7fffffff; |
| 778 | + return hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff; |
823 | 779 | } |
824 | 780 | |
825 | 781 | // }}} |
— | — | @@ -834,24 +790,27 @@ |
835 | 791 | * @return integer New value of $key |
836 | 792 | * @access private |
837 | 793 | */ |
838 | | - function _incrdecr ($cmd, $key, $amt=1) |
839 | | - { |
840 | | - if (!$this->_active) |
| 794 | + function _incrdecr( $cmd, $key, $amt = 1 ) { |
| 795 | + if ( !$this->_active ) { |
841 | 796 | return null; |
| 797 | + } |
842 | 798 | |
843 | | - $sock = $this->get_sock($key); |
844 | | - if (!is_resource($sock)) |
| 799 | + $sock = $this->get_sock( $key ); |
| 800 | + if ( !is_resource( $sock ) ) { |
845 | 801 | return null; |
| 802 | + } |
846 | 803 | |
847 | | - $key = is_array($key) ? $key[1] : $key; |
| 804 | + $key = is_array( $key ) ? $key[1] : $key; |
848 | 805 | @$this->stats[$cmd]++; |
849 | | - if (!$this->_safe_fwrite($sock, "$cmd $key $amt\r\n")) |
850 | | - return $this->_dead_sock($sock); |
| 806 | + if ( !$this->_safe_fwrite( $sock, "$cmd $key $amt\r\n" ) ) { |
| 807 | + return $this->_dead_sock( $sock ); |
| 808 | + } |
851 | 809 | |
852 | | - $line = fgets($sock); |
| 810 | + $line = fgets( $sock ); |
853 | 811 | $match = array(); |
854 | | - if (!preg_match('/^(\d+)/', $line, $match)) |
| 812 | + if ( !preg_match( '/^(\d+)/', $line, $match ) ) { |
855 | 813 | return null; |
| 814 | + } |
856 | 815 | return $match[1]; |
857 | 816 | } |
858 | 817 | |
— | — | @@ -866,53 +825,50 @@ |
867 | 826 | * |
868 | 827 | * @access private |
869 | 828 | */ |
870 | | - function _load_items ($sock, &$ret) |
871 | | - { |
872 | | - while (1) |
873 | | - { |
874 | | - $decl = fgets($sock); |
875 | | - if ($decl == "END\r\n") |
876 | | - { |
| 829 | + function _load_items( $sock, &$ret ) { |
| 830 | + while ( 1 ) { |
| 831 | + $decl = fgets( $sock ); |
| 832 | + if ( $decl == "END\r\n" ) { |
877 | 833 | return true; |
878 | | - } elseif (preg_match('/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match)) |
879 | | - { |
880 | | - list($rkey, $flags, $len) = array($match[1], $match[2], $match[3]); |
881 | | - $bneed = $len+2; |
| 834 | + } elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match ) ) { |
| 835 | + list( $rkey, $flags, $len ) = array( $match[1], $match[2], $match[3] ); |
| 836 | + $bneed = $len + 2; |
882 | 837 | $offset = 0; |
883 | 838 | |
884 | | - while ($bneed > 0) |
885 | | - { |
886 | | - $data = fread($sock, $bneed); |
887 | | - $n = strlen($data); |
888 | | - if ($n == 0) |
| 839 | + while ( $bneed > 0 ) { |
| 840 | + $data = fread( $sock, $bneed ); |
| 841 | + $n = strlen( $data ); |
| 842 | + if ( $n == 0 ) { |
889 | 843 | break; |
| 844 | + } |
890 | 845 | $offset += $n; |
891 | 846 | $bneed -= $n; |
892 | 847 | @$ret[$rkey] .= $data; |
893 | 848 | } |
894 | 849 | |
895 | | - if ($offset != $len+2) |
896 | | - { |
| 850 | + if ( $offset != $len + 2 ) { |
897 | 851 | // Something is borked! |
898 | | - if ($this->_debug) |
899 | | - $this->_debugprint(sprintf("Something is borked! key %s expecting %d got %d length\n", $rkey, $len+2, $offset)); |
| 852 | + if ( $this->_debug ) { |
| 853 | + $this->_debugprint( sprintf( "Something is borked! key %s expecting %d got %d length\n", $rkey, $len + 2, $offset ) ); |
| 854 | + } |
900 | 855 | |
901 | | - unset($ret[$rkey]); |
902 | | - $this->_close_sock($sock); |
| 856 | + unset( $ret[$rkey] ); |
| 857 | + $this->_close_sock( $sock ); |
903 | 858 | return false; |
904 | 859 | } |
905 | 860 | |
906 | | - if ($this->_have_zlib && $flags & self::COMPRESSED) |
907 | | - $ret[$rkey] = gzuncompress($ret[$rkey]); |
| 861 | + if ( $this->_have_zlib && $flags & self::COMPRESSED ) { |
| 862 | + $ret[$rkey] = gzuncompress( $ret[$rkey] ); |
| 863 | + } |
908 | 864 | |
909 | | - $ret[$rkey] = rtrim($ret[$rkey]); |
| 865 | + $ret[$rkey] = rtrim( $ret[$rkey] ); |
910 | 866 | |
911 | | - if ($flags & self::SERIALIZED) |
912 | | - $ret[$rkey] = unserialize($ret[$rkey]); |
| 867 | + if ( $flags & self::SERIALIZED ) { |
| 868 | + $ret[$rkey] = unserialize( $ret[$rkey] ); |
| 869 | + } |
913 | 870 | |
914 | | - } else |
915 | | - { |
916 | | - $this->_debugprint("Error parsing memcached response\n"); |
| 871 | + } else { |
| 872 | + $this->_debugprint( "Error parsing memcached response\n" ); |
917 | 873 | return 0; |
918 | 874 | } |
919 | 875 | } |
— | — | @@ -932,55 +888,57 @@ |
933 | 889 | * @return boolean |
934 | 890 | * @access private |
935 | 891 | */ |
936 | | - function _set ($cmd, $key, $val, $exp) |
937 | | - { |
938 | | - if (!$this->_active) |
| 892 | + function _set( $cmd, $key, $val, $exp ) { |
| 893 | + if ( !$this->_active ) { |
939 | 894 | return false; |
| 895 | + } |
940 | 896 | |
941 | | - $sock = $this->get_sock($key); |
942 | | - if (!is_resource($sock)) |
| 897 | + $sock = $this->get_sock( $key ); |
| 898 | + if ( !is_resource( $sock ) ) { |
943 | 899 | return false; |
| 900 | + } |
944 | 901 | |
945 | 902 | @$this->stats[$cmd]++; |
946 | 903 | |
947 | 904 | $flags = 0; |
948 | 905 | |
949 | | - if (!is_scalar($val)) |
950 | | - { |
951 | | - $val = serialize($val); |
| 906 | + if ( !is_scalar( $val ) ) { |
| 907 | + $val = serialize( $val ); |
952 | 908 | $flags |= self::SERIALIZED; |
953 | | - if ($this->_debug) |
954 | | - $this->_debugprint(sprintf("client: serializing data as it is not scalar\n")); |
| 909 | + if ( $this->_debug ) { |
| 910 | + $this->_debugprint( sprintf( "client: serializing data as it is not scalar\n" ) ); |
| 911 | + } |
955 | 912 | } |
956 | 913 | |
957 | | - $len = strlen($val); |
| 914 | + $len = strlen( $val ); |
958 | 915 | |
959 | | - if ($this->_have_zlib && $this->_compress_enable && |
960 | | - $this->_compress_threshold && $len >= $this->_compress_threshold) |
| 916 | + if ( $this->_have_zlib && $this->_compress_enable && |
| 917 | + $this->_compress_threshold && $len >= $this->_compress_threshold ) |
961 | 918 | { |
962 | | - $c_val = gzcompress($val, 9); |
963 | | - $c_len = strlen($c_val); |
| 919 | + $c_val = gzcompress( $val, 9 ); |
| 920 | + $c_len = strlen( $c_val ); |
964 | 921 | |
965 | | - if ($c_len < $len*(1 - self::COMPRESSION_SAVINGS)) |
966 | | - { |
967 | | - if ($this->_debug) |
968 | | - $this->_debugprint(sprintf("client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len)); |
| 922 | + if ( $c_len < $len * ( 1 - self::COMPRESSION_SAVINGS ) ) { |
| 923 | + if ( $this->_debug ) { |
| 924 | + $this->_debugprint( sprintf( "client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len ) ); |
| 925 | + } |
969 | 926 | $val = $c_val; |
970 | 927 | $len = $c_len; |
971 | 928 | $flags |= self::COMPRESSED; |
972 | 929 | } |
973 | 930 | } |
974 | | - if (!$this->_safe_fwrite($sock, "$cmd $key $flags $exp $len\r\n$val\r\n")) |
975 | | - return $this->_dead_sock($sock); |
| 931 | + if ( !$this->_safe_fwrite( $sock, "$cmd $key $flags $exp $len\r\n$val\r\n" ) ) { |
| 932 | + return $this->_dead_sock( $sock ); |
| 933 | + } |
976 | 934 | |
977 | | - $line = trim(fgets($sock)); |
| 935 | + $line = trim( fgets( $sock ) ); |
978 | 936 | |
979 | | - if ($this->_debug) |
980 | | - { |
981 | | - $this->_debugprint(sprintf("%s %s (%s)\n", $cmd, $key, $line)); |
| 937 | + if ( $this->_debug ) { |
| 938 | + $this->_debugprint( sprintf( "%s %s (%s)\n", $cmd, $key, $line ) ); |
982 | 939 | } |
983 | | - if ($line == "STORED") |
| 940 | + if ( $line == "STORED" ) { |
984 | 941 | return true; |
| 942 | + } |
985 | 943 | return false; |
986 | 944 | } |
987 | 945 | |
— | — | @@ -995,31 +953,34 @@ |
996 | 954 | * @return mixed IO Stream or false |
997 | 955 | * @access private |
998 | 956 | */ |
999 | | - function sock_to_host ($host) |
1000 | | - { |
1001 | | - if (isset($this->_cache_sock[$host])) |
| 957 | + function sock_to_host( $host ) { |
| 958 | + if ( isset( $this->_cache_sock[$host] ) ) { |
1002 | 959 | return $this->_cache_sock[$host]; |
| 960 | + } |
1003 | 961 | |
1004 | 962 | $sock = null; |
1005 | 963 | $now = time(); |
1006 | | - list ($ip, /* $port */) = explode (":", $host); |
1007 | | - if (isset($this->_host_dead[$host]) && $this->_host_dead[$host] > $now || |
1008 | | - isset($this->_host_dead[$ip]) && $this->_host_dead[$ip] > $now) |
| 964 | + list( $ip, /* $port */) = explode( ':', $host ); |
| 965 | + if ( isset( $this->_host_dead[$host] ) && $this->_host_dead[$host] > $now || |
| 966 | + isset( $this->_host_dead[$ip] ) && $this->_host_dead[$ip] > $now |
| 967 | + ) { |
1009 | 968 | return null; |
| 969 | + } |
1010 | 970 | |
1011 | | - if (!$this->_connect_sock($sock, $host)) |
1012 | | - return $this->_dead_host($host); |
| 971 | + if ( !$this->_connect_sock( $sock, $host ) ) { |
| 972 | + return $this->_dead_host( $host ); |
| 973 | + } |
1013 | 974 | |
1014 | 975 | // Do not buffer writes |
1015 | | - stream_set_write_buffer($sock, 0); |
| 976 | + stream_set_write_buffer( $sock, 0 ); |
1016 | 977 | |
1017 | 978 | $this->_cache_sock[$host] = $sock; |
1018 | 979 | |
1019 | 980 | return $this->_cache_sock[$host]; |
1020 | 981 | } |
1021 | 982 | |
1022 | | - function _debugprint($str){ |
1023 | | - print($str); |
| 983 | + function _debugprint( $str ) { |
| 984 | + print( $str ); |
1024 | 985 | } |
1025 | 986 | |
1026 | 987 | /** |
— | — | @@ -1028,21 +989,21 @@ |
1029 | 990 | * @return bool false on failure, true on success |
1030 | 991 | */ |
1031 | 992 | /* |
1032 | | - function _safe_fwrite($f, $buf, $len = false) { |
1033 | | - stream_set_blocking($f, 0); |
| 993 | + function _safe_fwrite( $f, $buf, $len = false ) { |
| 994 | + stream_set_blocking( $f, 0 ); |
1034 | 995 | |
1035 | | - if ($len === false) { |
1036 | | - wfDebug("Writing " . strlen( $buf ) . " bytes\n"); |
1037 | | - $bytesWritten = fwrite($f, $buf); |
| 996 | + if ( $len === false ) { |
| 997 | + wfDebug( "Writing " . strlen( $buf ) . " bytes\n" ); |
| 998 | + $bytesWritten = fwrite( $f, $buf ); |
1038 | 999 | } else { |
1039 | | - wfDebug("Writing $len bytes\n"); |
1040 | | - $bytesWritten = fwrite($f, $buf, $len); |
| 1000 | + wfDebug( "Writing $len bytes\n" ); |
| 1001 | + $bytesWritten = fwrite( $f, $buf, $len ); |
1041 | 1002 | } |
1042 | | - $n = stream_select($r=NULL, $w = array($f), $e = NULL, 10, 0); |
1043 | | - # $this->_timeout_seconds, $this->_timeout_microseconds); |
| 1003 | + $n = stream_select( $r = null, $w = array( $f ), $e = null, 10, 0 ); |
| 1004 | + # $this->_timeout_seconds, $this->_timeout_microseconds ); |
1044 | 1005 | |
1045 | | - wfDebug("stream_select returned $n\n"); |
1046 | | - stream_set_blocking($f, 1); |
| 1006 | + wfDebug( "stream_select returned $n\n" ); |
| 1007 | + stream_set_blocking( $f, 1 ); |
1047 | 1008 | return $n == 1; |
1048 | 1009 | return $bytesWritten; |
1049 | 1010 | }*/ |
— | — | @@ -1050,11 +1011,11 @@ |
1051 | 1012 | /** |
1052 | 1013 | * Original behaviour |
1053 | 1014 | */ |
1054 | | - function _safe_fwrite($f, $buf, $len = false) { |
1055 | | - if ($len === false) { |
1056 | | - $bytesWritten = fwrite($f, $buf); |
| 1015 | + function _safe_fwrite( $f, $buf, $len = false ) { |
| 1016 | + if ( $len === false ) { |
| 1017 | + $bytesWritten = fwrite( $f, $buf ); |
1057 | 1018 | } else { |
1058 | | - $bytesWritten = fwrite($f, $buf, $len); |
| 1019 | + $bytesWritten = fwrite( $f, $buf, $len ); |
1059 | 1020 | } |
1060 | 1021 | return $bytesWritten; |
1061 | 1022 | } |
— | — | @@ -1062,14 +1023,14 @@ |
1063 | 1024 | /** |
1064 | 1025 | * Flush the read buffer of a stream |
1065 | 1026 | */ |
1066 | | - function _flush_read_buffer($f) { |
1067 | | - if (!is_resource($f)) { |
| 1027 | + function _flush_read_buffer( $f ) { |
| 1028 | + if ( !is_resource( $f ) ) { |
1068 | 1029 | return; |
1069 | 1030 | } |
1070 | | - $n = stream_select($r=array($f), $w = null, $e = null, 0, 0); |
1071 | | - while ($n == 1 && !feof($f)) { |
1072 | | - fread($f, 1024); |
1073 | | - $n = stream_select($r=array($f), $w = null, $e = null, 0, 0); |
| 1031 | + $n = stream_select( $r = array( $f ), $w = null, $e = null, 0, 0 ); |
| 1032 | + while ( $n == 1 && !feof( $f ) ) { |
| 1033 | + fread( $f, 1024 ); |
| 1034 | + $n = stream_select( $r = array( $f ), $w = null, $e = null, 0, 0 ); |
1074 | 1035 | } |
1075 | 1036 | } |
1076 | 1037 | |