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