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