3 * Memcached client for PHP.
5 * +---------------------------------------------------------------------------+
6 * | memcached client, PHP |
7 * +---------------------------------------------------------------------------+
8 * | Copyright (c) 2003 Ryan T. Dean <rtdean@cytherianage.net> |
9 * | All rights reserved. |
11 * | Redistribution and use in source and binary forms, with or without |
12 * | modification, are permitted provided that the following conditions |
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. |
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 * +---------------------------------------------------------------------------+
43 * This is the PHP client for memcached - a distributed memory cache daemon.
44 * More information is available at http://www.danga.com/memcached/
48 * require_once 'memcached.php';
50 * $mc = new MWMemcached(array(
51 * 'servers' => array('127.0.0.1:10000',
52 * array('192.0.0.1:10010', 2),
55 * 'compress_threshold' => 10240,
56 * 'persistent' => true));
58 * $mc->add('key', array('some', 'array'));
59 * $mc->replace('key', 'some random string');
60 * $val = $mc->get('key');
62 * @author Ryan T. Dean <rtdean@cytherianage.net>
69 // {{{ class MWMemcached
71 * memcached client class implemented using (p)fsockopen()
73 * @author Ryan T. Dean <rtdean@cytherianage.net>
84 * Flag: indicates data is serialized
89 * Flag: indicates data is compressed
96 * Minimum savings to store data compressed
98 const COMPRESSION_SAVINGS
= 0.20;
115 * Cached Sockets that are connected
123 * Current debug status; 0 - none to 9 - profiling
131 * Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'
139 * Is compression available?
147 * Do we want to use compression?
152 var $_compress_enable;
155 * At how many bytes should we compress?
160 var $_compress_threshold;
163 * Are we using persistent links?
171 * If only using one server; contains ip:port to connect to
179 * Array containing ip:port or array(ip:port, weight)
195 * Total # of bit buckets we have
203 * # of total servers we have
211 * Stream timeout in seconds. Applies for example to fread()
216 var $_timeout_seconds;
219 * Stream timeout in microseconds
224 var $_timeout_microseconds;
227 * Connect timeout in seconds
229 var $_connect_timeout;
232 * Number of connection attempts for each server
234 var $_connect_attempts;
239 // {{{ public functions
243 * Memcache initializer
245 * @param $args Array Associative array of settings
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' );
258 $this->_cache_sock
= array();
259 $this->_host_dead
= array();
261 $this->_timeout_seconds
= 0;
262 $this->_timeout_microseconds
= isset( $args['timeout'] ) ?
$args['timeout'] : 500000;
264 $this->_connect_timeout
= isset( $args['connect_timeout'] ) ?
$args['connect_timeout'] : 0.1;
265 $this->_connect_attempts
= 2;
272 * Adds a key/value to the memcache server if one isn't already set with
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 expiration
281 * eg: strtotime("+3 hour")
285 public function add( $key, $val, $exp = 0 ) {
286 return $this->_set( 'add', $key, $val, $exp );
293 * Decrease a value stored on the memcache server
295 * @param $key String: key to decrease
296 * @param $amt Integer: (optional) amount to decrease
298 * @return Mixed: FALSE on failure, value on success
300 public function decr( $key, $amt = 1 ) {
301 return $this->_incrdecr( 'decr', $key, $amt );
308 * Deletes a key from the server, optionally after $time
310 * @param $key String: key to delete
311 * @param $time Integer: (optional) how long to wait before deleting
313 * @return Boolean: TRUE on success, FALSE on failure
315 public function delete( $key, $time = 0 ) {
316 if ( !$this->_active
) {
320 $sock = $this->get_sock( $key );
321 if ( !is_resource( $sock ) ) {
325 $key = is_array( $key ) ?
$key[1] : $key;
327 if ( isset( $this->stats
['delete'] ) ) {
328 $this->stats
['delete']++
;
330 $this->stats
['delete'] = 1;
332 $cmd = "delete $key $time\r\n";
333 if( !$this->_fwrite( $sock, $cmd ) ) {
336 $res = $this->_fgets( $sock );
338 if ( $this->_debug
) {
339 $this->_debugprint( sprintf( "MemCache: delete %s (%s)\n", $key, $res ) );
342 if ( $res == "DELETED" ||
$res == "NOT_FOUND" ) {
351 * @param $timeout int
354 public function lock( $key, $timeout = 0 ) {
363 public function unlock( $key ) {
369 // {{{ disconnect_all()
372 * Disconnects all connected sockets
374 public function disconnect_all() {
375 foreach ( $this->_cache_sock
as $sock ) {
379 $this->_cache_sock
= array();
383 // {{{ enable_compress()
386 * Enable / Disable compression
388 * @param $enable Boolean: TRUE to enable, FALSE to disable
390 public function enable_compress( $enable ) {
391 $this->_compress_enable
= $enable;
395 // {{{ forget_dead_hosts()
398 * Forget about all of the dead hosts
400 public function forget_dead_hosts() {
401 $this->_host_dead
= array();
408 * Retrieves the value associated with the key from the memcache server
410 * @param $key array|string key to retrieve
411 * @param $casToken[optional] Float
415 public function get( $key, &$casToken = null ) {
416 wfProfileIn( __METHOD__
);
418 if ( $this->_debug
) {
419 $this->_debugprint( "get($key)\n" );
422 if ( !$this->_active
) {
423 wfProfileOut( __METHOD__
);
427 $sock = $this->get_sock( $key );
429 if ( !is_resource( $sock ) ) {
430 wfProfileOut( __METHOD__
);
434 $key = is_array( $key ) ?
$key[1] : $key;
435 if ( isset( $this->stats
['get'] ) ) {
436 $this->stats
['get']++
;
438 $this->stats
['get'] = 1;
441 $cmd = "gets $key\r\n";
442 if ( !$this->_fwrite( $sock, $cmd ) ) {
443 wfProfileOut( __METHOD__
);
448 $this->_load_items( $sock, $val, $casToken );
450 if ( $this->_debug
) {
451 foreach ( $val as $k => $v ) {
452 $this->_debugprint( sprintf( "MemCache: sock %s got %s\n", serialize( $sock ), $k ) );
457 if ( isset( $val[$key] ) ) {
460 wfProfileOut( __METHOD__
);
468 * Get multiple keys from the server(s)
470 * @param $keys Array: keys to retrieve
474 public function get_multi( $keys ) {
475 if ( !$this->_active
) {
479 if ( isset( $this->stats
['get_multi'] ) ) {
480 $this->stats
['get_multi']++
;
482 $this->stats
['get_multi'] = 1;
484 $sock_keys = array();
486 foreach ( $keys as $key ) {
487 $sock = $this->get_sock( $key );
488 if ( !is_resource( $sock ) ) {
491 $key = is_array( $key ) ?
$key[1] : $key;
492 if ( !isset( $sock_keys[$sock] ) ) {
493 $sock_keys[ intval( $sock ) ] = array();
496 $sock_keys[ intval( $sock ) ][] = $key;
500 // Send out the requests
501 foreach ( $socks as $sock ) {
503 foreach ( $sock_keys[ intval( $sock ) ] as $key ) {
508 if ( $this->_fwrite( $sock, $cmd ) ) {
515 foreach ( $gather as $sock ) {
516 $this->_load_items( $sock, $val, $casToken );
519 if ( $this->_debug
) {
520 foreach ( $val as $k => $v ) {
521 $this->_debugprint( sprintf( "MemCache: got %s\n", $k ) );
532 * Increments $key (optionally) by $amt
534 * @param $key String: key to increment
535 * @param $amt Integer: (optional) amount to increment
537 * @return Integer: null if the key does not exist yet (this does NOT
538 * create new mappings if the key does not exist). If the key does
539 * exist, this returns the new value for that key.
541 public function incr( $key, $amt = 1 ) {
542 return $this->_incrdecr( 'incr', $key, $amt );
549 * Overwrites an existing value for key; only works if key is already set
551 * @param $key String: key to set value as
552 * @param $value Mixed: value to store
553 * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
554 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
555 * longer must be the timestamp of the time at which the mapping should expire. It
556 * is safe to use timestamps in all cases, regardless of exipration
557 * eg: strtotime("+3 hour")
561 public function replace( $key, $value, $exp = 0 ) {
562 return $this->_set( 'replace', $key, $value, $exp );
569 * Passes through $cmd to the memcache server connected by $sock; returns
570 * output as an array (null array if no output)
572 * @param $sock Resource: socket to send command on
573 * @param $cmd String: command to run
575 * @return Array: output array
577 public function run_command( $sock, $cmd ) {
578 if ( !is_resource( $sock ) ) {
582 if ( !$this->_fwrite( $sock, $cmd ) ) {
588 $res = $this->_fgets( $sock );
590 if ( preg_match( '/^END/', $res ) ) {
593 if ( strlen( $res ) == 0 ) {
604 * Unconditionally sets a key to a given value in the memcache. Returns true
605 * if set successfully.
607 * @param $key String: key to set value as
608 * @param $value Mixed: value to set
609 * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
610 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
611 * longer must be the timestamp of the time at which the mapping should expire. It
612 * is safe to use timestamps in all cases, regardless of exipration
613 * eg: strtotime("+3 hour")
615 * @return Boolean: TRUE on success
617 public function set( $key, $value, $exp = 0 ) {
618 return $this->_set( 'set', $key, $value, $exp );
625 * Sets a key to a given value in the memcache if the current value still corresponds
626 * to a known, given value. Returns true if set successfully.
628 * @param $casToken Float: current known value
629 * @param $key String: key to set value as
630 * @param $value Mixed: value to set
631 * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
632 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
633 * longer must be the timestamp of the time at which the mapping should expire. It
634 * is safe to use timestamps in all cases, regardless of exipration
635 * eg: strtotime("+3 hour")
637 * @return Boolean: TRUE on success
639 public function cas( $casToken, $key, $value, $exp = 0 ) {
640 return $this->_set( 'cas', $key, $value, $exp, $casToken );
644 // {{{ set_compress_threshold()
647 * Sets the compression threshold
649 * @param $thresh Integer: threshold to compress if larger than
651 public function set_compress_threshold( $thresh ) {
652 $this->_compress_threshold
= $thresh;
659 * Sets the debug flag
661 * @param $dbg Boolean: TRUE for debugging, FALSE otherwise
663 * @see MWMemcached::__construct
665 public function set_debug( $dbg ) {
666 $this->_debug
= $dbg;
673 * Sets the server list to distribute key gets and puts between
675 * @param $list Array of servers to connect to
677 * @see MWMemcached::__construct()
679 public function set_servers( $list ) {
680 $this->_servers
= $list;
681 $this->_active
= count( $list );
682 $this->_buckets
= null;
683 $this->_bucketcount
= 0;
685 $this->_single_sock
= null;
686 if ( $this->_active
== 1 ) {
687 $this->_single_sock
= $this->_servers
[0];
692 * Sets the timeout for new connections
694 * @param $seconds Integer: number of seconds
695 * @param $microseconds Integer: number of microseconds
697 public function set_timeout( $seconds, $microseconds ) {
698 $this->_timeout_seconds
= $seconds;
699 $this->_timeout_microseconds
= $microseconds;
704 // {{{ private methods
708 * Close the specified socket
710 * @param $sock String: socket to close
714 function _close_sock( $sock ) {
715 $host = array_search( $sock, $this->_cache_sock
);
716 fclose( $this->_cache_sock
[$host] );
717 unset( $this->_cache_sock
[$host] );
721 // {{{ _connect_sock()
724 * Connects $sock to $host, timing out after $timeout
726 * @param $sock Integer: socket to connect
727 * @param $host String: Host:IP to connect to
732 function _connect_sock( &$sock, $host ) {
733 list( $ip, $port ) = explode( ':', $host );
735 $timeout = $this->_connect_timeout
;
736 $errno = $errstr = null;
737 for( $i = 0; !$sock && $i < $this->_connect_attempts
; $i++
) {
738 wfSuppressWarnings();
739 if ( $this->_persistent
== 1 ) {
740 $sock = pfsockopen( $ip, $port, $errno, $errstr, $timeout );
742 $sock = fsockopen( $ip, $port, $errno, $errstr, $timeout );
747 $this->_error_log( "Error connecting to $host: $errstr\n" );
748 $this->_dead_host( $host );
752 // Initialise timeout
753 stream_set_timeout( $sock, $this->_timeout_seconds
, $this->_timeout_microseconds
);
755 // If the connection was persistent, flush the read buffer in case there
756 // was a previous incomplete request on this connection
757 if ( $this->_persistent
) {
758 $this->_flush_read_buffer( $sock );
767 * Marks a host as dead until 30-40 seconds in the future
769 * @param $sock String: socket to mark as dead
773 function _dead_sock( $sock ) {
774 $host = array_search( $sock, $this->_cache_sock
);
775 $this->_dead_host( $host );
781 function _dead_host( $host ) {
782 $parts = explode( ':', $host );
784 $this->_host_dead
[$ip] = time() +
30 +
intval( rand( 0, 10 ) );
785 $this->_host_dead
[$host] = $this->_host_dead
[$ip];
786 unset( $this->_cache_sock
[$host] );
795 * @param $key String: key to retrieve value for;
797 * @return Mixed: resource on success, false on failure
800 function get_sock( $key ) {
801 if ( !$this->_active
) {
805 if ( $this->_single_sock
!== null ) {
806 return $this->sock_to_host( $this->_single_sock
);
809 $hv = is_array( $key ) ?
intval( $key[0] ) : $this->_hashfunc( $key );
810 if ( $this->_buckets
=== null ) {
812 foreach ( $this->_servers
as $v ) {
813 if ( is_array( $v ) ) {
814 for( $i = 0; $i < $v[1]; $i++
) {
821 $this->_buckets
= $bu;
822 $this->_bucketcount
= count( $bu );
825 $realkey = is_array( $key ) ?
$key[1] : $key;
826 for( $tries = 0; $tries < 20; $tries++
) {
827 $host = $this->_buckets
[$hv %
$this->_bucketcount
];
828 $sock = $this->sock_to_host( $host );
829 if ( is_resource( $sock ) ) {
832 $hv = $this->_hashfunc( $hv . $realkey );
842 * Creates a hash integer based on the $key
844 * @param $key String: key to hash
846 * @return Integer: hash value
849 function _hashfunc( $key ) {
850 # Hash function must be in [0,0x7ffffff]
851 # We take the first 31 bits of the MD5 hash, which unlike the hash
852 # function used in a previous version of this client, works
853 return hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
860 * Perform increment/decriment on $key
862 * @param $cmd String command to perform
863 * @param $key String|array key to perform it on
864 * @param $amt Integer amount to adjust
866 * @return Integer: new value of $key
869 function _incrdecr( $cmd, $key, $amt = 1 ) {
870 if ( !$this->_active
) {
874 $sock = $this->get_sock( $key );
875 if ( !is_resource( $sock ) ) {
879 $key = is_array( $key ) ?
$key[1] : $key;
880 if ( isset( $this->stats
[$cmd] ) ) {
881 $this->stats
[$cmd]++
;
883 $this->stats
[$cmd] = 1;
885 if ( !$this->_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
889 $line = $this->_fgets( $sock );
891 if ( !preg_match( '/^(\d+)/', $line, $match ) ) {
901 * Load items into $ret from $sock
903 * @param $sock Resource: socket to read from
904 * @param $ret Array: returned values
905 * @param $casToken[optional] Float
906 * @return boolean True for success, false for failure
910 function _load_items( $sock, &$ret, &$casToken = null ) {
912 $decl = $this->_fgets( $sock );
913 if( $decl === false ) {
915 } elseif ( $decl == "END" ) {
917 } elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+) (\d+)$/', $decl, $match ) ) {
918 list( $rkey, $flags, $len, $casToken ) = array( $match[1], $match[2], $match[3], $match[4] );
919 $data = $this->_fread( $sock, $len +
2 );
920 if ( $data === false ) {
923 if ( substr( $data, -2 ) !== "\r\n" ) {
924 $this->_handle_error( $sock,
925 'line ending missing from data block from $1' );
928 $data = substr( $data, 0, -2 );
931 if ( $this->_have_zlib
&& $flags & self
::COMPRESSED
) {
932 $ret[$rkey] = gzuncompress( $ret[$rkey] );
935 if ( $flags & self
::SERIALIZED
) {
936 $ret[$rkey] = unserialize( $ret[$rkey] );
940 $this->_handle_error( $sock, 'Error parsing response from $1' );
950 * Performs the requested storage operation to the memcache server
952 * @param $cmd String: command to perform
953 * @param $key String: key to act on
954 * @param $val Mixed: what we need to store
955 * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
956 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
957 * longer must be the timestamp of the time at which the mapping should expire. It
958 * is safe to use timestamps in all cases, regardless of exipration
959 * eg: strtotime("+3 hour")
960 * @param $casToken[optional] Float
965 function _set( $cmd, $key, $val, $exp, $casToken = null ) {
966 if ( !$this->_active
) {
970 $sock = $this->get_sock( $key );
971 if ( !is_resource( $sock ) ) {
975 if ( isset( $this->stats
[$cmd] ) ) {
976 $this->stats
[$cmd]++
;
978 $this->stats
[$cmd] = 1;
983 if ( !is_scalar( $val ) ) {
984 $val = serialize( $val );
985 $flags |
= self
::SERIALIZED
;
986 if ( $this->_debug
) {
987 $this->_debugprint( sprintf( "client: serializing data as it is not scalar\n" ) );
991 $len = strlen( $val );
993 if ( $this->_have_zlib
&& $this->_compress_enable
&&
994 $this->_compress_threshold
&& $len >= $this->_compress_threshold
)
996 $c_val = gzcompress( $val, 9 );
997 $c_len = strlen( $c_val );
999 if ( $c_len < $len * ( 1 - self
::COMPRESSION_SAVINGS
) ) {
1000 if ( $this->_debug
) {
1001 $this->_debugprint( sprintf( "client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len ) );
1005 $flags |
= self
::COMPRESSED
;
1009 $command = "$cmd $key $flags $exp $len";
1011 $command .= " $casToken";
1014 if ( !$this->_fwrite( $sock, "$command\r\n$val\r\n" ) ) {
1018 $line = $this->_fgets( $sock );
1020 if ( $this->_debug
) {
1021 $this->_debugprint( sprintf( "%s %s (%s)\n", $cmd, $key, $line ) );
1023 if ( $line == "STORED" ) {
1030 // {{{ sock_to_host()
1033 * Returns the socket for the host
1035 * @param $host String: Host:IP to get socket for
1037 * @return Mixed: IO Stream or false
1040 function sock_to_host( $host ) {
1041 if ( isset( $this->_cache_sock
[$host] ) ) {
1042 return $this->_cache_sock
[$host];
1047 list( $ip, /* $port */) = explode( ':', $host );
1048 if ( isset( $this->_host_dead
[$host] ) && $this->_host_dead
[$host] > $now ||
1049 isset( $this->_host_dead
[$ip] ) && $this->_host_dead
[$ip] > $now
1054 if ( !$this->_connect_sock( $sock, $host ) ) {
1058 // Do not buffer writes
1059 stream_set_write_buffer( $sock, 0 );
1061 $this->_cache_sock
[$host] = $sock;
1063 return $this->_cache_sock
[$host];
1067 * @param $text string
1069 function _debugprint( $text ) {
1070 wfDebugLog( 'memcached', $text );
1074 * @param $text string
1076 function _error_log( $text ) {
1077 wfDebugLog( 'memcached-serious', "Memcached error: $text" );
1081 * Write to a stream. If there is an error, mark the socket dead.
1083 * @param $sock The socket
1084 * @param $buf The string to write
1085 * @return bool True on success, false on failure
1087 function _fwrite( $sock, $buf ) {
1089 $bufSize = strlen( $buf );
1090 while ( $bytesWritten < $bufSize ) {
1091 $result = fwrite( $sock, $buf );
1092 $data = stream_get_meta_data( $sock );
1093 if ( $data['timed_out'] ) {
1094 $this->_handle_error( $sock, 'timeout writing to $1' );
1097 // Contrary to the documentation, fwrite() returns zero on error in PHP 5.3.
1098 if ( $result === false ||
$result === 0 ) {
1099 $this->_handle_error( $sock, 'error writing to $1' );
1102 $bytesWritten +
= $result;
1109 * Handle an I/O error. Mark the socket dead and log an error.
1111 function _handle_error( $sock, $msg ) {
1112 $peer = stream_socket_get_name( $sock, true /** remote **/ );
1113 if ( strval( $peer ) === '' ) {
1114 $peer = array_search( $sock, $this->_cache_sock
);
1115 if ( $peer === false ) {
1116 $peer = '[unknown host]';
1119 $msg = str_replace( '$1', $peer, $msg );
1120 $this->_error_log( "$msg\n" );
1121 $this->_dead_sock( $sock );
1125 * Read the specified number of bytes from a stream. If there is an error,
1126 * mark the socket dead.
1128 * @param $sock The socket
1129 * @param $len The number of bytes to read
1130 * @return The string on success, false on failure.
1132 function _fread( $sock, $len ) {
1134 while ( $len > 0 ) {
1135 $result = fread( $sock, $len );
1136 $data = stream_get_meta_data( $sock );
1137 if ( $data['timed_out'] ) {
1138 $this->_handle_error( $sock, 'timeout reading from $1' );
1141 if ( $result === false ) {
1142 $this->_handle_error( $sock, 'error reading buffer from $1' );
1145 if ( $result === '' ) {
1146 // This will happen if the remote end of the socket is shut down
1147 $this->_handle_error( $sock, 'unexpected end of file reading from $1' );
1150 $len -= strlen( $result );
1157 * Read a line from a stream. If there is an error, mark the socket dead.
1158 * The \r\n line ending is stripped from the response.
1160 * @param $sock The socket
1161 * @return The string on success, false on failure
1163 function _fgets( $sock ) {
1164 $result = fgets( $sock );
1165 // fgets() may return a partial line if there is a select timeout after
1166 // a successful recv(), so we have to check for a timeout even if we
1167 // got a string response.
1168 $data = stream_get_meta_data( $sock );
1169 if ( $data['timed_out'] ) {
1170 $this->_handle_error( $sock, 'timeout reading line from $1' );
1173 if ( $result === false ) {
1174 $this->_handle_error( $sock, 'error reading line from $1' );
1177 if ( substr( $result, -2 ) === "\r\n" ) {
1178 $result = substr( $result, 0, -2 );
1179 } elseif ( substr( $result, -1 ) === "\n" ) {
1180 $result = substr( $result, 0, -1 );
1182 $this->_handle_error( $sock, 'line ending missing in response from $1' );
1189 * Flush the read buffer of a stream
1190 * @param $f Resource
1192 function _flush_read_buffer( $f ) {
1193 if ( !is_resource( $f ) ) {
1199 $n = stream_select( $r, $w, $e, 0, 0 );
1200 while ( $n == 1 && !feof( $f ) ) {
1205 $n = stream_select( $r, $w, $e, 0, 0 );
1217 class MemCachedClientforWiki
extends MWMemcached
{