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