Merge "Support mustache partials in server-side templates"
[lhc/web/wiklou.git] / includes / objectcache / MemcachedClient.php
1 <?php
2 // @codingStandardsIgnoreFile It's an external lib and it isn't. Let's not bother.
3 /**
4 * Memcached client for PHP.
5 *
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 * +---------------------------------------------------------------------------+
38 *
39 * @file
40 * $TCAnet$
41 */
42
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/
46 *
47 * Usage example:
48 *
49 * require_once 'memcached.php';
50 *
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));
58 *
59 * $mc->add( 'key', array( 'some', 'array' ) );
60 * $mc->replace( 'key', 'some random string' );
61 * $val = $mc->get( 'key' );
62 *
63 * @author Ryan T. Dean <rtdean@cytherianage.net>
64 * @version 0.1.2
65 */
66
67 use Psr\Log\LoggerInterface;
68 use Psr\Log\NullLogger;
69
70 // {{{ requirements
71 // }}}
72
73 // {{{ class MWMemcached
74 /**
75 * memcached client class implemented using (p)fsockopen()
76 *
77 * @author Ryan T. Dean <rtdean@cytherianage.net>
78 * @ingroup Cache
79 */
80 class MWMemcached {
81 // {{{ properties
82 // {{{ public
83
84 // {{{ constants
85 // {{{ flags
86
87 /**
88 * Flag: indicates data is serialized
89 */
90 const SERIALIZED = 1;
91
92 /**
93 * Flag: indicates data is compressed
94 */
95 const COMPRESSED = 2;
96
97 /**
98 * Flag: indicates data is an integer
99 */
100 const INTVAL = 4;
101
102 // }}}
103
104 /**
105 * Minimum savings to store data compressed
106 */
107 const COMPRESSION_SAVINGS = 0.20;
108
109 // }}}
110
111 /**
112 * Command statistics
113 *
114 * @var array
115 * @access public
116 */
117 public $stats;
118
119 // }}}
120 // {{{ private
121
122 /**
123 * Cached Sockets that are connected
124 *
125 * @var array
126 * @access private
127 */
128 public $_cache_sock;
129
130 /**
131 * Current debug status; 0 - none to 9 - profiling
132 *
133 * @var bool
134 * @access private
135 */
136 public $_debug;
137
138 /**
139 * Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'
140 *
141 * @var array
142 * @access private
143 */
144 public $_host_dead;
145
146 /**
147 * Is compression available?
148 *
149 * @var bool
150 * @access private
151 */
152 public $_have_zlib;
153
154 /**
155 * Do we want to use compression?
156 *
157 * @var bool
158 * @access private
159 */
160 public $_compress_enable;
161
162 /**
163 * At how many bytes should we compress?
164 *
165 * @var int
166 * @access private
167 */
168 public $_compress_threshold;
169
170 /**
171 * Are we using persistent links?
172 *
173 * @var bool
174 * @access private
175 */
176 public $_persistent;
177
178 /**
179 * If only using one server; contains ip:port to connect to
180 *
181 * @var string
182 * @access private
183 */
184 public $_single_sock;
185
186 /**
187 * Array containing ip:port or array(ip:port, weight)
188 *
189 * @var array
190 * @access private
191 */
192 public $_servers;
193
194 /**
195 * Our bit buckets
196 *
197 * @var array
198 * @access private
199 */
200 public $_buckets;
201
202 /**
203 * Total # of bit buckets we have
204 *
205 * @var int
206 * @access private
207 */
208 public $_bucketcount;
209
210 /**
211 * # of total servers we have
212 *
213 * @var int
214 * @access private
215 */
216 public $_active;
217
218 /**
219 * Stream timeout in seconds. Applies for example to fread()
220 *
221 * @var int
222 * @access private
223 */
224 public $_timeout_seconds;
225
226 /**
227 * Stream timeout in microseconds
228 *
229 * @var int
230 * @access private
231 */
232 public $_timeout_microseconds;
233
234 /**
235 * Connect timeout in seconds
236 */
237 public $_connect_timeout;
238
239 /**
240 * Number of connection attempts for each server
241 */
242 public $_connect_attempts;
243
244 /**
245 * @var LoggerInterface
246 */
247 private $_logger;
248
249 // }}}
250 // }}}
251 // {{{ methods
252 // {{{ public functions
253 // {{{ memcached()
254
255 /**
256 * Memcache initializer
257 *
258 * @param array $args Associative array of settings
259 *
260 * @return mixed
261 */
262 public function __construct( $args ) {
263 $this->set_servers( isset( $args['servers'] ) ? $args['servers'] : array() );
264 $this->_debug = isset( $args['debug'] ) ? $args['debug'] : false;
265 $this->stats = array();
266 $this->_compress_threshold = isset( $args['compress_threshold'] ) ? $args['compress_threshold'] : 0;
267 $this->_persistent = isset( $args['persistent'] ) ? $args['persistent'] : false;
268 $this->_compress_enable = true;
269 $this->_have_zlib = function_exists( 'gzcompress' );
270
271 $this->_cache_sock = array();
272 $this->_host_dead = array();
273
274 $this->_timeout_seconds = 0;
275 $this->_timeout_microseconds = isset( $args['timeout'] ) ? $args['timeout'] : 500000;
276
277 $this->_connect_timeout = isset( $args['connect_timeout'] ) ? $args['connect_timeout'] : 0.1;
278 $this->_connect_attempts = 2;
279
280 $this->_logger = isset( $args['logger'] ) ? $args['logger'] : new NullLogger();
281 }
282
283 // }}}
284 // {{{ add()
285
286 /**
287 * Adds a key/value to the memcache server if one isn't already set with
288 * that key
289 *
290 * @param string $key Key to set with data
291 * @param mixed $val Value to store
292 * @param int $exp (optional) Expiration time. This can be a number of seconds
293 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
294 * longer must be the timestamp of the time at which the mapping should expire. It
295 * is safe to use timestamps in all cases, regardless of expiration
296 * eg: strtotime("+3 hour")
297 *
298 * @return bool
299 */
300 public function add( $key, $val, $exp = 0 ) {
301 return $this->_set( 'add', $key, $val, $exp );
302 }
303
304 // }}}
305 // {{{ decr()
306
307 /**
308 * Decrease a value stored on the memcache server
309 *
310 * @param string $key Key to decrease
311 * @param int $amt (optional) amount to decrease
312 *
313 * @return mixed False on failure, value on success
314 */
315 public function decr( $key, $amt = 1 ) {
316 return $this->_incrdecr( 'decr', $key, $amt );
317 }
318
319 // }}}
320 // {{{ delete()
321
322 /**
323 * Deletes a key from the server, optionally after $time
324 *
325 * @param string $key Key to delete
326 * @param int $time (optional) how long to wait before deleting
327 *
328 * @return bool True on success, false on failure
329 */
330 public function delete( $key, $time = 0 ) {
331 if ( !$this->_active ) {
332 return false;
333 }
334
335 $sock = $this->get_sock( $key );
336 if ( !is_resource( $sock ) ) {
337 return false;
338 }
339
340 $key = is_array( $key ) ? $key[1] : $key;
341
342 if ( isset( $this->stats['delete'] ) ) {
343 $this->stats['delete']++;
344 } else {
345 $this->stats['delete'] = 1;
346 }
347 $cmd = "delete $key $time\r\n";
348 if ( !$this->_fwrite( $sock, $cmd ) ) {
349 return false;
350 }
351 $res = $this->_fgets( $sock );
352
353 if ( $this->_debug ) {
354 $this->_debugprint( sprintf( "MemCache: delete %s (%s)\n", $key, $res ) );
355 }
356
357 if ( $res == "DELETED" || $res == "NOT_FOUND" ) {
358 return true;
359 }
360
361 return false;
362 }
363
364 /**
365 * @param string $key
366 * @param int $timeout
367 * @return bool
368 */
369 public function lock( $key, $timeout = 0 ) {
370 /* stub */
371 return true;
372 }
373
374 /**
375 * @param string $key
376 * @return bool
377 */
378 public function unlock( $key ) {
379 /* stub */
380 return true;
381 }
382
383 // }}}
384 // {{{ disconnect_all()
385
386 /**
387 * Disconnects all connected sockets
388 */
389 public function disconnect_all() {
390 foreach ( $this->_cache_sock as $sock ) {
391 fclose( $sock );
392 }
393
394 $this->_cache_sock = array();
395 }
396
397 // }}}
398 // {{{ enable_compress()
399
400 /**
401 * Enable / Disable compression
402 *
403 * @param bool $enable True to enable, false to disable
404 */
405 public function enable_compress( $enable ) {
406 $this->_compress_enable = $enable;
407 }
408
409 // }}}
410 // {{{ forget_dead_hosts()
411
412 /**
413 * Forget about all of the dead hosts
414 */
415 public function forget_dead_hosts() {
416 $this->_host_dead = array();
417 }
418
419 // }}}
420 // {{{ get()
421
422 /**
423 * Retrieves the value associated with the key from the memcache server
424 *
425 * @param array|string $key key to retrieve
426 * @param float $casToken [optional]
427 *
428 * @return mixed
429 */
430 public function get( $key, &$casToken = null ) {
431
432 if ( $this->_debug ) {
433 $this->_debugprint( "get($key)\n" );
434 }
435
436 if ( !is_array( $key ) && strval( $key ) === '' ) {
437 $this->_debugprint( "Skipping key which equals to an empty string" );
438 return false;
439 }
440
441 if ( !$this->_active ) {
442 return false;
443 }
444
445 $sock = $this->get_sock( $key );
446
447 if ( !is_resource( $sock ) ) {
448 return false;
449 }
450
451 $key = is_array( $key ) ? $key[1] : $key;
452 if ( isset( $this->stats['get'] ) ) {
453 $this->stats['get']++;
454 } else {
455 $this->stats['get'] = 1;
456 }
457
458 $cmd = "gets $key\r\n";
459 if ( !$this->_fwrite( $sock, $cmd ) ) {
460 return false;
461 }
462
463 $val = array();
464 $this->_load_items( $sock, $val, $casToken );
465
466 if ( $this->_debug ) {
467 foreach ( $val as $k => $v ) {
468 $this->_debugprint( sprintf( "MemCache: sock %s got %s\n", serialize( $sock ), $k ) );
469 }
470 }
471
472 $value = false;
473 if ( isset( $val[$key] ) ) {
474 $value = $val[$key];
475 }
476 return $value;
477 }
478
479 // }}}
480 // {{{ get_multi()
481
482 /**
483 * Get multiple keys from the server(s)
484 *
485 * @param array $keys Keys to retrieve
486 *
487 * @return array
488 */
489 public function get_multi( $keys ) {
490 if ( !$this->_active ) {
491 return false;
492 }
493
494 if ( isset( $this->stats['get_multi'] ) ) {
495 $this->stats['get_multi']++;
496 } else {
497 $this->stats['get_multi'] = 1;
498 }
499 $sock_keys = array();
500 $socks = array();
501 foreach ( $keys as $key ) {
502 $sock = $this->get_sock( $key );
503 if ( !is_resource( $sock ) ) {
504 continue;
505 }
506 $key = is_array( $key ) ? $key[1] : $key;
507 if ( !isset( $sock_keys[$sock] ) ) {
508 $sock_keys[intval( $sock )] = array();
509 $socks[] = $sock;
510 }
511 $sock_keys[intval( $sock )][] = $key;
512 }
513
514 $gather = array();
515 // Send out the requests
516 foreach ( $socks as $sock ) {
517 $cmd = 'gets';
518 foreach ( $sock_keys[intval( $sock )] as $key ) {
519 $cmd .= ' ' . $key;
520 }
521 $cmd .= "\r\n";
522
523 if ( $this->_fwrite( $sock, $cmd ) ) {
524 $gather[] = $sock;
525 }
526 }
527
528 // Parse responses
529 $val = array();
530 foreach ( $gather as $sock ) {
531 $this->_load_items( $sock, $val, $casToken );
532 }
533
534 if ( $this->_debug ) {
535 foreach ( $val as $k => $v ) {
536 $this->_debugprint( sprintf( "MemCache: got %s\n", $k ) );
537 }
538 }
539
540 return $val;
541 }
542
543 // }}}
544 // {{{ incr()
545
546 /**
547 * Increments $key (optionally) by $amt
548 *
549 * @param string $key Key to increment
550 * @param int $amt (optional) amount to increment
551 *
552 * @return int|null Null if the key does not exist yet (this does NOT
553 * create new mappings if the key does not exist). If the key does
554 * exist, this returns the new value for that key.
555 */
556 public function incr( $key, $amt = 1 ) {
557 return $this->_incrdecr( 'incr', $key, $amt );
558 }
559
560 // }}}
561 // {{{ replace()
562
563 /**
564 * Overwrites an existing value for key; only works if key is already set
565 *
566 * @param string $key Key to set value as
567 * @param mixed $value Value to store
568 * @param int $exp (optional) Expiration time. This can be a number of seconds
569 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
570 * longer must be the timestamp of the time at which the mapping should expire. It
571 * is safe to use timestamps in all cases, regardless of exipration
572 * eg: strtotime("+3 hour")
573 *
574 * @return bool
575 */
576 public function replace( $key, $value, $exp = 0 ) {
577 return $this->_set( 'replace', $key, $value, $exp );
578 }
579
580 // }}}
581 // {{{ run_command()
582
583 /**
584 * Passes through $cmd to the memcache server connected by $sock; returns
585 * output as an array (null array if no output)
586 *
587 * @param Resource $sock Socket to send command on
588 * @param string $cmd Command to run
589 *
590 * @return array Output array
591 */
592 public function run_command( $sock, $cmd ) {
593 if ( !is_resource( $sock ) ) {
594 return array();
595 }
596
597 if ( !$this->_fwrite( $sock, $cmd ) ) {
598 return array();
599 }
600
601 $ret = array();
602 while ( true ) {
603 $res = $this->_fgets( $sock );
604 $ret[] = $res;
605 if ( preg_match( '/^END/', $res ) ) {
606 break;
607 }
608 if ( strlen( $res ) == 0 ) {
609 break;
610 }
611 }
612 return $ret;
613 }
614
615 // }}}
616 // {{{ set()
617
618 /**
619 * Unconditionally sets a key to a given value in the memcache. Returns true
620 * if set successfully.
621 *
622 * @param string $key Key to set value as
623 * @param mixed $value Value to set
624 * @param int $exp (optional) Expiration time. This can be a number of seconds
625 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
626 * longer must be the timestamp of the time at which the mapping should expire. It
627 * is safe to use timestamps in all cases, regardless of exipration
628 * eg: strtotime("+3 hour")
629 *
630 * @return bool True on success
631 */
632 public function set( $key, $value, $exp = 0 ) {
633 return $this->_set( 'set', $key, $value, $exp );
634 }
635
636 // }}}
637 // {{{ cas()
638
639 /**
640 * Sets a key to a given value in the memcache if the current value still corresponds
641 * to a known, given value. Returns true if set successfully.
642 *
643 * @param float $casToken Current known value
644 * @param string $key Key to set value as
645 * @param mixed $value Value to set
646 * @param int $exp (optional) Expiration time. This can be a number of seconds
647 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
648 * longer must be the timestamp of the time at which the mapping should expire. It
649 * is safe to use timestamps in all cases, regardless of exipration
650 * eg: strtotime("+3 hour")
651 *
652 * @return bool True on success
653 */
654 public function cas( $casToken, $key, $value, $exp = 0 ) {
655 return $this->_set( 'cas', $key, $value, $exp, $casToken );
656 }
657
658 // }}}
659 // {{{ set_compress_threshold()
660
661 /**
662 * Sets the compression threshold
663 *
664 * @param int $thresh Threshold to compress if larger than
665 */
666 public function set_compress_threshold( $thresh ) {
667 $this->_compress_threshold = $thresh;
668 }
669
670 // }}}
671 // {{{ set_debug()
672
673 /**
674 * Sets the debug flag
675 *
676 * @param bool $dbg True for debugging, false otherwise
677 *
678 * @see MWMemcached::__construct
679 */
680 public function set_debug( $dbg ) {
681 $this->_debug = $dbg;
682 }
683
684 // }}}
685 // {{{ set_servers()
686
687 /**
688 * Sets the server list to distribute key gets and puts between
689 *
690 * @param array $list Array of servers to connect to
691 *
692 * @see MWMemcached::__construct()
693 */
694 public function set_servers( $list ) {
695 $this->_servers = $list;
696 $this->_active = count( $list );
697 $this->_buckets = null;
698 $this->_bucketcount = 0;
699
700 $this->_single_sock = null;
701 if ( $this->_active == 1 ) {
702 $this->_single_sock = $this->_servers[0];
703 }
704 }
705
706 /**
707 * Sets the timeout for new connections
708 *
709 * @param int $seconds Number of seconds
710 * @param int $microseconds Number of microseconds
711 */
712 public function set_timeout( $seconds, $microseconds ) {
713 $this->_timeout_seconds = $seconds;
714 $this->_timeout_microseconds = $microseconds;
715 }
716
717 // }}}
718 // }}}
719 // {{{ private methods
720 // {{{ _close_sock()
721
722 /**
723 * Close the specified socket
724 *
725 * @param string $sock Socket to close
726 *
727 * @access private
728 */
729 function _close_sock( $sock ) {
730 $host = array_search( $sock, $this->_cache_sock );
731 fclose( $this->_cache_sock[$host] );
732 unset( $this->_cache_sock[$host] );
733 }
734
735 // }}}
736 // {{{ _connect_sock()
737
738 /**
739 * Connects $sock to $host, timing out after $timeout
740 *
741 * @param int $sock Socket to connect
742 * @param string $host Host:IP to connect to
743 *
744 * @return bool
745 * @access private
746 */
747 function _connect_sock( &$sock, $host ) {
748 list( $ip, $port ) = preg_split( '/:(?=\d)/', $host );
749 $sock = false;
750 $timeout = $this->_connect_timeout;
751 $errno = $errstr = null;
752 for ( $i = 0; !$sock && $i < $this->_connect_attempts; $i++ ) {
753 MediaWiki\suppressWarnings();
754 if ( $this->_persistent == 1 ) {
755 $sock = pfsockopen( $ip, $port, $errno, $errstr, $timeout );
756 } else {
757 $sock = fsockopen( $ip, $port, $errno, $errstr, $timeout );
758 }
759 MediaWiki\restoreWarnings();
760 }
761 if ( !$sock ) {
762 $this->_error_log( "Error connecting to $host: $errstr\n" );
763 $this->_dead_host( $host );
764 return false;
765 }
766
767 // Initialise timeout
768 stream_set_timeout( $sock, $this->_timeout_seconds, $this->_timeout_microseconds );
769
770 // If the connection was persistent, flush the read buffer in case there
771 // was a previous incomplete request on this connection
772 if ( $this->_persistent ) {
773 $this->_flush_read_buffer( $sock );
774 }
775 return true;
776 }
777
778 // }}}
779 // {{{ _dead_sock()
780
781 /**
782 * Marks a host as dead until 30-40 seconds in the future
783 *
784 * @param string $sock Socket to mark as dead
785 *
786 * @access private
787 */
788 function _dead_sock( $sock ) {
789 $host = array_search( $sock, $this->_cache_sock );
790 $this->_dead_host( $host );
791 }
792
793 /**
794 * @param string $host
795 */
796 function _dead_host( $host ) {
797 $parts = explode( ':', $host );
798 $ip = $parts[0];
799 $this->_host_dead[$ip] = time() + 30 + intval( rand( 0, 10 ) );
800 $this->_host_dead[$host] = $this->_host_dead[$ip];
801 unset( $this->_cache_sock[$host] );
802 }
803
804 // }}}
805 // {{{ get_sock()
806
807 /**
808 * get_sock
809 *
810 * @param string $key Key to retrieve value for;
811 *
812 * @return Resource|bool Resource on success, false on failure
813 * @access private
814 */
815 function get_sock( $key ) {
816 if ( !$this->_active ) {
817 return false;
818 }
819
820 if ( $this->_single_sock !== null ) {
821 return $this->sock_to_host( $this->_single_sock );
822 }
823
824 $hv = is_array( $key ) ? intval( $key[0] ) : $this->_hashfunc( $key );
825 if ( $this->_buckets === null ) {
826 $bu = array();
827 foreach ( $this->_servers as $v ) {
828 if ( is_array( $v ) ) {
829 for ( $i = 0; $i < $v[1]; $i++ ) {
830 $bu[] = $v[0];
831 }
832 } else {
833 $bu[] = $v;
834 }
835 }
836 $this->_buckets = $bu;
837 $this->_bucketcount = count( $bu );
838 }
839
840 $realkey = is_array( $key ) ? $key[1] : $key;
841 for ( $tries = 0; $tries < 20; $tries++ ) {
842 $host = $this->_buckets[$hv % $this->_bucketcount];
843 $sock = $this->sock_to_host( $host );
844 if ( is_resource( $sock ) ) {
845 return $sock;
846 }
847 $hv = $this->_hashfunc( $hv . $realkey );
848 }
849
850 return false;
851 }
852
853 // }}}
854 // {{{ _hashfunc()
855
856 /**
857 * Creates a hash integer based on the $key
858 *
859 * @param string $key Key to hash
860 *
861 * @return int Hash value
862 * @access private
863 */
864 function _hashfunc( $key ) {
865 # Hash function must be in [0,0x7ffffff]
866 # We take the first 31 bits of the MD5 hash, which unlike the hash
867 # function used in a previous version of this client, works
868 return hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
869 }
870
871 // }}}
872 // {{{ _incrdecr()
873
874 /**
875 * Perform increment/decriment on $key
876 *
877 * @param string $cmd Command to perform
878 * @param string|array $key Key to perform it on
879 * @param int $amt Amount to adjust
880 *
881 * @return int New value of $key
882 * @access private
883 */
884 function _incrdecr( $cmd, $key, $amt = 1 ) {
885 if ( !$this->_active ) {
886 return null;
887 }
888
889 $sock = $this->get_sock( $key );
890 if ( !is_resource( $sock ) ) {
891 return null;
892 }
893
894 $key = is_array( $key ) ? $key[1] : $key;
895 if ( isset( $this->stats[$cmd] ) ) {
896 $this->stats[$cmd]++;
897 } else {
898 $this->stats[$cmd] = 1;
899 }
900 if ( !$this->_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
901 return null;
902 }
903
904 $line = $this->_fgets( $sock );
905 $match = array();
906 if ( !preg_match( '/^(\d+)/', $line, $match ) ) {
907 return null;
908 }
909 return $match[1];
910 }
911
912 // }}}
913 // {{{ _load_items()
914
915 /**
916 * Load items into $ret from $sock
917 *
918 * @param Resource $sock Socket to read from
919 * @param array $ret returned values
920 * @param float $casToken [optional]
921 * @return bool True for success, false for failure
922 *
923 * @access private
924 */
925 function _load_items( $sock, &$ret, &$casToken = null ) {
926 $results = array();
927
928 while ( 1 ) {
929 $decl = $this->_fgets( $sock );
930
931 if ( $decl === false ) {
932 /*
933 * If nothing can be read, something is wrong because we know exactly when
934 * to stop reading (right after "END") and we return right after that.
935 */
936 return false;
937 } elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+) (\d+)$/', $decl, $match ) ) {
938 /*
939 * Read all data returned. This can be either one or multiple values.
940 * Save all that data (in an array) to be processed later: we'll first
941 * want to continue reading until "END" before doing anything else,
942 * to make sure that we don't leave our client in a state where it's
943 * output is not yet fully read.
944 */
945 $results[] = array(
946 $match[1], // rkey
947 $match[2], // flags
948 $match[3], // len
949 $match[4], // casToken
950 $this->_fread( $sock, $match[3] + 2 ), // data
951 );
952 } elseif ( $decl == "END" ) {
953 if ( count( $results ) == 0 ) {
954 return false;
955 }
956
957 /**
958 * All data has been read, time to process the data and build
959 * meaningful return values.
960 */
961 foreach ( $results as $vars ) {
962 list( $rkey, $flags, $len, $casToken, $data ) = $vars;
963
964 if ( $data === false || substr( $data, -2 ) !== "\r\n" ) {
965 $this->_handle_error( $sock,
966 'line ending missing from data block from $1' );
967 return false;
968 }
969 $data = substr( $data, 0, -2 );
970 $ret[$rkey] = $data;
971
972 if ( $this->_have_zlib && $flags & self::COMPRESSED ) {
973 $ret[$rkey] = gzuncompress( $ret[$rkey] );
974 }
975
976 /*
977 * This unserialize is the exact reason that we only want to
978 * process data after having read until "END" (instead of doing
979 * this right away): "unserialize" can trigger outside code:
980 * in the event that $ret[$rkey] is a serialized object,
981 * unserializing it will trigger __wakeup() if present. If that
982 * function attempted to read from memcached (while we did not
983 * yet read "END"), these 2 calls would collide.
984 */
985 if ( $flags & self::SERIALIZED ) {
986 $ret[$rkey] = unserialize( $ret[$rkey] );
987 } elseif ( $flags & self::INTVAL ) {
988 $ret[$rkey] = intval( $ret[$rkey] );
989 }
990 }
991
992 return true;
993 } else {
994 $this->_handle_error( $sock, 'Error parsing response from $1' );
995 return false;
996 }
997 }
998 }
999
1000 // }}}
1001 // {{{ _set()
1002
1003 /**
1004 * Performs the requested storage operation to the memcache server
1005 *
1006 * @param string $cmd Command to perform
1007 * @param string $key Key to act on
1008 * @param mixed $val What we need to store
1009 * @param int $exp (optional) Expiration time. This can be a number of seconds
1010 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
1011 * longer must be the timestamp of the time at which the mapping should expire. It
1012 * is safe to use timestamps in all cases, regardless of exipration
1013 * eg: strtotime("+3 hour")
1014 * @param float $casToken [optional]
1015 *
1016 * @return bool
1017 * @access private
1018 */
1019 function _set( $cmd, $key, $val, $exp, $casToken = null ) {
1020 if ( !$this->_active ) {
1021 return false;
1022 }
1023
1024 $sock = $this->get_sock( $key );
1025 if ( !is_resource( $sock ) ) {
1026 return false;
1027 }
1028
1029 if ( isset( $this->stats[$cmd] ) ) {
1030 $this->stats[$cmd]++;
1031 } else {
1032 $this->stats[$cmd] = 1;
1033 }
1034
1035 $flags = 0;
1036
1037 if ( is_int( $val ) ) {
1038 $flags |= self::INTVAL;
1039 } elseif ( !is_scalar( $val ) ) {
1040 $val = serialize( $val );
1041 $flags |= self::SERIALIZED;
1042 if ( $this->_debug ) {
1043 $this->_debugprint( sprintf( "client: serializing data as it is not scalar\n" ) );
1044 }
1045 }
1046
1047 $len = strlen( $val );
1048
1049 if ( $this->_have_zlib && $this->_compress_enable
1050 && $this->_compress_threshold && $len >= $this->_compress_threshold
1051 ) {
1052 $c_val = gzcompress( $val, 9 );
1053 $c_len = strlen( $c_val );
1054
1055 if ( $c_len < $len * ( 1 - self::COMPRESSION_SAVINGS ) ) {
1056 if ( $this->_debug ) {
1057 $this->_debugprint( sprintf( "client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len ) );
1058 }
1059 $val = $c_val;
1060 $len = $c_len;
1061 $flags |= self::COMPRESSED;
1062 }
1063 }
1064
1065 $command = "$cmd $key $flags $exp $len";
1066 if ( $casToken ) {
1067 $command .= " $casToken";
1068 }
1069
1070 if ( !$this->_fwrite( $sock, "$command\r\n$val\r\n" ) ) {
1071 return false;
1072 }
1073
1074 $line = $this->_fgets( $sock );
1075
1076 if ( $this->_debug ) {
1077 $this->_debugprint( sprintf( "%s %s (%s)\n", $cmd, $key, $line ) );
1078 }
1079 if ( $line == "STORED" ) {
1080 return true;
1081 }
1082 return false;
1083 }
1084
1085 // }}}
1086 // {{{ sock_to_host()
1087
1088 /**
1089 * Returns the socket for the host
1090 *
1091 * @param string $host Host:IP to get socket for
1092 *
1093 * @return Resource|bool IO Stream or false
1094 * @access private
1095 */
1096 function sock_to_host( $host ) {
1097 if ( isset( $this->_cache_sock[$host] ) ) {
1098 return $this->_cache_sock[$host];
1099 }
1100
1101 $sock = null;
1102 $now = time();
1103 list( $ip, /* $port */) = explode( ':', $host );
1104 if ( isset( $this->_host_dead[$host] ) && $this->_host_dead[$host] > $now ||
1105 isset( $this->_host_dead[$ip] ) && $this->_host_dead[$ip] > $now
1106 ) {
1107 return null;
1108 }
1109
1110 if ( !$this->_connect_sock( $sock, $host ) ) {
1111 return null;
1112 }
1113
1114 // Do not buffer writes
1115 stream_set_write_buffer( $sock, 0 );
1116
1117 $this->_cache_sock[$host] = $sock;
1118
1119 return $this->_cache_sock[$host];
1120 }
1121
1122 /**
1123 * @param string $text
1124 */
1125 function _debugprint( $text ) {
1126 $this->_logger->debug( $text );
1127 }
1128
1129 /**
1130 * @param string $text
1131 */
1132 function _error_log( $text ) {
1133 $this->_logger->error( "Memcached error: $text" );
1134 }
1135
1136 /**
1137 * Write to a stream. If there is an error, mark the socket dead.
1138 *
1139 * @param Resource $sock The socket
1140 * @param string $buf The string to write
1141 * @return bool True on success, false on failure
1142 */
1143 function _fwrite( $sock, $buf ) {
1144 $bytesWritten = 0;
1145 $bufSize = strlen( $buf );
1146 while ( $bytesWritten < $bufSize ) {
1147 $result = fwrite( $sock, $buf );
1148 $data = stream_get_meta_data( $sock );
1149 if ( $data['timed_out'] ) {
1150 $this->_handle_error( $sock, 'timeout writing to $1' );
1151 return false;
1152 }
1153 // Contrary to the documentation, fwrite() returns zero on error in PHP 5.3.
1154 if ( $result === false || $result === 0 ) {
1155 $this->_handle_error( $sock, 'error writing to $1' );
1156 return false;
1157 }
1158 $bytesWritten += $result;
1159 }
1160
1161 return true;
1162 }
1163
1164 /**
1165 * Handle an I/O error. Mark the socket dead and log an error.
1166 *
1167 * @param Resource $sock
1168 * @param string $msg
1169 */
1170 function _handle_error( $sock, $msg ) {
1171 $peer = stream_socket_get_name( $sock, true /** remote **/ );
1172 if ( strval( $peer ) === '' ) {
1173 $peer = array_search( $sock, $this->_cache_sock );
1174 if ( $peer === false ) {
1175 $peer = '[unknown host]';
1176 }
1177 }
1178 $msg = str_replace( '$1', $peer, $msg );
1179 $this->_error_log( "$msg\n" );
1180 $this->_dead_sock( $sock );
1181 }
1182
1183 /**
1184 * Read the specified number of bytes from a stream. If there is an error,
1185 * mark the socket dead.
1186 *
1187 * @param Resource $sock The socket
1188 * @param int $len The number of bytes to read
1189 * @return string|bool The string on success, false on failure.
1190 */
1191 function _fread( $sock, $len ) {
1192 $buf = '';
1193 while ( $len > 0 ) {
1194 $result = fread( $sock, $len );
1195 $data = stream_get_meta_data( $sock );
1196 if ( $data['timed_out'] ) {
1197 $this->_handle_error( $sock, 'timeout reading from $1' );
1198 return false;
1199 }
1200 if ( $result === false ) {
1201 $this->_handle_error( $sock, 'error reading buffer from $1' );
1202 return false;
1203 }
1204 if ( $result === '' ) {
1205 // This will happen if the remote end of the socket is shut down
1206 $this->_handle_error( $sock, 'unexpected end of file reading from $1' );
1207 return false;
1208 }
1209 $len -= strlen( $result );
1210 $buf .= $result;
1211 }
1212 return $buf;
1213 }
1214
1215 /**
1216 * Read a line from a stream. If there is an error, mark the socket dead.
1217 * The \r\n line ending is stripped from the response.
1218 *
1219 * @param Resource $sock The socket
1220 * @return string|bool The string on success, false on failure
1221 */
1222 function _fgets( $sock ) {
1223 $result = fgets( $sock );
1224 // fgets() may return a partial line if there is a select timeout after
1225 // a successful recv(), so we have to check for a timeout even if we
1226 // got a string response.
1227 $data = stream_get_meta_data( $sock );
1228 if ( $data['timed_out'] ) {
1229 $this->_handle_error( $sock, 'timeout reading line from $1' );
1230 return false;
1231 }
1232 if ( $result === false ) {
1233 $this->_handle_error( $sock, 'error reading line from $1' );
1234 return false;
1235 }
1236 if ( substr( $result, -2 ) === "\r\n" ) {
1237 $result = substr( $result, 0, -2 );
1238 } elseif ( substr( $result, -1 ) === "\n" ) {
1239 $result = substr( $result, 0, -1 );
1240 } else {
1241 $this->_handle_error( $sock, 'line ending missing in response from $1' );
1242 return false;
1243 }
1244 return $result;
1245 }
1246
1247 /**
1248 * Flush the read buffer of a stream
1249 * @param Resource $f
1250 */
1251 function _flush_read_buffer( $f ) {
1252 if ( !is_resource( $f ) ) {
1253 return;
1254 }
1255 $r = array( $f );
1256 $w = null;
1257 $e = null;
1258 $n = stream_select( $r, $w, $e, 0, 0 );
1259 while ( $n == 1 && !feof( $f ) ) {
1260 fread( $f, 1024 );
1261 $r = array( $f );
1262 $w = null;
1263 $e = null;
1264 $n = stream_select( $r, $w, $e, 0, 0 );
1265 }
1266 }
1267
1268 // }}}
1269 // }}}
1270 // }}}
1271 }
1272
1273 // }}}
1274
1275 class MemCachedClientforWiki extends MWMemcached {
1276 }