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