MWException: Don't send headers multiple times
[mediawiki.git] / includes / objectcache / MemcachedClient.php
blob9de840b62cf90163809d2a4c9bc4a1e554aeb579
1 <?php
2 // @codingStandardsIgnoreFile It's an external lib and it isn't. Let's not bother.
3 /**
4 * Memcached client for PHP.
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 * +---------------------------------------------------------------------------+
39 * @file
40 * $TCAnet$
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/
47 * Usage example:
49 * require_once 'memcached.php';
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));
59 * $mc->add( 'key', array( 'some', 'array' ) );
60 * $mc->replace( 'key', 'some random string' );
61 * $val = $mc->get( 'key' );
63 * @author Ryan T. Dean <rtdean@cytherianage.net>
64 * @version 0.1.2
67 // {{{ requirements
68 // }}}
70 // {{{ class MWMemcached
71 /**
72 * memcached client class implemented using (p)fsockopen()
74 * @author Ryan T. Dean <rtdean@cytherianage.net>
75 * @ingroup Cache
77 class MWMemcached {
78 // {{{ properties
79 // {{{ public
81 // {{{ constants
82 // {{{ flags
84 /**
85 * Flag: indicates data is serialized
87 const SERIALIZED = 1;
89 /**
90 * Flag: indicates data is compressed
92 const COMPRESSED = 2;
94 // }}}
96 /**
97 * Minimum savings to store data compressed
99 const COMPRESSION_SAVINGS = 0.20;
101 // }}}
104 * Command statistics
106 * @var array
107 * @access public
109 var $stats;
111 // }}}
112 // {{{ private
115 * Cached Sockets that are connected
117 * @var array
118 * @access private
120 var $_cache_sock;
123 * Current debug status; 0 - none to 9 - profiling
125 * @var bool
126 * @access private
128 var $_debug;
131 * Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'
133 * @var array
134 * @access private
136 var $_host_dead;
139 * Is compression available?
141 * @var bool
142 * @access private
144 var $_have_zlib;
147 * Do we want to use compression?
149 * @var bool
150 * @access private
152 var $_compress_enable;
155 * At how many bytes should we compress?
157 * @var int
158 * @access private
160 var $_compress_threshold;
163 * Are we using persistent links?
165 * @var bool
166 * @access private
168 var $_persistent;
171 * If only using one server; contains ip:port to connect to
173 * @var string
174 * @access private
176 var $_single_sock;
179 * Array containing ip:port or array(ip:port, weight)
181 * @var array
182 * @access private
184 var $_servers;
187 * Our bit buckets
189 * @var array
190 * @access private
192 var $_buckets;
195 * Total # of bit buckets we have
197 * @var int
198 * @access private
200 var $_bucketcount;
203 * # of total servers we have
205 * @var int
206 * @access private
208 var $_active;
211 * Stream timeout in seconds. Applies for example to fread()
213 * @var int
214 * @access private
216 var $_timeout_seconds;
219 * Stream timeout in microseconds
221 * @var int
222 * @access private
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;
236 // }}}
237 // }}}
238 // {{{ methods
239 // {{{ public functions
240 // {{{ memcached()
243 * Memcache initializer
245 * @param array $args Associative array of settings
247 * @return mixed
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;
268 // }}}
269 // {{{ add()
272 * Adds a key/value to the memcache server if one isn't already set with
273 * that key
275 * @param string $key Key to set with data
276 * @param mixed $val Value to store
277 * @param int $exp (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")
283 * @return bool
285 public function add( $key, $val, $exp = 0 ) {
286 return $this->_set( 'add', $key, $val, $exp );
289 // }}}
290 // {{{ decr()
293 * Decrease a value stored on the memcache server
295 * @param string $key Key to decrease
296 * @param int $amt (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 );
304 // }}}
305 // {{{ delete()
308 * Deletes a key from the server, optionally after $time
310 * @param string $key Key to delete
311 * @param int $time (optional) how long to wait before deleting
313 * @return bool True on success, false on failure
315 public function delete( $key, $time = 0 ) {
316 if ( !$this->_active ) {
317 return false;
320 $sock = $this->get_sock( $key );
321 if ( !is_resource( $sock ) ) {
322 return false;
325 $key = is_array( $key ) ? $key[1] : $key;
327 if ( isset( $this->stats['delete'] ) ) {
328 $this->stats['delete']++;
329 } else {
330 $this->stats['delete'] = 1;
332 $cmd = "delete $key $time\r\n";
333 if ( !$this->_fwrite( $sock, $cmd ) ) {
334 return false;
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" ) {
343 return true;
346 return false;
350 * @param string $key
351 * @param int $timeout
352 * @return bool
354 public function lock( $key, $timeout = 0 ) {
355 /* stub */
356 return true;
360 * @param string $key
361 * @return bool
363 public function unlock( $key ) {
364 /* stub */
365 return true;
368 // }}}
369 // {{{ disconnect_all()
372 * Disconnects all connected sockets
374 public function disconnect_all() {
375 foreach ( $this->_cache_sock as $sock ) {
376 fclose( $sock );
379 $this->_cache_sock = array();
382 // }}}
383 // {{{ enable_compress()
386 * Enable / Disable compression
388 * @param bool $enable True to enable, false to disable
390 public function enable_compress( $enable ) {
391 $this->_compress_enable = $enable;
394 // }}}
395 // {{{ forget_dead_hosts()
398 * Forget about all of the dead hosts
400 public function forget_dead_hosts() {
401 $this->_host_dead = array();
404 // }}}
405 // {{{ get()
408 * Retrieves the value associated with the key from the memcache server
410 * @param array|string $key key to retrieve
411 * @param float $casToken [optional]
413 * @return mixed
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__ );
424 return false;
427 $sock = $this->get_sock( $key );
429 if ( !is_resource( $sock ) ) {
430 wfProfileOut( __METHOD__ );
431 return false;
434 $key = is_array( $key ) ? $key[1] : $key;
435 if ( isset( $this->stats['get'] ) ) {
436 $this->stats['get']++;
437 } else {
438 $this->stats['get'] = 1;
441 $cmd = "gets $key\r\n";
442 if ( !$this->_fwrite( $sock, $cmd ) ) {
443 wfProfileOut( __METHOD__ );
444 return false;
447 $val = array();
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 ) );
456 $value = false;
457 if ( isset( $val[$key] ) ) {
458 $value = $val[$key];
460 wfProfileOut( __METHOD__ );
461 return $value;
464 // }}}
465 // {{{ get_multi()
468 * Get multiple keys from the server(s)
470 * @param array $keys Keys to retrieve
472 * @return array
474 public function get_multi( $keys ) {
475 if ( !$this->_active ) {
476 return false;
479 if ( isset( $this->stats['get_multi'] ) ) {
480 $this->stats['get_multi']++;
481 } else {
482 $this->stats['get_multi'] = 1;
484 $sock_keys = array();
485 $socks = array();
486 foreach ( $keys as $key ) {
487 $sock = $this->get_sock( $key );
488 if ( !is_resource( $sock ) ) {
489 continue;
491 $key = is_array( $key ) ? $key[1] : $key;
492 if ( !isset( $sock_keys[$sock] ) ) {
493 $sock_keys[intval( $sock )] = array();
494 $socks[] = $sock;
496 $sock_keys[intval( $sock )][] = $key;
499 $gather = array();
500 // Send out the requests
501 foreach ( $socks as $sock ) {
502 $cmd = 'gets';
503 foreach ( $sock_keys[intval( $sock )] as $key ) {
504 $cmd .= ' ' . $key;
506 $cmd .= "\r\n";
508 if ( $this->_fwrite( $sock, $cmd ) ) {
509 $gather[] = $sock;
513 // Parse responses
514 $val = array();
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 ) );
525 return $val;
528 // }}}
529 // {{{ incr()
532 * Increments $key (optionally) by $amt
534 * @param string $key Key to increment
535 * @param int $amt (optional) amount to increment
537 * @return int|null 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 );
545 // }}}
546 // {{{ replace()
549 * Overwrites an existing value for key; only works if key is already set
551 * @param string $key Key to set value as
552 * @param mixed $value Value to store
553 * @param int $exp (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")
559 * @return bool
561 public function replace( $key, $value, $exp = 0 ) {
562 return $this->_set( 'replace', $key, $value, $exp );
565 // }}}
566 // {{{ run_command()
569 * Passes through $cmd to the memcache server connected by $sock; returns
570 * output as an array (null array if no output)
572 * @param Resource $sock Socket to send command on
573 * @param string $cmd Command to run
575 * @return array Output array
577 public function run_command( $sock, $cmd ) {
578 if ( !is_resource( $sock ) ) {
579 return array();
582 if ( !$this->_fwrite( $sock, $cmd ) ) {
583 return array();
586 $ret = array();
587 while ( true ) {
588 $res = $this->_fgets( $sock );
589 $ret[] = $res;
590 if ( preg_match( '/^END/', $res ) ) {
591 break;
593 if ( strlen( $res ) == 0 ) {
594 break;
597 return $ret;
600 // }}}
601 // {{{ set()
604 * Unconditionally sets a key to a given value in the memcache. Returns true
605 * if set successfully.
607 * @param string $key Key to set value as
608 * @param mixed $value Value to set
609 * @param int $exp (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 bool True on success
617 public function set( $key, $value, $exp = 0 ) {
618 return $this->_set( 'set', $key, $value, $exp );
621 // }}}
622 // {{{ cas()
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 float $casToken Current known value
629 * @param string $key Key to set value as
630 * @param mixed $value Value to set
631 * @param int $exp (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 bool True on success
639 public function cas( $casToken, $key, $value, $exp = 0 ) {
640 return $this->_set( 'cas', $key, $value, $exp, $casToken );
643 // }}}
644 // {{{ set_compress_threshold()
647 * Sets the compression threshold
649 * @param int $thresh Threshold to compress if larger than
651 public function set_compress_threshold( $thresh ) {
652 $this->_compress_threshold = $thresh;
655 // }}}
656 // {{{ set_debug()
659 * Sets the debug flag
661 * @param bool $dbg True for debugging, false otherwise
663 * @see MWMemcached::__construct
665 public function set_debug( $dbg ) {
666 $this->_debug = $dbg;
669 // }}}
670 // {{{ set_servers()
673 * Sets the server list to distribute key gets and puts between
675 * @param array $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 int $seconds Number of seconds
695 * @param int $microseconds Number of microseconds
697 public function set_timeout( $seconds, $microseconds ) {
698 $this->_timeout_seconds = $seconds;
699 $this->_timeout_microseconds = $microseconds;
702 // }}}
703 // }}}
704 // {{{ private methods
705 // {{{ _close_sock()
708 * Close the specified socket
710 * @param string $sock Socket to close
712 * @access private
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] );
720 // }}}
721 // {{{ _connect_sock()
724 * Connects $sock to $host, timing out after $timeout
726 * @param int $sock Socket to connect
727 * @param string $host Host:IP to connect to
729 * @return bool
730 * @access private
732 function _connect_sock( &$sock, $host ) {
733 list( $ip, $port ) = preg_split( '/:(?=\d)/', $host );
734 $sock = false;
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 );
741 } else {
742 $sock = fsockopen( $ip, $port, $errno, $errstr, $timeout );
744 wfRestoreWarnings();
746 if ( !$sock ) {
747 $this->_error_log( "Error connecting to $host: $errstr\n" );
748 $this->_dead_host( $host );
749 return false;
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 );
760 return true;
763 // }}}
764 // {{{ _dead_sock()
767 * Marks a host as dead until 30-40 seconds in the future
769 * @param string $sock Socket to mark as dead
771 * @access private
773 function _dead_sock( $sock ) {
774 $host = array_search( $sock, $this->_cache_sock );
775 $this->_dead_host( $host );
779 * @param string $host
781 function _dead_host( $host ) {
782 $parts = explode( ':', $host );
783 $ip = $parts[0];
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] );
789 // }}}
790 // {{{ get_sock()
793 * get_sock
795 * @param string $key Key to retrieve value for;
797 * @return Resource|bool Resource on success, false on failure
798 * @access private
800 function get_sock( $key ) {
801 if ( !$this->_active ) {
802 return false;
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 ) {
811 $bu = array();
812 foreach ( $this->_servers as $v ) {
813 if ( is_array( $v ) ) {
814 for ( $i = 0; $i < $v[1]; $i++ ) {
815 $bu[] = $v[0];
817 } else {
818 $bu[] = $v;
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 ) ) {
830 return $sock;
832 $hv = $this->_hashfunc( $hv . $realkey );
835 return false;
838 // }}}
839 // {{{ _hashfunc()
842 * Creates a hash integer based on the $key
844 * @param string $key Key to hash
846 * @return int Hash value
847 * @access private
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;
856 // }}}
857 // {{{ _incrdecr()
860 * Perform increment/decriment on $key
862 * @param string $cmd Command to perform
863 * @param string|array $key Key to perform it on
864 * @param int $amt Amount to adjust
866 * @return int New value of $key
867 * @access private
869 function _incrdecr( $cmd, $key, $amt = 1 ) {
870 if ( !$this->_active ) {
871 return null;
874 $sock = $this->get_sock( $key );
875 if ( !is_resource( $sock ) ) {
876 return null;
879 $key = is_array( $key ) ? $key[1] : $key;
880 if ( isset( $this->stats[$cmd] ) ) {
881 $this->stats[$cmd]++;
882 } else {
883 $this->stats[$cmd] = 1;
885 if ( !$this->_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
886 return null;
889 $line = $this->_fgets( $sock );
890 $match = array();
891 if ( !preg_match( '/^(\d+)/', $line, $match ) ) {
892 return null;
894 return $match[1];
897 // }}}
898 // {{{ _load_items()
901 * Load items into $ret from $sock
903 * @param Resource $sock Socket to read from
904 * @param array $ret returned values
905 * @param float $casToken [optional]
906 * @return bool True for success, false for failure
908 * @access private
910 function _load_items( $sock, &$ret, &$casToken = null ) {
911 $results = array();
913 while ( 1 ) {
914 $decl = $this->_fgets( $sock );
916 if ( $decl === false ) {
918 * If nothing can be read, something is wrong because we know exactly when
919 * to stop reading (right after "END") and we return right after that.
921 return false;
922 } elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+) (\d+)$/', $decl, $match ) ) {
924 * Read all data returned. This can be either one or multiple values.
925 * Save all that data (in an array) to be processed later: we'll first
926 * want to continue reading until "END" before doing anything else,
927 * to make sure that we don't leave our client in a state where it's
928 * output is not yet fully read.
930 $results[] = array(
931 $match[1], // rkey
932 $match[2], // flags
933 $match[3], // len
934 $match[4], // casToken
935 $this->_fread( $sock, $match[3] + 2 ), // data
937 } elseif ( $decl == "END" ) {
938 if ( count( $results ) == 0 ) {
939 return false;
943 * All data has been read, time to process the data and build
944 * meaningful return values.
946 foreach ( $results as $vars ) {
947 list( $rkey, $flags, $len, $casToken, $data ) = $vars;
949 if ( $data === false || substr( $data, -2 ) !== "\r\n" ) {
950 $this->_handle_error( $sock,
951 'line ending missing from data block from $1' );
952 return false;
954 $data = substr( $data, 0, -2 );
955 $ret[$rkey] = $data;
957 if ( $this->_have_zlib && $flags & self::COMPRESSED ) {
958 $ret[$rkey] = gzuncompress( $ret[$rkey] );
962 * This unserialize is the exact reason that we only want to
963 * process data after having read until "END" (instead of doing
964 * this right away): "unserialize" can trigger outside code:
965 * in the event that $ret[$rkey] is a serialized object,
966 * unserializing it will trigger __wakeup() if present. If that
967 * function attempted to read from memcached (while we did not
968 * yet read "END"), these 2 calls would collide.
970 if ( $flags & self::SERIALIZED ) {
971 $ret[$rkey] = unserialize( $ret[$rkey] );
975 return true;
976 } else {
977 $this->_handle_error( $sock, 'Error parsing response from $1' );
978 return false;
983 // }}}
984 // {{{ _set()
987 * Performs the requested storage operation to the memcache server
989 * @param string $cmd Command to perform
990 * @param string $key Key to act on
991 * @param mixed $val What we need to store
992 * @param int $exp (optional) Expiration time. This can be a number of seconds
993 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
994 * longer must be the timestamp of the time at which the mapping should expire. It
995 * is safe to use timestamps in all cases, regardless of exipration
996 * eg: strtotime("+3 hour")
997 * @param float $casToken [optional]
999 * @return bool
1000 * @access private
1002 function _set( $cmd, $key, $val, $exp, $casToken = null ) {
1003 if ( !$this->_active ) {
1004 return false;
1007 $sock = $this->get_sock( $key );
1008 if ( !is_resource( $sock ) ) {
1009 return false;
1012 if ( isset( $this->stats[$cmd] ) ) {
1013 $this->stats[$cmd]++;
1014 } else {
1015 $this->stats[$cmd] = 1;
1018 $flags = 0;
1020 if ( !is_scalar( $val ) ) {
1021 $val = serialize( $val );
1022 $flags |= self::SERIALIZED;
1023 if ( $this->_debug ) {
1024 $this->_debugprint( sprintf( "client: serializing data as it is not scalar\n" ) );
1028 $len = strlen( $val );
1030 if ( $this->_have_zlib && $this->_compress_enable
1031 && $this->_compress_threshold && $len >= $this->_compress_threshold
1033 $c_val = gzcompress( $val, 9 );
1034 $c_len = strlen( $c_val );
1036 if ( $c_len < $len * ( 1 - self::COMPRESSION_SAVINGS ) ) {
1037 if ( $this->_debug ) {
1038 $this->_debugprint( sprintf( "client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len ) );
1040 $val = $c_val;
1041 $len = $c_len;
1042 $flags |= self::COMPRESSED;
1046 $command = "$cmd $key $flags $exp $len";
1047 if ( $casToken ) {
1048 $command .= " $casToken";
1051 if ( !$this->_fwrite( $sock, "$command\r\n$val\r\n" ) ) {
1052 return false;
1055 $line = $this->_fgets( $sock );
1057 if ( $this->_debug ) {
1058 $this->_debugprint( sprintf( "%s %s (%s)\n", $cmd, $key, $line ) );
1060 if ( $line == "STORED" ) {
1061 return true;
1063 return false;
1066 // }}}
1067 // {{{ sock_to_host()
1070 * Returns the socket for the host
1072 * @param string $host Host:IP to get socket for
1074 * @return Resource|bool IO Stream or false
1075 * @access private
1077 function sock_to_host( $host ) {
1078 if ( isset( $this->_cache_sock[$host] ) ) {
1079 return $this->_cache_sock[$host];
1082 $sock = null;
1083 $now = time();
1084 list( $ip, /* $port */) = explode( ':', $host );
1085 if ( isset( $this->_host_dead[$host] ) && $this->_host_dead[$host] > $now ||
1086 isset( $this->_host_dead[$ip] ) && $this->_host_dead[$ip] > $now
1088 return null;
1091 if ( !$this->_connect_sock( $sock, $host ) ) {
1092 return null;
1095 // Do not buffer writes
1096 stream_set_write_buffer( $sock, 0 );
1098 $this->_cache_sock[$host] = $sock;
1100 return $this->_cache_sock[$host];
1104 * @param string $text
1106 function _debugprint( $text ) {
1107 wfDebugLog( 'memcached', $text );
1111 * @param string $text
1113 function _error_log( $text ) {
1114 wfDebugLog( 'memcached-serious', "Memcached error: $text" );
1118 * Write to a stream. If there is an error, mark the socket dead.
1120 * @param Resource $sock The socket
1121 * @param string $buf The string to write
1122 * @return bool True on success, false on failure
1124 function _fwrite( $sock, $buf ) {
1125 $bytesWritten = 0;
1126 $bufSize = strlen( $buf );
1127 while ( $bytesWritten < $bufSize ) {
1128 $result = fwrite( $sock, $buf );
1129 $data = stream_get_meta_data( $sock );
1130 if ( $data['timed_out'] ) {
1131 $this->_handle_error( $sock, 'timeout writing to $1' );
1132 return false;
1134 // Contrary to the documentation, fwrite() returns zero on error in PHP 5.3.
1135 if ( $result === false || $result === 0 ) {
1136 $this->_handle_error( $sock, 'error writing to $1' );
1137 return false;
1139 $bytesWritten += $result;
1142 return true;
1146 * Handle an I/O error. Mark the socket dead and log an error.
1148 * @param Resource $sock
1149 * @param string $msg
1151 function _handle_error( $sock, $msg ) {
1152 $peer = stream_socket_get_name( $sock, true /** remote **/ );
1153 if ( strval( $peer ) === '' ) {
1154 $peer = array_search( $sock, $this->_cache_sock );
1155 if ( $peer === false ) {
1156 $peer = '[unknown host]';
1159 $msg = str_replace( '$1', $peer, $msg );
1160 $this->_error_log( "$msg\n" );
1161 $this->_dead_sock( $sock );
1165 * Read the specified number of bytes from a stream. If there is an error,
1166 * mark the socket dead.
1168 * @param Resource $sock The socket
1169 * @param int $len The number of bytes to read
1170 * @return string|bool The string on success, false on failure.
1172 function _fread( $sock, $len ) {
1173 $buf = '';
1174 while ( $len > 0 ) {
1175 $result = fread( $sock, $len );
1176 $data = stream_get_meta_data( $sock );
1177 if ( $data['timed_out'] ) {
1178 $this->_handle_error( $sock, 'timeout reading from $1' );
1179 return false;
1181 if ( $result === false ) {
1182 $this->_handle_error( $sock, 'error reading buffer from $1' );
1183 return false;
1185 if ( $result === '' ) {
1186 // This will happen if the remote end of the socket is shut down
1187 $this->_handle_error( $sock, 'unexpected end of file reading from $1' );
1188 return false;
1190 $len -= strlen( $result );
1191 $buf .= $result;
1193 return $buf;
1197 * Read a line from a stream. If there is an error, mark the socket dead.
1198 * The \r\n line ending is stripped from the response.
1200 * @param Resource $sock The socket
1201 * @return string|bool The string on success, false on failure
1203 function _fgets( $sock ) {
1204 $result = fgets( $sock );
1205 // fgets() may return a partial line if there is a select timeout after
1206 // a successful recv(), so we have to check for a timeout even if we
1207 // got a string response.
1208 $data = stream_get_meta_data( $sock );
1209 if ( $data['timed_out'] ) {
1210 $this->_handle_error( $sock, 'timeout reading line from $1' );
1211 return false;
1213 if ( $result === false ) {
1214 $this->_handle_error( $sock, 'error reading line from $1' );
1215 return false;
1217 if ( substr( $result, -2 ) === "\r\n" ) {
1218 $result = substr( $result, 0, -2 );
1219 } elseif ( substr( $result, -1 ) === "\n" ) {
1220 $result = substr( $result, 0, -1 );
1221 } else {
1222 $this->_handle_error( $sock, 'line ending missing in response from $1' );
1223 return false;
1225 return $result;
1229 * Flush the read buffer of a stream
1230 * @param Resource $f
1232 function _flush_read_buffer( $f ) {
1233 if ( !is_resource( $f ) ) {
1234 return;
1236 $r = array( $f );
1237 $w = null;
1238 $e = null;
1239 $n = stream_select( $r, $w, $e, 0, 0 );
1240 while ( $n == 1 && !feof( $f ) ) {
1241 fread( $f, 1024 );
1242 $r = array( $f );
1243 $w = null;
1244 $e = null;
1245 $n = stream_select( $r, $w, $e, 0, 0 );
1249 // }}}
1250 // }}}
1251 // }}}
1254 // }}}
1256 class MemCachedClientforWiki extends MWMemcached {