Move up devunt's name to Developers
[lhc/web/wiklou.git] / includes / objectcache / MemcachedClient.php
1 <?php
2 // @codingStandardsIgnoreFile It's an external lib and it isn't. Let's not bother.
3 /**
4 * Memcached client for PHP.
5 *
6 * +---------------------------------------------------------------------------+
7 * | memcached client, PHP |
8 * +---------------------------------------------------------------------------+
9 * | Copyright (c) 2003 Ryan T. Dean <rtdean@cytherianage.net> |
10 * | All rights reserved. |
11 * | |
12 * | Redistribution and use in source and binary forms, with or without |
13 * | modification, are permitted provided that the following conditions |
14 * | are met: |
15 * | |
16 * | 1. Redistributions of source code must retain the above copyright |
17 * | notice, this list of conditions and the following disclaimer. |
18 * | 2. Redistributions in binary form must reproduce the above copyright |
19 * | notice, this list of conditions and the following disclaimer in the |
20 * | documentation and/or other materials provided with the distribution. |
21 * | |
22 * | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
23 * | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
24 * | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
25 * | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
26 * | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
27 * | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
28 * | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
29 * | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
30 * | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
31 * | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 * +---------------------------------------------------------------------------+
33 * | Author: Ryan T. Dean <rtdean@cytherianage.net> |
34 * | Heavily influenced by the Perl memcached client by Brad Fitzpatrick. |
35 * | Permission granted by Brad Fitzpatrick for relicense of ported Perl |
36 * | client logic under 2-clause BSD license. |
37 * +---------------------------------------------------------------------------+
38 *
39 * @file
40 * $TCAnet$
41 */
42
43 /**
44 * This is the PHP client for memcached - a distributed memory cache daemon.
45 * More information is available at http://www.danga.com/memcached/
46 *
47 * Usage example:
48 *
49 * require_once 'memcached.php';
50 *
51 * $mc = new MWMemcached(array(
52 * 'servers' => array('127.0.0.1:10000',
53 * array('192.0.0.1:10010', 2),
54 * '127.0.0.1:10020'),
55 * 'debug' => false,
56 * 'compress_threshold' => 10240,
57 * 'persistent' => true));
58 *
59 * $mc->add( 'key', array( 'some', 'array' ) );
60 * $mc->replace( 'key', 'some random string' );
61 * $val = $mc->get( 'key' );
62 *
63 * @author Ryan T. Dean <rtdean@cytherianage.net>
64 * @version 0.1.2
65 */
66
67 use Psr\Log\LoggerInterface;
68 use Psr\Log\NullLogger;
69
70 // {{{ requirements
71 // }}}
72
73 // {{{ class MWMemcached
74 /**
75 * memcached client class implemented using (p)fsockopen()
76 *
77 * @author Ryan T. Dean <rtdean@cytherianage.net>
78 * @ingroup Cache
79 */
80 class MWMemcached {
81 // {{{ properties
82 // {{{ public
83
84 // {{{ constants
85 // {{{ flags
86
87 /**
88 * Flag: indicates data is serialized
89 */
90 const SERIALIZED = 1;
91
92 /**
93 * Flag: indicates data is compressed
94 */
95 const COMPRESSED = 2;
96
97 // }}}
98
99 /**
100 * Minimum savings to store data compressed
101 */
102 const COMPRESSION_SAVINGS = 0.20;
103
104 // }}}
105
106 /**
107 * Command statistics
108 *
109 * @var array
110 * @access public
111 */
112 public $stats;
113
114 // }}}
115 // {{{ private
116
117 /**
118 * Cached Sockets that are connected
119 *
120 * @var array
121 * @access private
122 */
123 public $_cache_sock;
124
125 /**
126 * Current debug status; 0 - none to 9 - profiling
127 *
128 * @var bool
129 * @access private
130 */
131 public $_debug;
132
133 /**
134 * Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'
135 *
136 * @var array
137 * @access private
138 */
139 public $_host_dead;
140
141 /**
142 * Is compression available?
143 *
144 * @var bool
145 * @access private
146 */
147 public $_have_zlib;
148
149 /**
150 * Do we want to use compression?
151 *
152 * @var bool
153 * @access private
154 */
155 public $_compress_enable;
156
157 /**
158 * At how many bytes should we compress?
159 *
160 * @var int
161 * @access private
162 */
163 public $_compress_threshold;
164
165 /**
166 * Are we using persistent links?
167 *
168 * @var bool
169 * @access private
170 */
171 public $_persistent;
172
173 /**
174 * If only using one server; contains ip:port to connect to
175 *
176 * @var string
177 * @access private
178 */
179 public $_single_sock;
180
181 /**
182 * Array containing ip:port or array(ip:port, weight)
183 *
184 * @var array
185 * @access private
186 */
187 public $_servers;
188
189 /**
190 * Our bit buckets
191 *
192 * @var array
193 * @access private
194 */
195 public $_buckets;
196
197 /**
198 * Total # of bit buckets we have
199 *
200 * @var int
201 * @access private
202 */
203 public $_bucketcount;
204
205 /**
206 * # of total servers we have
207 *
208 * @var int
209 * @access private
210 */
211 public $_active;
212
213 /**
214 * Stream timeout in seconds. Applies for example to fread()
215 *
216 * @var int
217 * @access private
218 */
219 public $_timeout_seconds;
220
221 /**
222 * Stream timeout in microseconds
223 *
224 * @var int
225 * @access private
226 */
227 public $_timeout_microseconds;
228
229 /**
230 * Connect timeout in seconds
231 */
232 public $_connect_timeout;
233
234 /**
235 * Number of connection attempts for each server
236 */
237 public $_connect_attempts;
238
239 /**
240 * @var LoggerInterface
241 */
242 private $_logger;
243
244 // }}}
245 // }}}
246 // {{{ methods
247 // {{{ public functions
248 // {{{ memcached()
249
250 /**
251 * Memcache initializer
252 *
253 * @param array $args Associative array of settings
254 *
255 * @return mixed
256 */
257 public function __construct( $args ) {
258 $this->set_servers( isset( $args['servers'] ) ? $args['servers'] : array() );
259 $this->_debug = isset( $args['debug'] ) ? $args['debug'] : false;
260 $this->stats = array();
261 $this->_compress_threshold = isset( $args['compress_threshold'] ) ? $args['compress_threshold'] : 0;
262 $this->_persistent = isset( $args['persistent'] ) ? $args['persistent'] : false;
263 $this->_compress_enable = true;
264 $this->_have_zlib = function_exists( 'gzcompress' );
265
266 $this->_cache_sock = array();
267 $this->_host_dead = array();
268
269 $this->_timeout_seconds = 0;
270 $this->_timeout_microseconds = isset( $args['timeout'] ) ? $args['timeout'] : 500000;
271
272 $this->_connect_timeout = isset( $args['connect_timeout'] ) ? $args['connect_timeout'] : 0.1;
273 $this->_connect_attempts = 2;
274
275 $this->_logger = isset( $args['logger'] ) ? $args['logger'] : new NullLogger();
276 }
277
278 // }}}
279 // {{{ add()
280
281 /**
282 * Adds a key/value to the memcache server if one isn't already set with
283 * that key
284 *
285 * @param string $key Key to set with data
286 * @param mixed $val Value to store
287 * @param int $exp (optional) Expiration time. This can be a number of seconds
288 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
289 * longer must be the timestamp of the time at which the mapping should expire. It
290 * is safe to use timestamps in all cases, regardless of expiration
291 * eg: strtotime("+3 hour")
292 *
293 * @return bool
294 */
295 public function add( $key, $val, $exp = 0 ) {
296 return $this->_set( 'add', $key, $val, $exp );
297 }
298
299 // }}}
300 // {{{ decr()
301
302 /**
303 * Decrease a value stored on the memcache server
304 *
305 * @param string $key Key to decrease
306 * @param int $amt (optional) amount to decrease
307 *
308 * @return mixed False on failure, value on success
309 */
310 public function decr( $key, $amt = 1 ) {
311 return $this->_incrdecr( 'decr', $key, $amt );
312 }
313
314 // }}}
315 // {{{ delete()
316
317 /**
318 * Deletes a key from the server, optionally after $time
319 *
320 * @param string $key Key to delete
321 * @param int $time (optional) how long to wait before deleting
322 *
323 * @return bool True on success, false on failure
324 */
325 public function delete( $key, $time = 0 ) {
326 if ( !$this->_active ) {
327 return false;
328 }
329
330 $sock = $this->get_sock( $key );
331 if ( !is_resource( $sock ) ) {
332 return false;
333 }
334
335 $key = is_array( $key ) ? $key[1] : $key;
336
337 if ( isset( $this->stats['delete'] ) ) {
338 $this->stats['delete']++;
339 } else {
340 $this->stats['delete'] = 1;
341 }
342 $cmd = "delete $key $time\r\n";
343 if ( !$this->_fwrite( $sock, $cmd ) ) {
344 return false;
345 }
346 $res = $this->_fgets( $sock );
347
348 if ( $this->_debug ) {
349 $this->_debugprint( sprintf( "MemCache: delete %s (%s)\n", $key, $res ) );
350 }
351
352 if ( $res == "DELETED" || $res == "NOT_FOUND" ) {
353 return true;
354 }
355
356 return false;
357 }
358
359 /**
360 * @param string $key
361 * @param int $timeout
362 * @return bool
363 */
364 public function lock( $key, $timeout = 0 ) {
365 /* stub */
366 return true;
367 }
368
369 /**
370 * @param string $key
371 * @return bool
372 */
373 public function unlock( $key ) {
374 /* stub */
375 return true;
376 }
377
378 // }}}
379 // {{{ disconnect_all()
380
381 /**
382 * Disconnects all connected sockets
383 */
384 public function disconnect_all() {
385 foreach ( $this->_cache_sock as $sock ) {
386 fclose( $sock );
387 }
388
389 $this->_cache_sock = array();
390 }
391
392 // }}}
393 // {{{ enable_compress()
394
395 /**
396 * Enable / Disable compression
397 *
398 * @param bool $enable True to enable, false to disable
399 */
400 public function enable_compress( $enable ) {
401 $this->_compress_enable = $enable;
402 }
403
404 // }}}
405 // {{{ forget_dead_hosts()
406
407 /**
408 * Forget about all of the dead hosts
409 */
410 public function forget_dead_hosts() {
411 $this->_host_dead = array();
412 }
413
414 // }}}
415 // {{{ get()
416
417 /**
418 * Retrieves the value associated with the key from the memcache server
419 *
420 * @param array|string $key key to retrieve
421 * @param float $casToken [optional]
422 *
423 * @return mixed
424 */
425 public function get( $key, &$casToken = null ) {
426
427 if ( $this->_debug ) {
428 $this->_debugprint( "get($key)\n" );
429 }
430
431 if ( !is_array( $key ) && strval( $key ) === '' ) {
432 $this->_debugprint( "Skipping key which equals to an empty string" );
433 return false;
434 }
435
436 if ( !$this->_active ) {
437 return false;
438 }
439
440 $sock = $this->get_sock( $key );
441
442 if ( !is_resource( $sock ) ) {
443 return false;
444 }
445
446 $key = is_array( $key ) ? $key[1] : $key;
447 if ( isset( $this->stats['get'] ) ) {
448 $this->stats['get']++;
449 } else {
450 $this->stats['get'] = 1;
451 }
452
453 $cmd = "gets $key\r\n";
454 if ( !$this->_fwrite( $sock, $cmd ) ) {
455 return false;
456 }
457
458 $val = array();
459 $this->_load_items( $sock, $val, $casToken );
460
461 if ( $this->_debug ) {
462 foreach ( $val as $k => $v ) {
463 $this->_debugprint( sprintf( "MemCache: sock %s got %s\n", serialize( $sock ), $k ) );
464 }
465 }
466
467 $value = false;
468 if ( isset( $val[$key] ) ) {
469 $value = $val[$key];
470 }
471 return $value;
472 }
473
474 // }}}
475 // {{{ get_multi()
476
477 /**
478 * Get multiple keys from the server(s)
479 *
480 * @param array $keys Keys to retrieve
481 *
482 * @return array
483 */
484 public function get_multi( $keys ) {
485 if ( !$this->_active ) {
486 return false;
487 }
488
489 if ( isset( $this->stats['get_multi'] ) ) {
490 $this->stats['get_multi']++;
491 } else {
492 $this->stats['get_multi'] = 1;
493 }
494 $sock_keys = array();
495 $socks = array();
496 foreach ( $keys as $key ) {
497 $sock = $this->get_sock( $key );
498 if ( !is_resource( $sock ) ) {
499 continue;
500 }
501 $key = is_array( $key ) ? $key[1] : $key;
502 if ( !isset( $sock_keys[$sock] ) ) {
503 $sock_keys[intval( $sock )] = array();
504 $socks[] = $sock;
505 }
506 $sock_keys[intval( $sock )][] = $key;
507 }
508
509 $gather = array();
510 // Send out the requests
511 foreach ( $socks as $sock ) {
512 $cmd = 'gets';
513 foreach ( $sock_keys[intval( $sock )] as $key ) {
514 $cmd .= ' ' . $key;
515 }
516 $cmd .= "\r\n";
517
518 if ( $this->_fwrite( $sock, $cmd ) ) {
519 $gather[] = $sock;
520 }
521 }
522
523 // Parse responses
524 $val = array();
525 foreach ( $gather as $sock ) {
526 $this->_load_items( $sock, $val, $casToken );
527 }
528
529 if ( $this->_debug ) {
530 foreach ( $val as $k => $v ) {
531 $this->_debugprint( sprintf( "MemCache: got %s\n", $k ) );
532 }
533 }
534
535 return $val;
536 }
537
538 // }}}
539 // {{{ incr()
540
541 /**
542 * Increments $key (optionally) by $amt
543 *
544 * @param string $key Key to increment
545 * @param int $amt (optional) amount to increment
546 *
547 * @return int|null Null if the key does not exist yet (this does NOT
548 * create new mappings if the key does not exist). If the key does
549 * exist, this returns the new value for that key.
550 */
551 public function incr( $key, $amt = 1 ) {
552 return $this->_incrdecr( 'incr', $key, $amt );
553 }
554
555 // }}}
556 // {{{ replace()
557
558 /**
559 * Overwrites an existing value for key; only works if key is already set
560 *
561 * @param string $key Key to set value as
562 * @param mixed $value Value to store
563 * @param int $exp (optional) Expiration time. This can be a number of seconds
564 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
565 * longer must be the timestamp of the time at which the mapping should expire. It
566 * is safe to use timestamps in all cases, regardless of exipration
567 * eg: strtotime("+3 hour")
568 *
569 * @return bool
570 */
571 public function replace( $key, $value, $exp = 0 ) {
572 return $this->_set( 'replace', $key, $value, $exp );
573 }
574
575 // }}}
576 // {{{ run_command()
577
578 /**
579 * Passes through $cmd to the memcache server connected by $sock; returns
580 * output as an array (null array if no output)
581 *
582 * @param Resource $sock Socket to send command on
583 * @param string $cmd Command to run
584 *
585 * @return array Output array
586 */
587 public function run_command( $sock, $cmd ) {
588 if ( !is_resource( $sock ) ) {
589 return array();
590 }
591
592 if ( !$this->_fwrite( $sock, $cmd ) ) {
593 return array();
594 }
595
596 $ret = array();
597 while ( true ) {
598 $res = $this->_fgets( $sock );
599 $ret[] = $res;
600 if ( preg_match( '/^END/', $res ) ) {
601 break;
602 }
603 if ( strlen( $res ) == 0 ) {
604 break;
605 }
606 }
607 return $ret;
608 }
609
610 // }}}
611 // {{{ set()
612
613 /**
614 * Unconditionally sets a key to a given value in the memcache. Returns true
615 * if set successfully.
616 *
617 * @param string $key Key to set value as
618 * @param mixed $value Value to set
619 * @param int $exp (optional) Expiration time. This can be a number of seconds
620 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
621 * longer must be the timestamp of the time at which the mapping should expire. It
622 * is safe to use timestamps in all cases, regardless of exipration
623 * eg: strtotime("+3 hour")
624 *
625 * @return bool True on success
626 */
627 public function set( $key, $value, $exp = 0 ) {
628 return $this->_set( 'set', $key, $value, $exp );
629 }
630
631 // }}}
632 // {{{ cas()
633
634 /**
635 * Sets a key to a given value in the memcache if the current value still corresponds
636 * to a known, given value. Returns true if set successfully.
637 *
638 * @param float $casToken Current known value
639 * @param string $key Key to set value as
640 * @param mixed $value Value to set
641 * @param int $exp (optional) Expiration time. This can be a number of seconds
642 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
643 * longer must be the timestamp of the time at which the mapping should expire. It
644 * is safe to use timestamps in all cases, regardless of exipration
645 * eg: strtotime("+3 hour")
646 *
647 * @return bool True on success
648 */
649 public function cas( $casToken, $key, $value, $exp = 0 ) {
650 return $this->_set( 'cas', $key, $value, $exp, $casToken );
651 }
652
653 // }}}
654 // {{{ set_compress_threshold()
655
656 /**
657 * Sets the compression threshold
658 *
659 * @param int $thresh Threshold to compress if larger than
660 */
661 public function set_compress_threshold( $thresh ) {
662 $this->_compress_threshold = $thresh;
663 }
664
665 // }}}
666 // {{{ set_debug()
667
668 /**
669 * Sets the debug flag
670 *
671 * @param bool $dbg True for debugging, false otherwise
672 *
673 * @see MWMemcached::__construct
674 */
675 public function set_debug( $dbg ) {
676 $this->_debug = $dbg;
677 }
678
679 // }}}
680 // {{{ set_servers()
681
682 /**
683 * Sets the server list to distribute key gets and puts between
684 *
685 * @param array $list Array of servers to connect to
686 *
687 * @see MWMemcached::__construct()
688 */
689 public function set_servers( $list ) {
690 $this->_servers = $list;
691 $this->_active = count( $list );
692 $this->_buckets = null;
693 $this->_bucketcount = 0;
694
695 $this->_single_sock = null;
696 if ( $this->_active == 1 ) {
697 $this->_single_sock = $this->_servers[0];
698 }
699 }
700
701 /**
702 * Sets the timeout for new connections
703 *
704 * @param int $seconds Number of seconds
705 * @param int $microseconds Number of microseconds
706 */
707 public function set_timeout( $seconds, $microseconds ) {
708 $this->_timeout_seconds = $seconds;
709 $this->_timeout_microseconds = $microseconds;
710 }
711
712 // }}}
713 // }}}
714 // {{{ private methods
715 // {{{ _close_sock()
716
717 /**
718 * Close the specified socket
719 *
720 * @param string $sock Socket to close
721 *
722 * @access private
723 */
724 function _close_sock( $sock ) {
725 $host = array_search( $sock, $this->_cache_sock );
726 fclose( $this->_cache_sock[$host] );
727 unset( $this->_cache_sock[$host] );
728 }
729
730 // }}}
731 // {{{ _connect_sock()
732
733 /**
734 * Connects $sock to $host, timing out after $timeout
735 *
736 * @param int $sock Socket to connect
737 * @param string $host Host:IP to connect to
738 *
739 * @return bool
740 * @access private
741 */
742 function _connect_sock( &$sock, $host ) {
743 list( $ip, $port ) = preg_split( '/:(?=\d)/', $host );
744 $sock = false;
745 $timeout = $this->_connect_timeout;
746 $errno = $errstr = null;
747 for ( $i = 0; !$sock && $i < $this->_connect_attempts; $i++ ) {
748 wfSuppressWarnings();
749 if ( $this->_persistent == 1 ) {
750 $sock = pfsockopen( $ip, $port, $errno, $errstr, $timeout );
751 } else {
752 $sock = fsockopen( $ip, $port, $errno, $errstr, $timeout );
753 }
754 wfRestoreWarnings();
755 }
756 if ( !$sock ) {
757 $this->_error_log( "Error connecting to $host: $errstr\n" );
758 $this->_dead_host( $host );
759 return false;
760 }
761
762 // Initialise timeout
763 stream_set_timeout( $sock, $this->_timeout_seconds, $this->_timeout_microseconds );
764
765 // If the connection was persistent, flush the read buffer in case there
766 // was a previous incomplete request on this connection
767 if ( $this->_persistent ) {
768 $this->_flush_read_buffer( $sock );
769 }
770 return true;
771 }
772
773 // }}}
774 // {{{ _dead_sock()
775
776 /**
777 * Marks a host as dead until 30-40 seconds in the future
778 *
779 * @param string $sock Socket to mark as dead
780 *
781 * @access private
782 */
783 function _dead_sock( $sock ) {
784 $host = array_search( $sock, $this->_cache_sock );
785 $this->_dead_host( $host );
786 }
787
788 /**
789 * @param string $host
790 */
791 function _dead_host( $host ) {
792 $parts = explode( ':', $host );
793 $ip = $parts[0];
794 $this->_host_dead[$ip] = time() + 30 + intval( rand( 0, 10 ) );
795 $this->_host_dead[$host] = $this->_host_dead[$ip];
796 unset( $this->_cache_sock[$host] );
797 }
798
799 // }}}
800 // {{{ get_sock()
801
802 /**
803 * get_sock
804 *
805 * @param string $key Key to retrieve value for;
806 *
807 * @return Resource|bool Resource on success, false on failure
808 * @access private
809 */
810 function get_sock( $key ) {
811 if ( !$this->_active ) {
812 return false;
813 }
814
815 if ( $this->_single_sock !== null ) {
816 return $this->sock_to_host( $this->_single_sock );
817 }
818
819 $hv = is_array( $key ) ? intval( $key[0] ) : $this->_hashfunc( $key );
820 if ( $this->_buckets === null ) {
821 $bu = array();
822 foreach ( $this->_servers as $v ) {
823 if ( is_array( $v ) ) {
824 for ( $i = 0; $i < $v[1]; $i++ ) {
825 $bu[] = $v[0];
826 }
827 } else {
828 $bu[] = $v;
829 }
830 }
831 $this->_buckets = $bu;
832 $this->_bucketcount = count( $bu );
833 }
834
835 $realkey = is_array( $key ) ? $key[1] : $key;
836 for ( $tries = 0; $tries < 20; $tries++ ) {
837 $host = $this->_buckets[$hv % $this->_bucketcount];
838 $sock = $this->sock_to_host( $host );
839 if ( is_resource( $sock ) ) {
840 return $sock;
841 }
842 $hv = $this->_hashfunc( $hv . $realkey );
843 }
844
845 return false;
846 }
847
848 // }}}
849 // {{{ _hashfunc()
850
851 /**
852 * Creates a hash integer based on the $key
853 *
854 * @param string $key Key to hash
855 *
856 * @return int Hash value
857 * @access private
858 */
859 function _hashfunc( $key ) {
860 # Hash function must be in [0,0x7ffffff]
861 # We take the first 31 bits of the MD5 hash, which unlike the hash
862 # function used in a previous version of this client, works
863 return hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
864 }
865
866 // }}}
867 // {{{ _incrdecr()
868
869 /**
870 * Perform increment/decriment on $key
871 *
872 * @param string $cmd Command to perform
873 * @param string|array $key Key to perform it on
874 * @param int $amt Amount to adjust
875 *
876 * @return int New value of $key
877 * @access private
878 */
879 function _incrdecr( $cmd, $key, $amt = 1 ) {
880 if ( !$this->_active ) {
881 return null;
882 }
883
884 $sock = $this->get_sock( $key );
885 if ( !is_resource( $sock ) ) {
886 return null;
887 }
888
889 $key = is_array( $key ) ? $key[1] : $key;
890 if ( isset( $this->stats[$cmd] ) ) {
891 $this->stats[$cmd]++;
892 } else {
893 $this->stats[$cmd] = 1;
894 }
895 if ( !$this->_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
896 return null;
897 }
898
899 $line = $this->_fgets( $sock );
900 $match = array();
901 if ( !preg_match( '/^(\d+)/', $line, $match ) ) {
902 return null;
903 }
904 return $match[1];
905 }
906
907 // }}}
908 // {{{ _load_items()
909
910 /**
911 * Load items into $ret from $sock
912 *
913 * @param Resource $sock Socket to read from
914 * @param array $ret returned values
915 * @param float $casToken [optional]
916 * @return bool True for success, false for failure
917 *
918 * @access private
919 */
920 function _load_items( $sock, &$ret, &$casToken = null ) {
921 $results = array();
922
923 while ( 1 ) {
924 $decl = $this->_fgets( $sock );
925
926 if ( $decl === false ) {
927 /*
928 * If nothing can be read, something is wrong because we know exactly when
929 * to stop reading (right after "END") and we return right after that.
930 */
931 return false;
932 } elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+) (\d+)$/', $decl, $match ) ) {
933 /*
934 * Read all data returned. This can be either one or multiple values.
935 * Save all that data (in an array) to be processed later: we'll first
936 * want to continue reading until "END" before doing anything else,
937 * to make sure that we don't leave our client in a state where it's
938 * output is not yet fully read.
939 */
940 $results[] = array(
941 $match[1], // rkey
942 $match[2], // flags
943 $match[3], // len
944 $match[4], // casToken
945 $this->_fread( $sock, $match[3] + 2 ), // data
946 );
947 } elseif ( $decl == "END" ) {
948 if ( count( $results ) == 0 ) {
949 return false;
950 }
951
952 /**
953 * All data has been read, time to process the data and build
954 * meaningful return values.
955 */
956 foreach ( $results as $vars ) {
957 list( $rkey, $flags, $len, $casToken, $data ) = $vars;
958
959 if ( $data === false || substr( $data, -2 ) !== "\r\n" ) {
960 $this->_handle_error( $sock,
961 'line ending missing from data block from $1' );
962 return false;
963 }
964 $data = substr( $data, 0, -2 );
965 $ret[$rkey] = $data;
966
967 if ( $this->_have_zlib && $flags & self::COMPRESSED ) {
968 $ret[$rkey] = gzuncompress( $ret[$rkey] );
969 }
970
971 /*
972 * This unserialize is the exact reason that we only want to
973 * process data after having read until "END" (instead of doing
974 * this right away): "unserialize" can trigger outside code:
975 * in the event that $ret[$rkey] is a serialized object,
976 * unserializing it will trigger __wakeup() if present. If that
977 * function attempted to read from memcached (while we did not
978 * yet read "END"), these 2 calls would collide.
979 */
980 if ( $flags & self::SERIALIZED ) {
981 $ret[$rkey] = unserialize( $ret[$rkey] );
982 }
983 }
984
985 return true;
986 } else {
987 $this->_handle_error( $sock, 'Error parsing response from $1' );
988 return false;
989 }
990 }
991 }
992
993 // }}}
994 // {{{ _set()
995
996 /**
997 * Performs the requested storage operation to the memcache server
998 *
999 * @param string $cmd Command to perform
1000 * @param string $key Key to act on
1001 * @param mixed $val What we need to store
1002 * @param int $exp (optional) Expiration time. This can be a number of seconds
1003 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
1004 * longer must be the timestamp of the time at which the mapping should expire. It
1005 * is safe to use timestamps in all cases, regardless of exipration
1006 * eg: strtotime("+3 hour")
1007 * @param float $casToken [optional]
1008 *
1009 * @return bool
1010 * @access private
1011 */
1012 function _set( $cmd, $key, $val, $exp, $casToken = null ) {
1013 if ( !$this->_active ) {
1014 return false;
1015 }
1016
1017 $sock = $this->get_sock( $key );
1018 if ( !is_resource( $sock ) ) {
1019 return false;
1020 }
1021
1022 if ( isset( $this->stats[$cmd] ) ) {
1023 $this->stats[$cmd]++;
1024 } else {
1025 $this->stats[$cmd] = 1;
1026 }
1027
1028 $flags = 0;
1029
1030 if ( !is_scalar( $val ) ) {
1031 $val = serialize( $val );
1032 $flags |= self::SERIALIZED;
1033 if ( $this->_debug ) {
1034 $this->_debugprint( sprintf( "client: serializing data as it is not scalar\n" ) );
1035 }
1036 }
1037
1038 $len = strlen( $val );
1039
1040 if ( $this->_have_zlib && $this->_compress_enable
1041 && $this->_compress_threshold && $len >= $this->_compress_threshold
1042 ) {
1043 $c_val = gzcompress( $val, 9 );
1044 $c_len = strlen( $c_val );
1045
1046 if ( $c_len < $len * ( 1 - self::COMPRESSION_SAVINGS ) ) {
1047 if ( $this->_debug ) {
1048 $this->_debugprint( sprintf( "client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len ) );
1049 }
1050 $val = $c_val;
1051 $len = $c_len;
1052 $flags |= self::COMPRESSED;
1053 }
1054 }
1055
1056 $command = "$cmd $key $flags $exp $len";
1057 if ( $casToken ) {
1058 $command .= " $casToken";
1059 }
1060
1061 if ( !$this->_fwrite( $sock, "$command\r\n$val\r\n" ) ) {
1062 return false;
1063 }
1064
1065 $line = $this->_fgets( $sock );
1066
1067 if ( $this->_debug ) {
1068 $this->_debugprint( sprintf( "%s %s (%s)\n", $cmd, $key, $line ) );
1069 }
1070 if ( $line == "STORED" ) {
1071 return true;
1072 }
1073 return false;
1074 }
1075
1076 // }}}
1077 // {{{ sock_to_host()
1078
1079 /**
1080 * Returns the socket for the host
1081 *
1082 * @param string $host Host:IP to get socket for
1083 *
1084 * @return Resource|bool IO Stream or false
1085 * @access private
1086 */
1087 function sock_to_host( $host ) {
1088 if ( isset( $this->_cache_sock[$host] ) ) {
1089 return $this->_cache_sock[$host];
1090 }
1091
1092 $sock = null;
1093 $now = time();
1094 list( $ip, /* $port */) = explode( ':', $host );
1095 if ( isset( $this->_host_dead[$host] ) && $this->_host_dead[$host] > $now ||
1096 isset( $this->_host_dead[$ip] ) && $this->_host_dead[$ip] > $now
1097 ) {
1098 return null;
1099 }
1100
1101 if ( !$this->_connect_sock( $sock, $host ) ) {
1102 return null;
1103 }
1104
1105 // Do not buffer writes
1106 stream_set_write_buffer( $sock, 0 );
1107
1108 $this->_cache_sock[$host] = $sock;
1109
1110 return $this->_cache_sock[$host];
1111 }
1112
1113 /**
1114 * @param string $text
1115 */
1116 function _debugprint( $text ) {
1117 $this->_logger->debug( $text );
1118 }
1119
1120 /**
1121 * @param string $text
1122 */
1123 function _error_log( $text ) {
1124 $this->_logger->error( "Memcached error: $text" );
1125 }
1126
1127 /**
1128 * Write to a stream. If there is an error, mark the socket dead.
1129 *
1130 * @param Resource $sock The socket
1131 * @param string $buf The string to write
1132 * @return bool True on success, false on failure
1133 */
1134 function _fwrite( $sock, $buf ) {
1135 $bytesWritten = 0;
1136 $bufSize = strlen( $buf );
1137 while ( $bytesWritten < $bufSize ) {
1138 $result = fwrite( $sock, $buf );
1139 $data = stream_get_meta_data( $sock );
1140 if ( $data['timed_out'] ) {
1141 $this->_handle_error( $sock, 'timeout writing to $1' );
1142 return false;
1143 }
1144 // Contrary to the documentation, fwrite() returns zero on error in PHP 5.3.
1145 if ( $result === false || $result === 0 ) {
1146 $this->_handle_error( $sock, 'error writing to $1' );
1147 return false;
1148 }
1149 $bytesWritten += $result;
1150 }
1151
1152 return true;
1153 }
1154
1155 /**
1156 * Handle an I/O error. Mark the socket dead and log an error.
1157 *
1158 * @param Resource $sock
1159 * @param string $msg
1160 */
1161 function _handle_error( $sock, $msg ) {
1162 $peer = stream_socket_get_name( $sock, true /** remote **/ );
1163 if ( strval( $peer ) === '' ) {
1164 $peer = array_search( $sock, $this->_cache_sock );
1165 if ( $peer === false ) {
1166 $peer = '[unknown host]';
1167 }
1168 }
1169 $msg = str_replace( '$1', $peer, $msg );
1170 $this->_error_log( "$msg\n" );
1171 $this->_dead_sock( $sock );
1172 }
1173
1174 /**
1175 * Read the specified number of bytes from a stream. If there is an error,
1176 * mark the socket dead.
1177 *
1178 * @param Resource $sock The socket
1179 * @param int $len The number of bytes to read
1180 * @return string|bool The string on success, false on failure.
1181 */
1182 function _fread( $sock, $len ) {
1183 $buf = '';
1184 while ( $len > 0 ) {
1185 $result = fread( $sock, $len );
1186 $data = stream_get_meta_data( $sock );
1187 if ( $data['timed_out'] ) {
1188 $this->_handle_error( $sock, 'timeout reading from $1' );
1189 return false;
1190 }
1191 if ( $result === false ) {
1192 $this->_handle_error( $sock, 'error reading buffer from $1' );
1193 return false;
1194 }
1195 if ( $result === '' ) {
1196 // This will happen if the remote end of the socket is shut down
1197 $this->_handle_error( $sock, 'unexpected end of file reading from $1' );
1198 return false;
1199 }
1200 $len -= strlen( $result );
1201 $buf .= $result;
1202 }
1203 return $buf;
1204 }
1205
1206 /**
1207 * Read a line from a stream. If there is an error, mark the socket dead.
1208 * The \r\n line ending is stripped from the response.
1209 *
1210 * @param Resource $sock The socket
1211 * @return string|bool The string on success, false on failure
1212 */
1213 function _fgets( $sock ) {
1214 $result = fgets( $sock );
1215 // fgets() may return a partial line if there is a select timeout after
1216 // a successful recv(), so we have to check for a timeout even if we
1217 // got a string response.
1218 $data = stream_get_meta_data( $sock );
1219 if ( $data['timed_out'] ) {
1220 $this->_handle_error( $sock, 'timeout reading line from $1' );
1221 return false;
1222 }
1223 if ( $result === false ) {
1224 $this->_handle_error( $sock, 'error reading line from $1' );
1225 return false;
1226 }
1227 if ( substr( $result, -2 ) === "\r\n" ) {
1228 $result = substr( $result, 0, -2 );
1229 } elseif ( substr( $result, -1 ) === "\n" ) {
1230 $result = substr( $result, 0, -1 );
1231 } else {
1232 $this->_handle_error( $sock, 'line ending missing in response from $1' );
1233 return false;
1234 }
1235 return $result;
1236 }
1237
1238 /**
1239 * Flush the read buffer of a stream
1240 * @param Resource $f
1241 */
1242 function _flush_read_buffer( $f ) {
1243 if ( !is_resource( $f ) ) {
1244 return;
1245 }
1246 $r = array( $f );
1247 $w = null;
1248 $e = null;
1249 $n = stream_select( $r, $w, $e, 0, 0 );
1250 while ( $n == 1 && !feof( $f ) ) {
1251 fread( $f, 1024 );
1252 $r = array( $f );
1253 $w = null;
1254 $e = null;
1255 $n = stream_select( $r, $w, $e, 0, 0 );
1256 }
1257 }
1258
1259 // }}}
1260 // }}}
1261 // }}}
1262 }
1263
1264 // }}}
1265
1266 class MemCachedClientforWiki extends MWMemcached {
1267 }