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