Merge "Revert "Remove old compat methods/functions for mb_ functions""
[mediawiki.git] / includes / objectcache / MemcachedClient.php
blob9602ffec919c36aadd6bf7a7efd54d74df16e59e
1 <?php
2 /**
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. |
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 * +---------------------------------------------------------------------------+
38 * @file
39 * $TCAnet$
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/
46 * Usage example:
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),
53 * '127.0.0.1:10020'),
54 * 'debug' => false,
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>
63 * @version 0.1.2
66 // {{{ requirements
67 // }}}
69 // {{{ class MWMemcached
70 /**
71 * memcached client class implemented using (p)fsockopen()
73 * @author Ryan T. Dean <rtdean@cytherianage.net>
74 * @ingroup Cache
76 class MWMemcached {
77 // {{{ properties
78 // {{{ public
80 // {{{ constants
81 // {{{ flags
83 /**
84 * Flag: indicates data is serialized
86 const SERIALIZED = 1;
88 /**
89 * Flag: indicates data is compressed
91 const COMPRESSED = 2;
93 // }}}
95 /**
96 * Minimum savings to store data compressed
98 const COMPRESSION_SAVINGS = 0.20;
100 // }}}
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 boolean
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 boolean
142 * @access private
144 var $_have_zlib;
147 * Do we want to use compression?
149 * @var boolean
150 * @access private
152 var $_compress_enable;
155 * At how many bytes should we compress?
157 * @var integer
158 * @access private
160 var $_compress_threshold;
163 * Are we using persistent links?
165 * @var boolean
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 integer
198 * @access private
200 var $_bucketcount;
203 * # of total servers we have
205 * @var integer
206 * @access private
208 var $_active;
211 * Stream timeout in seconds. Applies for example to fread()
213 * @var integer
214 * @access private
216 var $_timeout_seconds;
219 * Stream timeout in microseconds
221 * @var integer
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 $args Array 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'] : 100000;
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 $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")
283 * @return Boolean
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 $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 );
304 // }}}
305 // {{{ delete()
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 ) {
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->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
334 $this->_dead_sock( $sock );
335 return false;
337 $res = trim( fgets( $sock ) );
339 if ( $this->_debug ) {
340 $this->_debugprint( sprintf( "MemCache: delete %s (%s)\n", $key, $res ) );
343 if ( $res == "DELETED" || $res == "NOT_FOUND" ) {
344 return true;
347 return false;
351 * @param $key
352 * @param $timeout int
353 * @return bool
355 public function lock( $key, $timeout = 0 ) {
356 /* stub */
357 return true;
361 * @param $key
362 * @return bool
364 public function unlock( $key ) {
365 /* stub */
366 return true;
369 // }}}
370 // {{{ disconnect_all()
373 * Disconnects all connected sockets
375 public function disconnect_all() {
376 foreach ( $this->_cache_sock as $sock ) {
377 fclose( $sock );
380 $this->_cache_sock = array();
383 // }}}
384 // {{{ enable_compress()
387 * Enable / Disable compression
389 * @param $enable Boolean: TRUE to enable, FALSE to disable
391 public function enable_compress( $enable ) {
392 $this->_compress_enable = $enable;
395 // }}}
396 // {{{ forget_dead_hosts()
399 * Forget about all of the dead hosts
401 public function forget_dead_hosts() {
402 $this->_host_dead = array();
405 // }}}
406 // {{{ get()
409 * Retrieves the value associated with the key from the memcache server
411 * @param $key array|string key to retrieve
413 * @return Mixed
415 public function get( $key ) {
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 = "get $key\r\n";
442 if ( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
443 $this->_dead_sock( $sock );
444 wfProfileOut( __METHOD__ );
445 return false;
448 $val = array();
449 $this->_load_items( $sock, $val );
451 if ( $this->_debug ) {
452 foreach ( $val as $k => $v ) {
453 $this->_debugprint( sprintf( "MemCache: sock %s got %s\n", serialize( $sock ), $k ) );
457 $value = false;
458 if ( isset( $val[$key] ) ) {
459 $value = $val[$key];
461 wfProfileOut( __METHOD__ );
462 return $value;
465 // }}}
466 // {{{ get_multi()
469 * Get multiple keys from the server(s)
471 * @param $keys Array: keys to retrieve
473 * @return Array
475 public function get_multi( $keys ) {
476 if ( !$this->_active ) {
477 return false;
480 if ( isset( $this->stats['get_multi'] ) ) {
481 $this->stats['get_multi']++;
482 } else {
483 $this->stats['get_multi'] = 1;
485 $sock_keys = array();
486 $socks = array();
487 foreach ( $keys as $key ) {
488 $sock = $this->get_sock( $key );
489 if ( !is_resource( $sock ) ) {
490 continue;
492 $key = is_array( $key ) ? $key[1] : $key;
493 if ( !isset( $sock_keys[$sock] ) ) {
494 $sock_keys[$sock] = array();
495 $socks[] = $sock;
497 $sock_keys[$sock][] = $key;
500 $gather = array();
501 // Send out the requests
502 foreach ( $socks as $sock ) {
503 $cmd = 'get';
504 foreach ( $sock_keys[$sock] as $key ) {
505 $cmd .= ' ' . $key;
507 $cmd .= "\r\n";
509 if ( $this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
510 $gather[] = $sock;
511 } else {
512 $this->_dead_sock( $sock );
516 // Parse responses
517 $val = array();
518 foreach ( $gather as $sock ) {
519 $this->_load_items( $sock, $val );
522 if ( $this->_debug ) {
523 foreach ( $val as $k => $v ) {
524 $this->_debugprint( sprintf( "MemCache: got %s\n", $k ) );
528 return $val;
531 // }}}
532 // {{{ incr()
535 * Increments $key (optionally) by $amt
537 * @param $key String: key to increment
538 * @param $amt Integer: (optional) amount to increment
540 * @return Integer: null if the key does not exist yet (this does NOT
541 * create new mappings if the key does not exist). If the key does
542 * exist, this returns the new value for that key.
544 public function incr( $key, $amt = 1 ) {
545 return $this->_incrdecr( 'incr', $key, $amt );
548 // }}}
549 // {{{ replace()
552 * Overwrites an existing value for key; only works if key is already set
554 * @param $key String: key to set value as
555 * @param $value Mixed: value to store
556 * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
557 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
558 * longer must be the timestamp of the time at which the mapping should expire. It
559 * is safe to use timestamps in all cases, regardless of exipration
560 * eg: strtotime("+3 hour")
562 * @return Boolean
564 public function replace( $key, $value, $exp = 0 ) {
565 return $this->_set( 'replace', $key, $value, $exp );
568 // }}}
569 // {{{ run_command()
572 * Passes through $cmd to the memcache server connected by $sock; returns
573 * output as an array (null array if no output)
575 * NOTE: due to a possible bug in how PHP reads while using fgets(), each
576 * line may not be terminated by a \r\n. More specifically, my testing
577 * has shown that, on FreeBSD at least, each line is terminated only
578 * with a \n. This is with the PHP flag auto_detect_line_endings set
579 * to falase (the default).
581 * @param $sock Resource: socket to send command on
582 * @param $cmd String: command to run
584 * @return Array: output array
586 public function run_command( $sock, $cmd ) {
587 if ( !is_resource( $sock ) ) {
588 return array();
591 if ( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
592 return array();
595 $ret = array();
596 while ( true ) {
597 $res = fgets( $sock );
598 $ret[] = $res;
599 if ( preg_match( '/^END/', $res ) ) {
600 break;
602 if ( strlen( $res ) == 0 ) {
603 break;
606 return $ret;
609 // }}}
610 // {{{ set()
613 * Unconditionally sets a key to a given value in the memcache. Returns true
614 * if set successfully.
616 * @param $key String: key to set value as
617 * @param $value Mixed: value to set
618 * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
619 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
620 * longer must be the timestamp of the time at which the mapping should expire. It
621 * is safe to use timestamps in all cases, regardless of exipration
622 * eg: strtotime("+3 hour")
624 * @return Boolean: TRUE on success
626 public function set( $key, $value, $exp = 0 ) {
627 return $this->_set( 'set', $key, $value, $exp );
630 // }}}
631 // {{{ set_compress_threshold()
634 * Sets the compression threshold
636 * @param $thresh Integer: threshold to compress if larger than
638 public function set_compress_threshold( $thresh ) {
639 $this->_compress_threshold = $thresh;
642 // }}}
643 // {{{ set_debug()
646 * Sets the debug flag
648 * @param $dbg Boolean: TRUE for debugging, FALSE otherwise
650 * @see MWMemcached::__construct
652 public function set_debug( $dbg ) {
653 $this->_debug = $dbg;
656 // }}}
657 // {{{ set_servers()
660 * Sets the server list to distribute key gets and puts between
662 * @param $list Array of servers to connect to
664 * @see MWMemcached::__construct()
666 public function set_servers( $list ) {
667 $this->_servers = $list;
668 $this->_active = count( $list );
669 $this->_buckets = null;
670 $this->_bucketcount = 0;
672 $this->_single_sock = null;
673 if ( $this->_active == 1 ) {
674 $this->_single_sock = $this->_servers[0];
679 * Sets the timeout for new connections
681 * @param $seconds Integer: number of seconds
682 * @param $microseconds Integer: number of microseconds
684 public function set_timeout( $seconds, $microseconds ) {
685 $this->_timeout_seconds = $seconds;
686 $this->_timeout_microseconds = $microseconds;
689 // }}}
690 // }}}
691 // {{{ private methods
692 // {{{ _close_sock()
695 * Close the specified socket
697 * @param $sock String: socket to close
699 * @access private
701 function _close_sock( $sock ) {
702 $host = array_search( $sock, $this->_cache_sock );
703 fclose( $this->_cache_sock[$host] );
704 unset( $this->_cache_sock[$host] );
707 // }}}
708 // {{{ _connect_sock()
711 * Connects $sock to $host, timing out after $timeout
713 * @param $sock Integer: socket to connect
714 * @param $host String: Host:IP to connect to
716 * @return boolean
717 * @access private
719 function _connect_sock( &$sock, $host ) {
720 list( $ip, $port ) = explode( ':', $host );
721 $sock = false;
722 $timeout = $this->_connect_timeout;
723 $errno = $errstr = null;
724 for( $i = 0; !$sock && $i < $this->_connect_attempts; $i++ ) {
725 wfSuppressWarnings();
726 if ( $this->_persistent == 1 ) {
727 $sock = pfsockopen( $ip, $port, $errno, $errstr, $timeout );
728 } else {
729 $sock = fsockopen( $ip, $port, $errno, $errstr, $timeout );
731 wfRestoreWarnings();
733 if ( !$sock ) {
734 if ( $this->_debug ) {
735 $this->_debugprint( "Error connecting to $host: $errstr\n" );
737 return false;
740 // Initialise timeout
741 stream_set_timeout( $sock, $this->_timeout_seconds, $this->_timeout_microseconds );
743 return true;
746 // }}}
747 // {{{ _dead_sock()
750 * Marks a host as dead until 30-40 seconds in the future
752 * @param $sock String: socket to mark as dead
754 * @access private
756 function _dead_sock( $sock ) {
757 $host = array_search( $sock, $this->_cache_sock );
758 $this->_dead_host( $host );
762 * @param $host
764 function _dead_host( $host ) {
765 $parts = explode( ':', $host );
766 $ip = $parts[0];
767 $this->_host_dead[$ip] = time() + 30 + intval( rand( 0, 10 ) );
768 $this->_host_dead[$host] = $this->_host_dead[$ip];
769 unset( $this->_cache_sock[$host] );
772 // }}}
773 // {{{ get_sock()
776 * get_sock
778 * @param $key String: key to retrieve value for;
780 * @return Mixed: resource on success, false on failure
781 * @access private
783 function get_sock( $key ) {
784 if ( !$this->_active ) {
785 return false;
788 if ( $this->_single_sock !== null ) {
789 $this->_flush_read_buffer( $this->_single_sock );
790 return $this->sock_to_host( $this->_single_sock );
793 $hv = is_array( $key ) ? intval( $key[0] ) : $this->_hashfunc( $key );
794 if ( $this->_buckets === null ) {
795 $bu = array();
796 foreach ( $this->_servers as $v ) {
797 if ( is_array( $v ) ) {
798 for( $i = 0; $i < $v[1]; $i++ ) {
799 $bu[] = $v[0];
801 } else {
802 $bu[] = $v;
805 $this->_buckets = $bu;
806 $this->_bucketcount = count( $bu );
809 $realkey = is_array( $key ) ? $key[1] : $key;
810 for( $tries = 0; $tries < 20; $tries++ ) {
811 $host = $this->_buckets[$hv % $this->_bucketcount];
812 $sock = $this->sock_to_host( $host );
813 if ( is_resource( $sock ) ) {
814 $this->_flush_read_buffer( $sock );
815 return $sock;
817 $hv = $this->_hashfunc( $hv . $realkey );
820 return false;
823 // }}}
824 // {{{ _hashfunc()
827 * Creates a hash integer based on the $key
829 * @param $key String: key to hash
831 * @return Integer: hash value
832 * @access private
834 function _hashfunc( $key ) {
835 # Hash function must be in [0,0x7ffffff]
836 # We take the first 31 bits of the MD5 hash, which unlike the hash
837 # function used in a previous version of this client, works
838 return hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
841 // }}}
842 // {{{ _incrdecr()
845 * Perform increment/decriment on $key
847 * @param $cmd String command to perform
848 * @param $key String|array key to perform it on
849 * @param $amt Integer amount to adjust
851 * @return Integer: new value of $key
852 * @access private
854 function _incrdecr( $cmd, $key, $amt = 1 ) {
855 if ( !$this->_active ) {
856 return null;
859 $sock = $this->get_sock( $key );
860 if ( !is_resource( $sock ) ) {
861 return null;
864 $key = is_array( $key ) ? $key[1] : $key;
865 if ( isset( $this->stats[$cmd] ) ) {
866 $this->stats[$cmd]++;
867 } else {
868 $this->stats[$cmd] = 1;
870 if ( !$this->_safe_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
871 $this->_dead_sock( $sock );
872 return null;
875 $line = fgets( $sock );
876 $match = array();
877 if ( !preg_match( '/^(\d+)/', $line, $match ) ) {
878 return null;
880 return $match[1];
883 // }}}
884 // {{{ _load_items()
887 * Load items into $ret from $sock
889 * @param $sock Resource: socket to read from
890 * @param $ret Array: returned values
892 * @return bool|int
893 * @access private
895 function _load_items( $sock, &$ret ) {
896 while ( 1 ) {
897 $decl = fgets( $sock );
898 if ( $decl == "END\r\n" ) {
899 return true;
900 } elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match ) ) {
901 list( $rkey, $flags, $len ) = array( $match[1], $match[2], $match[3] );
902 $bneed = $len + 2;
903 $offset = 0;
905 while ( $bneed > 0 ) {
906 $data = fread( $sock, $bneed );
907 $n = strlen( $data );
908 if ( $n == 0 ) {
909 break;
911 $offset += $n;
912 $bneed -= $n;
913 if ( isset( $ret[$rkey] ) ) {
914 $ret[$rkey] .= $data;
915 } else {
916 $ret[$rkey] = $data;
920 if ( $offset != $len + 2 ) {
921 // Something is borked!
922 if ( $this->_debug ) {
923 $this->_debugprint( sprintf( "Something is borked! key %s expecting %d got %d length\n", $rkey, $len + 2, $offset ) );
926 unset( $ret[$rkey] );
927 $this->_close_sock( $sock );
928 return false;
931 if ( $this->_have_zlib && $flags & self::COMPRESSED ) {
932 $ret[$rkey] = gzuncompress( $ret[$rkey] );
935 $ret[$rkey] = rtrim( $ret[$rkey] );
937 if ( $flags & self::SERIALIZED ) {
938 $ret[$rkey] = unserialize( $ret[$rkey] );
941 } else {
942 $this->_debugprint( "Error parsing memcached response\n" );
943 return 0;
948 // }}}
949 // {{{ _set()
952 * Performs the requested storage operation to the memcache server
954 * @param $cmd String: command to perform
955 * @param $key String: key to act on
956 * @param $val Mixed: what we need to store
957 * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
958 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
959 * longer must be the timestamp of the time at which the mapping should expire. It
960 * is safe to use timestamps in all cases, regardless of exipration
961 * eg: strtotime("+3 hour")
963 * @return Boolean
964 * @access private
966 function _set( $cmd, $key, $val, $exp ) {
967 if ( !$this->_active ) {
968 return false;
971 $sock = $this->get_sock( $key );
972 if ( !is_resource( $sock ) ) {
973 return false;
976 if ( isset( $this->stats[$cmd] ) ) {
977 $this->stats[$cmd]++;
978 } else {
979 $this->stats[$cmd] = 1;
982 $flags = 0;
984 if ( !is_scalar( $val ) ) {
985 $val = serialize( $val );
986 $flags |= self::SERIALIZED;
987 if ( $this->_debug ) {
988 $this->_debugprint( sprintf( "client: serializing data as it is not scalar\n" ) );
992 $len = strlen( $val );
994 if ( $this->_have_zlib && $this->_compress_enable &&
995 $this->_compress_threshold && $len >= $this->_compress_threshold )
997 $c_val = gzcompress( $val, 9 );
998 $c_len = strlen( $c_val );
1000 if ( $c_len < $len * ( 1 - self::COMPRESSION_SAVINGS ) ) {
1001 if ( $this->_debug ) {
1002 $this->_debugprint( sprintf( "client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len ) );
1004 $val = $c_val;
1005 $len = $c_len;
1006 $flags |= self::COMPRESSED;
1009 if ( !$this->_safe_fwrite( $sock, "$cmd $key $flags $exp $len\r\n$val\r\n" ) ) {
1010 $this->_dead_sock( $sock );
1011 return false;
1014 $line = trim( fgets( $sock ) );
1016 if ( $this->_debug ) {
1017 $this->_debugprint( sprintf( "%s %s (%s)\n", $cmd, $key, $line ) );
1019 if ( $line == "STORED" ) {
1020 return true;
1022 return false;
1025 // }}}
1026 // {{{ sock_to_host()
1029 * Returns the socket for the host
1031 * @param $host String: Host:IP to get socket for
1033 * @return Mixed: IO Stream or false
1034 * @access private
1036 function sock_to_host( $host ) {
1037 if ( isset( $this->_cache_sock[$host] ) ) {
1038 return $this->_cache_sock[$host];
1041 $sock = null;
1042 $now = time();
1043 list( $ip, /* $port */) = explode( ':', $host );
1044 if ( isset( $this->_host_dead[$host] ) && $this->_host_dead[$host] > $now ||
1045 isset( $this->_host_dead[$ip] ) && $this->_host_dead[$ip] > $now
1047 return null;
1050 if ( !$this->_connect_sock( $sock, $host ) ) {
1051 $this->_dead_host( $host );
1052 return null;
1055 // Do not buffer writes
1056 stream_set_write_buffer( $sock, 0 );
1058 $this->_cache_sock[$host] = $sock;
1060 return $this->_cache_sock[$host];
1064 * @param $str string
1066 function _debugprint( $str ) {
1067 print( $str );
1071 * Write to a stream, timing out after the correct amount of time
1073 * @return Boolean: false on failure, true on success
1076 function _safe_fwrite( $f, $buf, $len = false ) {
1077 stream_set_blocking( $f, 0 );
1079 if ( $len === false ) {
1080 wfDebug( "Writing " . strlen( $buf ) . " bytes\n" );
1081 $bytesWritten = fwrite( $f, $buf );
1082 } else {
1083 wfDebug( "Writing $len bytes\n" );
1084 $bytesWritten = fwrite( $f, $buf, $len );
1086 $n = stream_select( $r = null, $w = array( $f ), $e = null, 10, 0 );
1087 # $this->_timeout_seconds, $this->_timeout_microseconds );
1089 wfDebug( "stream_select returned $n\n" );
1090 stream_set_blocking( $f, 1 );
1091 return $n == 1;
1092 return $bytesWritten;
1096 * Original behaviour
1097 * @param $f
1098 * @param $buf
1099 * @param $len bool
1100 * @return int
1102 function _safe_fwrite( $f, $buf, $len = false ) {
1103 if ( $len === false ) {
1104 $bytesWritten = fwrite( $f, $buf );
1105 } else {
1106 $bytesWritten = fwrite( $f, $buf, $len );
1108 return $bytesWritten;
1112 * Flush the read buffer of a stream
1113 * @param $f Resource
1115 function _flush_read_buffer( $f ) {
1116 if ( !is_resource( $f ) ) {
1117 return;
1119 $n = stream_select( $r = array( $f ), $w = null, $e = null, 0, 0 );
1120 while ( $n == 1 && !feof( $f ) ) {
1121 fread( $f, 1024 );
1122 $n = stream_select( $r = array( $f ), $w = null, $e = null, 0, 0 );
1126 // }}}
1127 // }}}
1128 // }}}
1131 // vim: sts=3 sw=3 et
1133 // }}}
1135 class MemCachedClientforWiki extends MWMemcached {
1137 function _debugprint( $text ) {
1138 global $wgDebugLogGroups;
1139 if( !isset( $wgDebugLogGroups['memcached'] ) ) {
1140 # Prefix message since it will end up in main debug log file
1141 $text = "memcached: $text";
1143 wfDebugLog( 'memcached', $text );