Read full memcached response before manipulating data
[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'] : 500000;
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 expiration
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->_fwrite( $sock, $cmd ) ) {
334 return false;
335 }
336 $res = $this->_fgets( $sock );
337
338 if ( $this->_debug ) {
339 $this->_debugprint( sprintf( "MemCache: delete %s (%s)\n", $key, $res ) );
340 }
341
342 if ( $res == "DELETED" || $res == "NOT_FOUND" ) {
343 return true;
344 }
345
346 return false;
347 }
348
349 /**
350 * @param $key
351 * @param $timeout int
352 * @return bool
353 */
354 public function lock( $key, $timeout = 0 ) {
355 /* stub */
356 return true;
357 }
358
359 /**
360 * @param $key
361 * @return bool
362 */
363 public function unlock( $key ) {
364 /* stub */
365 return true;
366 }
367
368 // }}}
369 // {{{ disconnect_all()
370
371 /**
372 * Disconnects all connected sockets
373 */
374 public function disconnect_all() {
375 foreach ( $this->_cache_sock as $sock ) {
376 fclose( $sock );
377 }
378
379 $this->_cache_sock = array();
380 }
381
382 // }}}
383 // {{{ enable_compress()
384
385 /**
386 * Enable / Disable compression
387 *
388 * @param $enable Boolean: TRUE to enable, FALSE to disable
389 */
390 public function enable_compress( $enable ) {
391 $this->_compress_enable = $enable;
392 }
393
394 // }}}
395 // {{{ forget_dead_hosts()
396
397 /**
398 * Forget about all of the dead hosts
399 */
400 public function forget_dead_hosts() {
401 $this->_host_dead = array();
402 }
403
404 // }}}
405 // {{{ get()
406
407 /**
408 * Retrieves the value associated with the key from the memcache server
409 *
410 * @param $key array|string key to retrieve
411 * @param $casToken[optional] Float
412 *
413 * @return Mixed
414 */
415 public function get( $key, &$casToken = null ) {
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 = "gets $key\r\n";
442 if ( !$this->_fwrite( $sock, $cmd ) ) {
443 wfProfileOut( __METHOD__ );
444 return false;
445 }
446
447 $val = array();
448 $this->_load_items( $sock, $val, $casToken );
449
450 if ( $this->_debug ) {
451 foreach ( $val as $k => $v ) {
452 $this->_debugprint( sprintf( "MemCache: sock %s got %s\n", serialize( $sock ), $k ) );
453 }
454 }
455
456 $value = false;
457 if ( isset( $val[$key] ) ) {
458 $value = $val[$key];
459 }
460 wfProfileOut( __METHOD__ );
461 return $value;
462 }
463
464 // }}}
465 // {{{ get_multi()
466
467 /**
468 * Get multiple keys from the server(s)
469 *
470 * @param $keys Array: keys to retrieve
471 *
472 * @return Array
473 */
474 public function get_multi( $keys ) {
475 if ( !$this->_active ) {
476 return false;
477 }
478
479 if ( isset( $this->stats['get_multi'] ) ) {
480 $this->stats['get_multi']++;
481 } else {
482 $this->stats['get_multi'] = 1;
483 }
484 $sock_keys = array();
485 $socks = array();
486 foreach ( $keys as $key ) {
487 $sock = $this->get_sock( $key );
488 if ( !is_resource( $sock ) ) {
489 continue;
490 }
491 $key = is_array( $key ) ? $key[1] : $key;
492 if ( !isset( $sock_keys[$sock] ) ) {
493 $sock_keys[ intval( $sock ) ] = array();
494 $socks[] = $sock;
495 }
496 $sock_keys[ intval( $sock ) ][] = $key;
497 }
498
499 $gather = array();
500 // Send out the requests
501 foreach ( $socks as $sock ) {
502 $cmd = 'gets';
503 foreach ( $sock_keys[ intval( $sock ) ] as $key ) {
504 $cmd .= ' ' . $key;
505 }
506 $cmd .= "\r\n";
507
508 if ( $this->_fwrite( $sock, $cmd ) ) {
509 $gather[] = $sock;
510 }
511 }
512
513 // Parse responses
514 $val = array();
515 foreach ( $gather as $sock ) {
516 $this->_load_items( $sock, $val, $casToken );
517 }
518
519 if ( $this->_debug ) {
520 foreach ( $val as $k => $v ) {
521 $this->_debugprint( sprintf( "MemCache: got %s\n", $k ) );
522 }
523 }
524
525 return $val;
526 }
527
528 // }}}
529 // {{{ incr()
530
531 /**
532 * Increments $key (optionally) by $amt
533 *
534 * @param $key String: key to increment
535 * @param $amt Integer: (optional) amount to increment
536 *
537 * @return Integer: null if the key does not exist yet (this does NOT
538 * create new mappings if the key does not exist). If the key does
539 * exist, this returns the new value for that key.
540 */
541 public function incr( $key, $amt = 1 ) {
542 return $this->_incrdecr( 'incr', $key, $amt );
543 }
544
545 // }}}
546 // {{{ replace()
547
548 /**
549 * Overwrites an existing value for key; only works if key is already set
550 *
551 * @param $key String: key to set value as
552 * @param $value Mixed: value to store
553 * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
554 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
555 * longer must be the timestamp of the time at which the mapping should expire. It
556 * is safe to use timestamps in all cases, regardless of exipration
557 * eg: strtotime("+3 hour")
558 *
559 * @return Boolean
560 */
561 public function replace( $key, $value, $exp = 0 ) {
562 return $this->_set( 'replace', $key, $value, $exp );
563 }
564
565 // }}}
566 // {{{ run_command()
567
568 /**
569 * Passes through $cmd to the memcache server connected by $sock; returns
570 * output as an array (null array if no output)
571 *
572 * @param $sock Resource: socket to send command on
573 * @param $cmd String: command to run
574 *
575 * @return Array: output array
576 */
577 public function run_command( $sock, $cmd ) {
578 if ( !is_resource( $sock ) ) {
579 return array();
580 }
581
582 if ( !$this->_fwrite( $sock, $cmd ) ) {
583 return array();
584 }
585
586 $ret = array();
587 while ( true ) {
588 $res = $this->_fgets( $sock );
589 $ret[] = $res;
590 if ( preg_match( '/^END/', $res ) ) {
591 break;
592 }
593 if ( strlen( $res ) == 0 ) {
594 break;
595 }
596 }
597 return $ret;
598 }
599
600 // }}}
601 // {{{ set()
602
603 /**
604 * Unconditionally sets a key to a given value in the memcache. Returns true
605 * if set successfully.
606 *
607 * @param $key String: key to set value as
608 * @param $value Mixed: value to set
609 * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
610 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
611 * longer must be the timestamp of the time at which the mapping should expire. It
612 * is safe to use timestamps in all cases, regardless of exipration
613 * eg: strtotime("+3 hour")
614 *
615 * @return Boolean: TRUE on success
616 */
617 public function set( $key, $value, $exp = 0 ) {
618 return $this->_set( 'set', $key, $value, $exp );
619 }
620
621 // }}}
622 // {{{ cas()
623
624 /**
625 * Sets a key to a given value in the memcache if the current value still corresponds
626 * to a known, given value. Returns true if set successfully.
627 *
628 * @param $casToken Float: current known value
629 * @param $key String: key to set value as
630 * @param $value Mixed: value to set
631 * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
632 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
633 * longer must be the timestamp of the time at which the mapping should expire. It
634 * is safe to use timestamps in all cases, regardless of exipration
635 * eg: strtotime("+3 hour")
636 *
637 * @return Boolean: TRUE on success
638 */
639 public function cas( $casToken, $key, $value, $exp = 0 ) {
640 return $this->_set( 'cas', $key, $value, $exp, $casToken );
641 }
642
643 // }}}
644 // {{{ set_compress_threshold()
645
646 /**
647 * Sets the compression threshold
648 *
649 * @param $thresh Integer: threshold to compress if larger than
650 */
651 public function set_compress_threshold( $thresh ) {
652 $this->_compress_threshold = $thresh;
653 }
654
655 // }}}
656 // {{{ set_debug()
657
658 /**
659 * Sets the debug flag
660 *
661 * @param $dbg Boolean: TRUE for debugging, FALSE otherwise
662 *
663 * @see MWMemcached::__construct
664 */
665 public function set_debug( $dbg ) {
666 $this->_debug = $dbg;
667 }
668
669 // }}}
670 // {{{ set_servers()
671
672 /**
673 * Sets the server list to distribute key gets and puts between
674 *
675 * @param $list Array of servers to connect to
676 *
677 * @see MWMemcached::__construct()
678 */
679 public function set_servers( $list ) {
680 $this->_servers = $list;
681 $this->_active = count( $list );
682 $this->_buckets = null;
683 $this->_bucketcount = 0;
684
685 $this->_single_sock = null;
686 if ( $this->_active == 1 ) {
687 $this->_single_sock = $this->_servers[0];
688 }
689 }
690
691 /**
692 * Sets the timeout for new connections
693 *
694 * @param $seconds Integer: number of seconds
695 * @param $microseconds Integer: number of microseconds
696 */
697 public function set_timeout( $seconds, $microseconds ) {
698 $this->_timeout_seconds = $seconds;
699 $this->_timeout_microseconds = $microseconds;
700 }
701
702 // }}}
703 // }}}
704 // {{{ private methods
705 // {{{ _close_sock()
706
707 /**
708 * Close the specified socket
709 *
710 * @param $sock String: socket to close
711 *
712 * @access private
713 */
714 function _close_sock( $sock ) {
715 $host = array_search( $sock, $this->_cache_sock );
716 fclose( $this->_cache_sock[$host] );
717 unset( $this->_cache_sock[$host] );
718 }
719
720 // }}}
721 // {{{ _connect_sock()
722
723 /**
724 * Connects $sock to $host, timing out after $timeout
725 *
726 * @param $sock Integer: socket to connect
727 * @param $host String: Host:IP to connect to
728 *
729 * @return boolean
730 * @access private
731 */
732 function _connect_sock( &$sock, $host ) {
733 list( $ip, $port ) = explode( ':', $host );
734 $sock = false;
735 $timeout = $this->_connect_timeout;
736 $errno = $errstr = null;
737 for( $i = 0; !$sock && $i < $this->_connect_attempts; $i++ ) {
738 wfSuppressWarnings();
739 if ( $this->_persistent == 1 ) {
740 $sock = pfsockopen( $ip, $port, $errno, $errstr, $timeout );
741 } else {
742 $sock = fsockopen( $ip, $port, $errno, $errstr, $timeout );
743 }
744 wfRestoreWarnings();
745 }
746 if ( !$sock ) {
747 $this->_error_log( "Error connecting to $host: $errstr\n" );
748 $this->_dead_host( $host );
749 return false;
750 }
751
752 // Initialise timeout
753 stream_set_timeout( $sock, $this->_timeout_seconds, $this->_timeout_microseconds );
754
755 // If the connection was persistent, flush the read buffer in case there
756 // was a previous incomplete request on this connection
757 if ( $this->_persistent ) {
758 $this->_flush_read_buffer( $sock );
759 }
760 return true;
761 }
762
763 // }}}
764 // {{{ _dead_sock()
765
766 /**
767 * Marks a host as dead until 30-40 seconds in the future
768 *
769 * @param $sock String: socket to mark as dead
770 *
771 * @access private
772 */
773 function _dead_sock( $sock ) {
774 $host = array_search( $sock, $this->_cache_sock );
775 $this->_dead_host( $host );
776 }
777
778 /**
779 * @param $host
780 */
781 function _dead_host( $host ) {
782 $parts = explode( ':', $host );
783 $ip = $parts[0];
784 $this->_host_dead[$ip] = time() + 30 + intval( rand( 0, 10 ) );
785 $this->_host_dead[$host] = $this->_host_dead[$ip];
786 unset( $this->_cache_sock[$host] );
787 }
788
789 // }}}
790 // {{{ get_sock()
791
792 /**
793 * get_sock
794 *
795 * @param $key String: key to retrieve value for;
796 *
797 * @return Mixed: resource on success, false on failure
798 * @access private
799 */
800 function get_sock( $key ) {
801 if ( !$this->_active ) {
802 return false;
803 }
804
805 if ( $this->_single_sock !== null ) {
806 return $this->sock_to_host( $this->_single_sock );
807 }
808
809 $hv = is_array( $key ) ? intval( $key[0] ) : $this->_hashfunc( $key );
810 if ( $this->_buckets === null ) {
811 $bu = array();
812 foreach ( $this->_servers as $v ) {
813 if ( is_array( $v ) ) {
814 for( $i = 0; $i < $v[1]; $i++ ) {
815 $bu[] = $v[0];
816 }
817 } else {
818 $bu[] = $v;
819 }
820 }
821 $this->_buckets = $bu;
822 $this->_bucketcount = count( $bu );
823 }
824
825 $realkey = is_array( $key ) ? $key[1] : $key;
826 for( $tries = 0; $tries < 20; $tries++ ) {
827 $host = $this->_buckets[$hv % $this->_bucketcount];
828 $sock = $this->sock_to_host( $host );
829 if ( is_resource( $sock ) ) {
830 return $sock;
831 }
832 $hv = $this->_hashfunc( $hv . $realkey );
833 }
834
835 return false;
836 }
837
838 // }}}
839 // {{{ _hashfunc()
840
841 /**
842 * Creates a hash integer based on the $key
843 *
844 * @param $key String: key to hash
845 *
846 * @return Integer: hash value
847 * @access private
848 */
849 function _hashfunc( $key ) {
850 # Hash function must be in [0,0x7ffffff]
851 # We take the first 31 bits of the MD5 hash, which unlike the hash
852 # function used in a previous version of this client, works
853 return hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
854 }
855
856 // }}}
857 // {{{ _incrdecr()
858
859 /**
860 * Perform increment/decriment on $key
861 *
862 * @param $cmd String command to perform
863 * @param $key String|array key to perform it on
864 * @param $amt Integer amount to adjust
865 *
866 * @return Integer: new value of $key
867 * @access private
868 */
869 function _incrdecr( $cmd, $key, $amt = 1 ) {
870 if ( !$this->_active ) {
871 return null;
872 }
873
874 $sock = $this->get_sock( $key );
875 if ( !is_resource( $sock ) ) {
876 return null;
877 }
878
879 $key = is_array( $key ) ? $key[1] : $key;
880 if ( isset( $this->stats[$cmd] ) ) {
881 $this->stats[$cmd]++;
882 } else {
883 $this->stats[$cmd] = 1;
884 }
885 if ( !$this->_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
886 return null;
887 }
888
889 $line = $this->_fgets( $sock );
890 $match = array();
891 if ( !preg_match( '/^(\d+)/', $line, $match ) ) {
892 return null;
893 }
894 return $match[1];
895 }
896
897 // }}}
898 // {{{ _load_items()
899
900 /**
901 * Load items into $ret from $sock
902 *
903 * @param $sock Resource: socket to read from
904 * @param $ret Array: returned values
905 * @param $casToken[optional] Float
906 * @return boolean True for success, false for failure
907 *
908 * @access private
909 */
910 function _load_items( $sock, &$ret, &$casToken = null ) {
911 $results = array();
912
913 while ( 1 ) {
914 $decl = $this->_fgets( $sock );
915 if( $decl === false ) {
916 return false;
917 } elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+) (\d+)$/', $decl, $match ) ) {
918
919 $results[] = array(
920 $match[1], // rkey
921 $match[2], // flags
922 $match[3], // len
923 $match[4], // casToken
924 $this->_fread( $sock, $match[3] + 2 ), // data
925 );
926 } elseif ( $decl == "END" ) {
927 if ( count( $results ) == 0 ) {
928 return false;
929 }
930
931 foreach ( $results as $vars ) {
932 list( $rkey, $flags, $len, $casToken, $data ) = $vars;
933
934 if ( $data === false || substr( $data, -2 ) !== "\r\n" ) {
935 $this->_handle_error( $sock,
936 'line ending missing from data block from $1' );
937 return false;
938 }
939 $data = substr( $data, 0, -2 );
940 $ret[$rkey] = $data;
941
942 if ( $this->_have_zlib && $flags & self::COMPRESSED ) {
943 $ret[$rkey] = gzuncompress( $ret[$rkey] );
944 }
945
946 if ( $flags & self::SERIALIZED ) {
947 $ret[$rkey] = unserialize( $ret[$rkey] );
948 }
949 }
950
951 return true;
952 } else {
953 $this->_handle_error( $sock, 'Error parsing response from $1' );
954 return false;
955 }
956 }
957 }
958
959 // }}}
960 // {{{ _set()
961
962 /**
963 * Performs the requested storage operation to the memcache server
964 *
965 * @param $cmd String: command to perform
966 * @param $key String: key to act on
967 * @param $val Mixed: what we need to store
968 * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
969 * to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
970 * longer must be the timestamp of the time at which the mapping should expire. It
971 * is safe to use timestamps in all cases, regardless of exipration
972 * eg: strtotime("+3 hour")
973 * @param $casToken[optional] Float
974 *
975 * @return Boolean
976 * @access private
977 */
978 function _set( $cmd, $key, $val, $exp, $casToken = null ) {
979 if ( !$this->_active ) {
980 return false;
981 }
982
983 $sock = $this->get_sock( $key );
984 if ( !is_resource( $sock ) ) {
985 return false;
986 }
987
988 if ( isset( $this->stats[$cmd] ) ) {
989 $this->stats[$cmd]++;
990 } else {
991 $this->stats[$cmd] = 1;
992 }
993
994 $flags = 0;
995
996 if ( !is_scalar( $val ) ) {
997 $val = serialize( $val );
998 $flags |= self::SERIALIZED;
999 if ( $this->_debug ) {
1000 $this->_debugprint( sprintf( "client: serializing data as it is not scalar\n" ) );
1001 }
1002 }
1003
1004 $len = strlen( $val );
1005
1006 if ( $this->_have_zlib && $this->_compress_enable &&
1007 $this->_compress_threshold && $len >= $this->_compress_threshold )
1008 {
1009 $c_val = gzcompress( $val, 9 );
1010 $c_len = strlen( $c_val );
1011
1012 if ( $c_len < $len * ( 1 - self::COMPRESSION_SAVINGS ) ) {
1013 if ( $this->_debug ) {
1014 $this->_debugprint( sprintf( "client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len ) );
1015 }
1016 $val = $c_val;
1017 $len = $c_len;
1018 $flags |= self::COMPRESSED;
1019 }
1020 }
1021
1022 $command = "$cmd $key $flags $exp $len";
1023 if ( $casToken ) {
1024 $command .= " $casToken";
1025 }
1026
1027 if ( !$this->_fwrite( $sock, "$command\r\n$val\r\n" ) ) {
1028 return false;
1029 }
1030
1031 $line = $this->_fgets( $sock );
1032
1033 if ( $this->_debug ) {
1034 $this->_debugprint( sprintf( "%s %s (%s)\n", $cmd, $key, $line ) );
1035 }
1036 if ( $line == "STORED" ) {
1037 return true;
1038 }
1039 return false;
1040 }
1041
1042 // }}}
1043 // {{{ sock_to_host()
1044
1045 /**
1046 * Returns the socket for the host
1047 *
1048 * @param $host String: Host:IP to get socket for
1049 *
1050 * @return Mixed: IO Stream or false
1051 * @access private
1052 */
1053 function sock_to_host( $host ) {
1054 if ( isset( $this->_cache_sock[$host] ) ) {
1055 return $this->_cache_sock[$host];
1056 }
1057
1058 $sock = null;
1059 $now = time();
1060 list( $ip, /* $port */) = explode( ':', $host );
1061 if ( isset( $this->_host_dead[$host] ) && $this->_host_dead[$host] > $now ||
1062 isset( $this->_host_dead[$ip] ) && $this->_host_dead[$ip] > $now
1063 ) {
1064 return null;
1065 }
1066
1067 if ( !$this->_connect_sock( $sock, $host ) ) {
1068 return null;
1069 }
1070
1071 // Do not buffer writes
1072 stream_set_write_buffer( $sock, 0 );
1073
1074 $this->_cache_sock[$host] = $sock;
1075
1076 return $this->_cache_sock[$host];
1077 }
1078
1079 /**
1080 * @param $text string
1081 */
1082 function _debugprint( $text ) {
1083 wfDebugLog( 'memcached', $text );
1084 }
1085
1086 /**
1087 * @param $text string
1088 */
1089 function _error_log( $text ) {
1090 wfDebugLog( 'memcached-serious', "Memcached error: $text" );
1091 }
1092
1093 /**
1094 * Write to a stream. If there is an error, mark the socket dead.
1095 *
1096 * @param $sock The socket
1097 * @param $buf The string to write
1098 * @return bool True on success, false on failure
1099 */
1100 function _fwrite( $sock, $buf ) {
1101 $bytesWritten = 0;
1102 $bufSize = strlen( $buf );
1103 while ( $bytesWritten < $bufSize ) {
1104 $result = fwrite( $sock, $buf );
1105 $data = stream_get_meta_data( $sock );
1106 if ( $data['timed_out'] ) {
1107 $this->_handle_error( $sock, 'timeout writing to $1' );
1108 return false;
1109 }
1110 // Contrary to the documentation, fwrite() returns zero on error in PHP 5.3.
1111 if ( $result === false || $result === 0 ) {
1112 $this->_handle_error( $sock, 'error writing to $1' );
1113 return false;
1114 }
1115 $bytesWritten += $result;
1116 }
1117
1118 return true;
1119 }
1120
1121 /**
1122 * Handle an I/O error. Mark the socket dead and log an error.
1123 */
1124 function _handle_error( $sock, $msg ) {
1125 $peer = stream_socket_get_name( $sock, true /** remote **/ );
1126 if ( strval( $peer ) === '' ) {
1127 $peer = array_search( $sock, $this->_cache_sock );
1128 if ( $peer === false ) {
1129 $peer = '[unknown host]';
1130 }
1131 }
1132 $msg = str_replace( '$1', $peer, $msg );
1133 $this->_error_log( "$msg\n" );
1134 $this->_dead_sock( $sock );
1135 }
1136
1137 /**
1138 * Read the specified number of bytes from a stream. If there is an error,
1139 * mark the socket dead.
1140 *
1141 * @param $sock The socket
1142 * @param $len The number of bytes to read
1143 * @return The string on success, false on failure.
1144 */
1145 function _fread( $sock, $len ) {
1146 $buf = '';
1147 while ( $len > 0 ) {
1148 $result = fread( $sock, $len );
1149 $data = stream_get_meta_data( $sock );
1150 if ( $data['timed_out'] ) {
1151 $this->_handle_error( $sock, 'timeout reading from $1' );
1152 return false;
1153 }
1154 if ( $result === false ) {
1155 $this->_handle_error( $sock, 'error reading buffer from $1' );
1156 return false;
1157 }
1158 if ( $result === '' ) {
1159 // This will happen if the remote end of the socket is shut down
1160 $this->_handle_error( $sock, 'unexpected end of file reading from $1' );
1161 return false;
1162 }
1163 $len -= strlen( $result );
1164 $buf .= $result;
1165 }
1166 return $buf;
1167 }
1168
1169 /**
1170 * Read a line from a stream. If there is an error, mark the socket dead.
1171 * The \r\n line ending is stripped from the response.
1172 *
1173 * @param $sock The socket
1174 * @return The string on success, false on failure
1175 */
1176 function _fgets( $sock ) {
1177 $result = fgets( $sock );
1178 // fgets() may return a partial line if there is a select timeout after
1179 // a successful recv(), so we have to check for a timeout even if we
1180 // got a string response.
1181 $data = stream_get_meta_data( $sock );
1182 if ( $data['timed_out'] ) {
1183 $this->_handle_error( $sock, 'timeout reading line from $1' );
1184 return false;
1185 }
1186 if ( $result === false ) {
1187 $this->_handle_error( $sock, 'error reading line from $1' );
1188 return false;
1189 }
1190 if ( substr( $result, -2 ) === "\r\n" ) {
1191 $result = substr( $result, 0, -2 );
1192 } elseif ( substr( $result, -1 ) === "\n" ) {
1193 $result = substr( $result, 0, -1 );
1194 } else {
1195 $this->_handle_error( $sock, 'line ending missing in response from $1' );
1196 return false;
1197 }
1198 return $result;
1199 }
1200
1201 /**
1202 * Flush the read buffer of a stream
1203 * @param $f Resource
1204 */
1205 function _flush_read_buffer( $f ) {
1206 if ( !is_resource( $f ) ) {
1207 return;
1208 }
1209 $r = array( $f );
1210 $w = null;
1211 $e = null;
1212 $n = stream_select( $r, $w, $e, 0, 0 );
1213 while ( $n == 1 && !feof( $f ) ) {
1214 fread( $f, 1024 );
1215 $r = array( $f );
1216 $w = null;
1217 $e = null;
1218 $n = stream_select( $r, $w, $e, 0, 0 );
1219 }
1220 }
1221
1222 // }}}
1223 // }}}
1224 // }}}
1225 }
1226
1227
1228 // }}}
1229
1230 class MemCachedClientforWiki extends MWMemcached {
1231 }