merging latest master
[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 /**
104 * Command statistics
105 *
106 * @var array
107 * @access public
108 */
109 var $stats;
110
111 // }}}
112 // {{{ private
113
114 /**
115 * Cached Sockets that are connected
116 *
117 * @var array
118 * @access private
119 */
120 var $_cache_sock;
121
122 /**
123 * Current debug status; 0 - none to 9 - profiling
124 *
125 * @var boolean
126 * @access private
127 */
128 var $_debug;
129
130 /**
131 * Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'
132 *
133 * @var array
134 * @access private
135 */
136 var $_host_dead;
137
138 /**
139 * Is compression available?
140 *
141 * @var boolean
142 * @access private
143 */
144 var $_have_zlib;
145
146 /**
147 * Do we want to use compression?
148 *
149 * @var boolean
150 * @access private
151 */
152 var $_compress_enable;
153
154 /**
155 * At how many bytes should we compress?
156 *
157 * @var integer
158 * @access private
159 */
160 var $_compress_threshold;
161
162 /**
163 * Are we using persistent links?
164 *
165 * @var boolean
166 * @access private
167 */
168 var $_persistent;
169
170 /**
171 * If only using one server; contains ip:port to connect to
172 *
173 * @var string
174 * @access private
175 */
176 var $_single_sock;
177
178 /**
179 * Array containing ip:port or array(ip:port, weight)
180 *
181 * @var array
182 * @access private
183 */
184 var $_servers;
185
186 /**
187 * Our bit buckets
188 *
189 * @var array
190 * @access private
191 */
192 var $_buckets;
193
194 /**
195 * Total # of bit buckets we have
196 *
197 * @var integer
198 * @access private
199 */
200 var $_bucketcount;
201
202 /**
203 * # of total servers we have
204 *
205 * @var integer
206 * @access private
207 */
208 var $_active;
209
210 /**
211 * Stream timeout in seconds. Applies for example to fread()
212 *
213 * @var integer
214 * @access private
215 */
216 var $_timeout_seconds;
217
218 /**
219 * Stream timeout in microseconds
220 *
221 * @var integer
222 * @access private
223 */
224 var $_timeout_microseconds;
225
226 /**
227 * Connect timeout in seconds
228 */
229 var $_connect_timeout;
230
231 /**
232 * Number of connection attempts for each server
233 */
234 var $_connect_attempts;
235
236 // }}}
237 // }}}
238 // {{{ methods
239 // {{{ public functions
240 // {{{ memcached()
241
242 /**
243 * Memcache initializer
244 *
245 * @param $args Array Associative array of settings
246 *
247 * @return mixed
248 */
249 public function __construct( $args ) {
250 $this->set_servers( isset( $args['servers'] ) ? $args['servers'] : array() );
251 $this->_debug = isset( $args['debug'] ) ? $args['debug'] : false;
252 $this->stats = array();
253 $this->_compress_threshold = isset( $args['compress_threshold'] ) ? $args['compress_threshold'] : 0;
254 $this->_persistent = isset( $args['persistent'] ) ? $args['persistent'] : false;
255 $this->_compress_enable = true;
256 $this->_have_zlib = function_exists( 'gzcompress' );
257
258 $this->_cache_sock = array();
259 $this->_host_dead = array();
260
261 $this->_timeout_seconds = 0;
262 $this->_timeout_microseconds = isset( $args['timeout'] ) ? $args['timeout'] : 500000;
263
264 $this->_connect_timeout = isset( $args['connect_timeout'] ) ? $args['connect_timeout'] : 0.1;
265 $this->_connect_attempts = 2;
266 }
267
268 // }}}
269 // {{{ add()
270
271 /**
272 * Adds a key/value to the memcache server if one isn't already set with
273 * that key
274 *
275 * @param $key String: key to set with data
276 * @param $val Mixed: value to store
277 * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
278 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
279 * longer must be the timestamp of the time at which the mapping should expire. It
280 * is safe to use timestamps in all cases, regardless of exipration
281 * eg: strtotime("+3 hour")
282 *
283 * @return Boolean
284 */
285 public function add( $key, $val, $exp = 0 ) {
286 return $this->_set( 'add', $key, $val, $exp );
287 }
288
289 // }}}
290 // {{{ decr()
291
292 /**
293 * Decrease a value stored on the memcache server
294 *
295 * @param $key String: key to decrease
296 * @param $amt Integer: (optional) amount to decrease
297 *
298 * @return Mixed: FALSE on failure, value on success
299 */
300 public function decr( $key, $amt = 1 ) {
301 return $this->_incrdecr( 'decr', $key, $amt );
302 }
303
304 // }}}
305 // {{{ delete()
306
307 /**
308 * Deletes a key from the server, optionally after $time
309 *
310 * @param $key String: key to delete
311 * @param $time Integer: (optional) how long to wait before deleting
312 *
313 * @return Boolean: TRUE on success, FALSE on failure
314 */
315 public function delete( $key, $time = 0 ) {
316 if ( !$this->_active ) {
317 return false;
318 }
319
320 $sock = $this->get_sock( $key );
321 if ( !is_resource( $sock ) ) {
322 return false;
323 }
324
325 $key = is_array( $key ) ? $key[1] : $key;
326
327 if ( isset( $this->stats['delete'] ) ) {
328 $this->stats['delete']++;
329 } else {
330 $this->stats['delete'] = 1;
331 }
332 $cmd = "delete $key $time\r\n";
333 if( !$this->_fwrite( $sock, $cmd ) ) {
334 return false;
335 }
336 $res = $this->_fgets( $sock );
337
338 if ( $this->_debug ) {
339 $this->_debugprint( sprintf( "MemCache: delete %s (%s)\n", $key, $res ) );
340 }
341
342 if ( $res == "DELETED" || $res == "NOT_FOUND" ) {
343 return true;
344 }
345
346 return false;
347 }
348
349 /**
350 * @param $key
351 * @param $timeout int
352 * @return bool
353 */
354 public function lock( $key, $timeout = 0 ) {
355 /* stub */
356 return true;
357 }
358
359 /**
360 * @param $key
361 * @return bool
362 */
363 public function unlock( $key ) {
364 /* stub */
365 return true;
366 }
367
368 // }}}
369 // {{{ disconnect_all()
370
371 /**
372 * Disconnects all connected sockets
373 */
374 public function disconnect_all() {
375 foreach ( $this->_cache_sock as $sock ) {
376 fclose( $sock );
377 }
378
379 $this->_cache_sock = array();
380 }
381
382 // }}}
383 // {{{ enable_compress()
384
385 /**
386 * Enable / Disable compression
387 *
388 * @param $enable Boolean: TRUE to enable, FALSE to disable
389 */
390 public function enable_compress( $enable ) {
391 $this->_compress_enable = $enable;
392 }
393
394 // }}}
395 // {{{ forget_dead_hosts()
396
397 /**
398 * Forget about all of the dead hosts
399 */
400 public function forget_dead_hosts() {
401 $this->_host_dead = array();
402 }
403
404 // }}}
405 // {{{ get()
406
407 /**
408 * Retrieves the value associated with the key from the memcache server
409 *
410 * @param $key array|string key to retrieve
411 *
412 * @return Mixed
413 */
414 public function get( $key ) {
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 = "get $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 );
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 $keys Array: 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 = 'get';
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 );
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 $key String: 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 $key String: 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 $cmd String: 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 $key String: 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 // {{{ set_compress_threshold()
622
623 /**
624 * Sets the compression threshold
625 *
626 * @param $thresh Integer: threshold to compress if larger than
627 */
628 public function set_compress_threshold( $thresh ) {
629 $this->_compress_threshold = $thresh;
630 }
631
632 // }}}
633 // {{{ set_debug()
634
635 /**
636 * Sets the debug flag
637 *
638 * @param $dbg Boolean: TRUE for debugging, FALSE otherwise
639 *
640 * @see MWMemcached::__construct
641 */
642 public function set_debug( $dbg ) {
643 $this->_debug = $dbg;
644 }
645
646 // }}}
647 // {{{ set_servers()
648
649 /**
650 * Sets the server list to distribute key gets and puts between
651 *
652 * @param $list Array of servers to connect to
653 *
654 * @see MWMemcached::__construct()
655 */
656 public function set_servers( $list ) {
657 $this->_servers = $list;
658 $this->_active = count( $list );
659 $this->_buckets = null;
660 $this->_bucketcount = 0;
661
662 $this->_single_sock = null;
663 if ( $this->_active == 1 ) {
664 $this->_single_sock = $this->_servers[0];
665 }
666 }
667
668 /**
669 * Sets the timeout for new connections
670 *
671 * @param $seconds Integer: number of seconds
672 * @param $microseconds Integer: number of microseconds
673 */
674 public function set_timeout( $seconds, $microseconds ) {
675 $this->_timeout_seconds = $seconds;
676 $this->_timeout_microseconds = $microseconds;
677 }
678
679 // }}}
680 // }}}
681 // {{{ private methods
682 // {{{ _close_sock()
683
684 /**
685 * Close the specified socket
686 *
687 * @param $sock String: socket to close
688 *
689 * @access private
690 */
691 function _close_sock( $sock ) {
692 $host = array_search( $sock, $this->_cache_sock );
693 fclose( $this->_cache_sock[$host] );
694 unset( $this->_cache_sock[$host] );
695 }
696
697 // }}}
698 // {{{ _connect_sock()
699
700 /**
701 * Connects $sock to $host, timing out after $timeout
702 *
703 * @param $sock Integer: socket to connect
704 * @param $host String: Host:IP to connect to
705 *
706 * @return boolean
707 * @access private
708 */
709 function _connect_sock( &$sock, $host ) {
710 list( $ip, $port ) = explode( ':', $host );
711 $sock = false;
712 $timeout = $this->_connect_timeout;
713 $errno = $errstr = null;
714 for( $i = 0; !$sock && $i < $this->_connect_attempts; $i++ ) {
715 wfSuppressWarnings();
716 if ( $this->_persistent == 1 ) {
717 $sock = pfsockopen( $ip, $port, $errno, $errstr, $timeout );
718 } else {
719 $sock = fsockopen( $ip, $port, $errno, $errstr, $timeout );
720 }
721 wfRestoreWarnings();
722 }
723 if ( !$sock ) {
724 $this->_error_log( "Error connecting to $host: $errstr\n" );
725 $this->_dead_host( $host );
726 return false;
727 }
728
729 // Initialise timeout
730 stream_set_timeout( $sock, $this->_timeout_seconds, $this->_timeout_microseconds );
731
732 // If the connection was persistent, flush the read buffer in case there
733 // was a previous incomplete request on this connection
734 if ( $this->_persistent ) {
735 $this->_flush_read_buffer( $sock );
736 }
737 return true;
738 }
739
740 // }}}
741 // {{{ _dead_sock()
742
743 /**
744 * Marks a host as dead until 30-40 seconds in the future
745 *
746 * @param $sock String: socket to mark as dead
747 *
748 * @access private
749 */
750 function _dead_sock( $sock ) {
751 $host = array_search( $sock, $this->_cache_sock );
752 $this->_dead_host( $host );
753 }
754
755 /**
756 * @param $host
757 */
758 function _dead_host( $host ) {
759 $parts = explode( ':', $host );
760 $ip = $parts[0];
761 $this->_host_dead[$ip] = time() + 30 + intval( rand( 0, 10 ) );
762 $this->_host_dead[$host] = $this->_host_dead[$ip];
763 unset( $this->_cache_sock[$host] );
764 }
765
766 // }}}
767 // {{{ get_sock()
768
769 /**
770 * get_sock
771 *
772 * @param $key String: key to retrieve value for;
773 *
774 * @return Mixed: resource on success, false on failure
775 * @access private
776 */
777 function get_sock( $key ) {
778 if ( !$this->_active ) {
779 return false;
780 }
781
782 if ( $this->_single_sock !== null ) {
783 return $this->sock_to_host( $this->_single_sock );
784 }
785
786 $hv = is_array( $key ) ? intval( $key[0] ) : $this->_hashfunc( $key );
787 if ( $this->_buckets === null ) {
788 $bu = array();
789 foreach ( $this->_servers as $v ) {
790 if ( is_array( $v ) ) {
791 for( $i = 0; $i < $v[1]; $i++ ) {
792 $bu[] = $v[0];
793 }
794 } else {
795 $bu[] = $v;
796 }
797 }
798 $this->_buckets = $bu;
799 $this->_bucketcount = count( $bu );
800 }
801
802 $realkey = is_array( $key ) ? $key[1] : $key;
803 for( $tries = 0; $tries < 20; $tries++ ) {
804 $host = $this->_buckets[$hv % $this->_bucketcount];
805 $sock = $this->sock_to_host( $host );
806 if ( is_resource( $sock ) ) {
807 return $sock;
808 }
809 $hv = $this->_hashfunc( $hv . $realkey );
810 }
811
812 return false;
813 }
814
815 // }}}
816 // {{{ _hashfunc()
817
818 /**
819 * Creates a hash integer based on the $key
820 *
821 * @param $key String: key to hash
822 *
823 * @return Integer: hash value
824 * @access private
825 */
826 function _hashfunc( $key ) {
827 # Hash function must be in [0,0x7ffffff]
828 # We take the first 31 bits of the MD5 hash, which unlike the hash
829 # function used in a previous version of this client, works
830 return hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
831 }
832
833 // }}}
834 // {{{ _incrdecr()
835
836 /**
837 * Perform increment/decriment on $key
838 *
839 * @param $cmd String command to perform
840 * @param $key String|array key to perform it on
841 * @param $amt Integer amount to adjust
842 *
843 * @return Integer: new value of $key
844 * @access private
845 */
846 function _incrdecr( $cmd, $key, $amt = 1 ) {
847 if ( !$this->_active ) {
848 return null;
849 }
850
851 $sock = $this->get_sock( $key );
852 if ( !is_resource( $sock ) ) {
853 return null;
854 }
855
856 $key = is_array( $key ) ? $key[1] : $key;
857 if ( isset( $this->stats[$cmd] ) ) {
858 $this->stats[$cmd]++;
859 } else {
860 $this->stats[$cmd] = 1;
861 }
862 if ( !$this->_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
863 return null;
864 }
865
866 $line = $this->_fgets( $sock );
867 $match = array();
868 if ( !preg_match( '/^(\d+)/', $line, $match ) ) {
869 return null;
870 }
871 return $match[1];
872 }
873
874 // }}}
875 // {{{ _load_items()
876
877 /**
878 * Load items into $ret from $sock
879 *
880 * @param $sock Resource: socket to read from
881 * @param $ret Array: returned values
882 * @return boolean True for success, false for failure
883 *
884 * @access private
885 */
886 function _load_items( $sock, &$ret ) {
887 while ( 1 ) {
888 $decl = $this->_fgets( $sock );
889 if( $decl === false ) {
890 return false;
891 } elseif ( $decl == "END" ) {
892 return true;
893 } elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+)$/', $decl, $match ) ) {
894 list( $rkey, $flags, $len ) = array( $match[1], $match[2], $match[3] );
895 $data = $this->_fread( $sock, $len + 2 );
896 if ( $data === false ) {
897 return false;
898 }
899 if ( substr( $data, -2 ) !== "\r\n" ) {
900 $this->_handle_error( $sock,
901 'line ending missing from data block from $1' );
902 return false;
903 }
904 $data = substr( $data, 0, -2 );
905 $ret[$rkey] = $data;
906
907 if ( $this->_have_zlib && $flags & self::COMPRESSED ) {
908 $ret[$rkey] = gzuncompress( $ret[$rkey] );
909 }
910
911 if ( $flags & self::SERIALIZED ) {
912 $ret[$rkey] = unserialize( $ret[$rkey] );
913 }
914
915 } else {
916 $this->_handle_error( $sock, 'Error parsing response from $1' );
917 return false;
918 }
919 }
920 }
921
922 // }}}
923 // {{{ _set()
924
925 /**
926 * Performs the requested storage operation to the memcache server
927 *
928 * @param $cmd String: command to perform
929 * @param $key String: key to act on
930 * @param $val Mixed: what we need to store
931 * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
932 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
933 * longer must be the timestamp of the time at which the mapping should expire. It
934 * is safe to use timestamps in all cases, regardless of exipration
935 * eg: strtotime("+3 hour")
936 *
937 * @return Boolean
938 * @access private
939 */
940 function _set( $cmd, $key, $val, $exp ) {
941 if ( !$this->_active ) {
942 return false;
943 }
944
945 $sock = $this->get_sock( $key );
946 if ( !is_resource( $sock ) ) {
947 return false;
948 }
949
950 if ( isset( $this->stats[$cmd] ) ) {
951 $this->stats[$cmd]++;
952 } else {
953 $this->stats[$cmd] = 1;
954 }
955
956 $flags = 0;
957
958 if ( !is_scalar( $val ) ) {
959 $val = serialize( $val );
960 $flags |= self::SERIALIZED;
961 if ( $this->_debug ) {
962 $this->_debugprint( sprintf( "client: serializing data as it is not scalar\n" ) );
963 }
964 }
965
966 $len = strlen( $val );
967
968 if ( $this->_have_zlib && $this->_compress_enable &&
969 $this->_compress_threshold && $len >= $this->_compress_threshold )
970 {
971 $c_val = gzcompress( $val, 9 );
972 $c_len = strlen( $c_val );
973
974 if ( $c_len < $len * ( 1 - self::COMPRESSION_SAVINGS ) ) {
975 if ( $this->_debug ) {
976 $this->_debugprint( sprintf( "client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len ) );
977 }
978 $val = $c_val;
979 $len = $c_len;
980 $flags |= self::COMPRESSED;
981 }
982 }
983 if ( !$this->_fwrite( $sock, "$cmd $key $flags $exp $len\r\n$val\r\n" ) ) {
984 return false;
985 }
986
987 $line = $this->_fgets( $sock );
988
989 if ( $this->_debug ) {
990 $this->_debugprint( sprintf( "%s %s (%s)\n", $cmd, $key, $line ) );
991 }
992 if ( $line == "STORED" ) {
993 return true;
994 }
995 return false;
996 }
997
998 // }}}
999 // {{{ sock_to_host()
1000
1001 /**
1002 * Returns the socket for the host
1003 *
1004 * @param $host String: Host:IP to get socket for
1005 *
1006 * @return Mixed: IO Stream or false
1007 * @access private
1008 */
1009 function sock_to_host( $host ) {
1010 if ( isset( $this->_cache_sock[$host] ) ) {
1011 return $this->_cache_sock[$host];
1012 }
1013
1014 $sock = null;
1015 $now = time();
1016 list( $ip, /* $port */) = explode( ':', $host );
1017 if ( isset( $this->_host_dead[$host] ) && $this->_host_dead[$host] > $now ||
1018 isset( $this->_host_dead[$ip] ) && $this->_host_dead[$ip] > $now
1019 ) {
1020 return null;
1021 }
1022
1023 if ( !$this->_connect_sock( $sock, $host ) ) {
1024 return null;
1025 }
1026
1027 // Do not buffer writes
1028 stream_set_write_buffer( $sock, 0 );
1029
1030 $this->_cache_sock[$host] = $sock;
1031
1032 return $this->_cache_sock[$host];
1033 }
1034
1035 /**
1036 * @param $text string
1037 */
1038 function _debugprint( $text ) {
1039 global $wgDebugLogGroups;
1040 if( !isset( $wgDebugLogGroups['memcached'] ) ) {
1041 # Prefix message since it will end up in main debug log file
1042 $text = "memcached: $text";
1043 }
1044 wfDebugLog( 'memcached', $text );
1045 }
1046
1047 /**
1048 * @param $text string
1049 */
1050 function _error_log( $text ) {
1051 wfDebugLog( 'memcached-serious', "Memcached error: $text" );
1052 }
1053
1054 /**
1055 * Write to a stream. If there is an error, mark the socket dead.
1056 *
1057 * @param $sock The socket
1058 * @param $buf The string to write
1059 * @return bool True on success, false on failure
1060 */
1061 function _fwrite( $sock, $buf ) {
1062 $bytesWritten = 0;
1063 $bufSize = strlen( $buf );
1064 while ( $bytesWritten < $bufSize ) {
1065 $result = fwrite( $sock, $buf );
1066 $data = stream_get_meta_data( $sock );
1067 if ( $data['timed_out'] ) {
1068 $this->_handle_error( $sock, 'timeout writing to $1' );
1069 return false;
1070 }
1071 // Contrary to the documentation, fwrite() returns zero on error in PHP 5.3.
1072 if ( $result === false || $result === 0 ) {
1073 $this->_handle_error( $sock, 'error writing to $1' );
1074 return false;
1075 }
1076 $bytesWritten += $result;
1077 }
1078
1079 return true;
1080 }
1081
1082 /**
1083 * Handle an I/O error. Mark the socket dead and log an error.
1084 */
1085 function _handle_error( $sock, $msg ) {
1086 $peer = stream_socket_get_name( $sock, true /** remote **/ );
1087 if ( strval( $peer ) === '' ) {
1088 $peer = array_search( $sock, $this->_cache_sock );
1089 if ( $peer === false ) {
1090 $peer = '[unknown host]';
1091 }
1092 }
1093 $msg = str_replace( '$1', $peer, $msg );
1094 $this->_error_log( "$msg\n" );
1095 $this->_dead_sock( $sock );
1096 }
1097
1098 /**
1099 * Read the specified number of bytes from a stream. If there is an error,
1100 * mark the socket dead.
1101 *
1102 * @param $sock The socket
1103 * @param $len The number of bytes to read
1104 * @return The string on success, false on failure.
1105 */
1106 function _fread( $sock, $len ) {
1107 $buf = '';
1108 while ( $len > 0 ) {
1109 $result = fread( $sock, $len );
1110 $data = stream_get_meta_data( $sock );
1111 if ( $data['timed_out'] ) {
1112 $this->_handle_error( $sock, 'timeout reading from $1' );
1113 return false;
1114 }
1115 if ( $result === false ) {
1116 $this->_handle_error( $sock, 'error reading buffer from $1' );
1117 return false;
1118 }
1119 if ( $result === '' ) {
1120 // This will happen if the remote end of the socket is shut down
1121 $this->_handle_error( $sock, 'unexpected end of file reading from $1' );
1122 return false;
1123 }
1124 $len -= strlen( $result );
1125 $buf .= $result;
1126 }
1127 return $buf;
1128 }
1129
1130 /**
1131 * Read a line from a stream. If there is an error, mark the socket dead.
1132 * The \r\n line ending is stripped from the response.
1133 *
1134 * @param $sock The socket
1135 * @return The string on success, false on failure
1136 */
1137 function _fgets( $sock ) {
1138 $result = fgets( $sock );
1139 // fgets() may return a partial line if there is a select timeout after
1140 // a successful recv(), so we have to check for a timeout even if we
1141 // got a string response.
1142 $data = stream_get_meta_data( $sock );
1143 if ( $data['timed_out'] ) {
1144 $this->_handle_error( $sock, 'timeout reading line from $1' );
1145 return false;
1146 }
1147 if ( $result === false ) {
1148 $this->_handle_error( $sock, 'error reading line from $1' );
1149 return false;
1150 }
1151 if ( substr( $result, -2 ) === "\r\n" ) {
1152 $result = substr( $result, 0, -2 );
1153 } elseif ( substr( $result, -1 ) === "\n" ) {
1154 $result = substr( $result, 0, -1 );
1155 } else {
1156 $this->_handle_error( $sock, 'line ending missing in response from $1' );
1157 return false;
1158 }
1159 return $result;
1160 }
1161
1162 /**
1163 * Flush the read buffer of a stream
1164 * @param $f Resource
1165 */
1166 function _flush_read_buffer( $f ) {
1167 if ( !is_resource( $f ) ) {
1168 return;
1169 }
1170 $n = stream_select( $r = array( $f ), $w = null, $e = null, 0, 0 );
1171 while ( $n == 1 && !feof( $f ) ) {
1172 fread( $f, 1024 );
1173 $n = stream_select( $r = array( $f ), $w = null, $e = null, 0, 0 );
1174 }
1175 }
1176
1177 // }}}
1178 // }}}
1179 // }}}
1180 }
1181
1182
1183 // }}}
1184
1185 class MemCachedClientforWiki extends MWMemcached {
1186 }