Removed error suppression opertators (per bug 24159)
[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 $args Associative array of settings
243 *
244 * @return mixed
245 */
246 public function __construct( $args ) {
247 global $wgMemCachedTimeout;
248 $this->set_servers( isset( $args['servers'] ) ? $args['servers'] : array() );
249 $this->_debug = isset( $args['debug'] ) ? $args['debug'] : false;
250 $this->stats = array();
251 $this->_compress_threshold = isset( $args['compress_threshold'] ) ? $args['compress_threshold'] : 0;
252 $this->_persistant = isset( $args['persistant'] ) ? $args['persistant'] : false;
253 $this->_compress_enable = true;
254 $this->_have_zlib = function_exists( 'gzcompress' );
255
256 $this->_cache_sock = array();
257 $this->_host_dead = array();
258
259 $this->_timeout_seconds = 0;
260 $this->_timeout_microseconds = $wgMemCachedTimeout;
261
262 $this->_connect_timeout = 0.01;
263 $this->_connect_attempts = 2;
264 }
265
266 // }}}
267 // {{{ add()
268
269 /**
270 * Adds a key/value to the memcache server if one isn't already set with
271 * that key
272 *
273 * @param $key String: key to set with data
274 * @param $val Mixed: value to store
275 * @param $exp Integer: (optional) time to expire data at
276 *
277 * @return Boolean
278 */
279 public function add( $key, $val, $exp = 0 ) {
280 return $this->_set( 'add', $key, $val, $exp );
281 }
282
283 // }}}
284 // {{{ decr()
285
286 /**
287 * Decriment a value stored on the memcache server
288 *
289 * @param $key String: key to decriment
290 * @param $amt Integer: (optional) amount to decriment
291 *
292 * @return Mixed: FALSE on failure, value on success
293 */
294 public function decr( $key, $amt = 1 ) {
295 return $this->_incrdecr( 'decr', $key, $amt );
296 }
297
298 // }}}
299 // {{{ delete()
300
301 /**
302 * Deletes a key from the server, optionally after $time
303 *
304 * @param $key String: key to delete
305 * @param $time Integer: (optional) how long to wait before deleting
306 *
307 * @return Boolean: TRUE on success, FALSE on failure
308 */
309 public function delete( $key, $time = 0 ) {
310 if ( !$this->_active ) {
311 return false;
312 }
313
314 $sock = $this->get_sock( $key );
315 if ( !is_resource( $sock ) ) {
316 return false;
317 }
318
319 $key = is_array( $key ) ? $key[1] : $key;
320
321 if ( isset( $this->stats['delete'] ) ) {
322 $this->stats['delete']++;
323 } else {
324 $this->stats['delete'] = 1;
325 }
326 $cmd = "delete $key $time\r\n";
327 if( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
328 $this->_dead_sock( $sock );
329 return false;
330 }
331 $res = trim( fgets( $sock ) );
332
333 if ( $this->_debug ) {
334 $this->_debugprint( sprintf( "MemCache: delete %s (%s)\n", $key, $res ) );
335 }
336
337 if ( $res == "DELETED" ) {
338 return true;
339 }
340 return false;
341 }
342
343 // }}}
344 // {{{ disconnect_all()
345
346 /**
347 * Disconnects all connected sockets
348 */
349 public function disconnect_all() {
350 foreach ( $this->_cache_sock as $sock ) {
351 fclose( $sock );
352 }
353
354 $this->_cache_sock = array();
355 }
356
357 // }}}
358 // {{{ enable_compress()
359
360 /**
361 * Enable / Disable compression
362 *
363 * @param $enable Boolean: TRUE to enable, FALSE to disable
364 */
365 public function enable_compress( $enable ) {
366 $this->_compress_enable = $enable;
367 }
368
369 // }}}
370 // {{{ forget_dead_hosts()
371
372 /**
373 * Forget about all of the dead hosts
374 */
375 public function forget_dead_hosts() {
376 $this->_host_dead = array();
377 }
378
379 // }}}
380 // {{{ get()
381
382 /**
383 * Retrieves the value associated with the key from the memcache server
384 *
385 * @param $key Mixed: key to retrieve
386 *
387 * @return Mixed
388 */
389 public function get( $key ) {
390 wfProfileIn( __METHOD__ );
391
392 if ( $this->_debug ) {
393 $this->_debugprint( "get($key)\n" );
394 }
395
396 if ( !$this->_active ) {
397 wfProfileOut( __METHOD__ );
398 return false;
399 }
400
401 $sock = $this->get_sock( $key );
402
403 if ( !is_resource( $sock ) ) {
404 wfProfileOut( __METHOD__ );
405 return false;
406 }
407
408 if ( isset( $this->stats['get'] ) ) {
409 $this->stats['get']++;
410 } else {
411 $this->stats['get'] = 1;
412 }
413
414 $cmd = "get $key\r\n";
415 if ( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
416 $this->_dead_sock( $sock );
417 wfProfileOut( __METHOD__ );
418 return false;
419 }
420
421 $val = array();
422 $this->_load_items( $sock, $val );
423
424 if ( $this->_debug ) {
425 foreach ( $val as $k => $v ) {
426 $this->_debugprint( sprintf( "MemCache: sock %s got %s\n", serialize( $sock ), $k ) );
427 }
428 }
429
430 wfProfileOut( __METHOD__ );
431 return @$val[$key];
432 }
433
434 // }}}
435 // {{{ get_multi()
436
437 /**
438 * Get multiple keys from the server(s)
439 *
440 * @param $keys Array: keys to retrieve
441 *
442 * @return Array
443 */
444 public function get_multi( $keys ) {
445 if ( !$this->_active ) {
446 return false;
447 }
448
449 if ( isset( $this->stats['get_multi'] ) ) {
450 $this->stats['get_multi']++;
451 } else {
452 $this->stats['get_multi'] = 1;
453 }
454 $sock_keys = array();
455
456 foreach ( $keys as $key ) {
457 $sock = $this->get_sock( $key );
458 if ( !is_resource( $sock ) ) {
459 continue;
460 }
461 $key = is_array( $key ) ? $key[1] : $key;
462 if ( !isset( $sock_keys[$sock] ) ) {
463 $sock_keys[$sock] = array();
464 $socks[] = $sock;
465 }
466 $sock_keys[$sock][] = $key;
467 }
468
469 // Send out the requests
470 foreach ( $socks as $sock ) {
471 $cmd = 'get';
472 foreach ( $sock_keys[$sock] as $key ) {
473 $cmd .= ' ' . $key;
474 }
475 $cmd .= "\r\n";
476
477 if ( $this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
478 $gather[] = $sock;
479 } else {
480 $this->_dead_sock( $sock );
481 }
482 }
483
484 // Parse responses
485 $val = array();
486 foreach ( $gather as $sock ) {
487 $this->_load_items( $sock, $val );
488 }
489
490 if ( $this->_debug ) {
491 foreach ( $val as $k => $v ) {
492 $this->_debugprint( sprintf( "MemCache: got %s\n", $k ) );
493 }
494 }
495
496 return $val;
497 }
498
499 // }}}
500 // {{{ incr()
501
502 /**
503 * Increments $key (optionally) by $amt
504 *
505 * @param $key String: key to increment
506 * @param $amt Integer: (optional) amount to increment
507 *
508 * @return Integer: new key value?
509 */
510 public function incr( $key, $amt = 1 ) {
511 return $this->_incrdecr( 'incr', $key, $amt );
512 }
513
514 // }}}
515 // {{{ replace()
516
517 /**
518 * Overwrites an existing value for key; only works if key is already set
519 *
520 * @param $key String: key to set value as
521 * @param $value Mixed: value to store
522 * @param $exp Integer: (optional) experiation time
523 *
524 * @return Boolean
525 */
526 public function replace( $key, $value, $exp = 0 ) {
527 return $this->_set( 'replace', $key, $value, $exp );
528 }
529
530 // }}}
531 // {{{ run_command()
532
533 /**
534 * Passes through $cmd to the memcache server connected by $sock; returns
535 * output as an array (null array if no output)
536 *
537 * NOTE: due to a possible bug in how PHP reads while using fgets(), each
538 * line may not be terminated by a \r\n. More specifically, my testing
539 * has shown that, on FreeBSD at least, each line is terminated only
540 * with a \n. This is with the PHP flag auto_detect_line_endings set
541 * to falase (the default).
542 *
543 * @param $sock Ressource: socket to send command on
544 * @param $cmd String: command to run
545 *
546 * @return Array: output array
547 */
548 public function run_command( $sock, $cmd ) {
549 if ( !is_resource( $sock ) ) {
550 return array();
551 }
552
553 if ( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
554 return array();
555 }
556
557 while ( true ) {
558 $res = fgets( $sock );
559 $ret[] = $res;
560 if ( preg_match( '/^END/', $res ) ) {
561 break;
562 }
563 if ( strlen( $res ) == 0 ) {
564 break;
565 }
566 }
567 return $ret;
568 }
569
570 // }}}
571 // {{{ set()
572
573 /**
574 * Unconditionally sets a key to a given value in the memcache. Returns true
575 * if set successfully.
576 *
577 * @param $key String: key to set value as
578 * @param $value Mixed: value to set
579 * @param $exp Integer: (optional) Experiation time
580 *
581 * @return Boolean: TRUE on success
582 */
583 public function set( $key, $value, $exp = 0 ) {
584 return $this->_set( 'set', $key, $value, $exp );
585 }
586
587 // }}}
588 // {{{ set_compress_threshold()
589
590 /**
591 * Sets the compression threshold
592 *
593 * @param $thresh Integer: threshold to compress if larger than
594 */
595 public function set_compress_threshold( $thresh ) {
596 $this->_compress_threshold = $thresh;
597 }
598
599 // }}}
600 // {{{ set_debug()
601
602 /**
603 * Sets the debug flag
604 *
605 * @param $dbg Boolean: TRUE for debugging, FALSE otherwise
606 *
607 * @see MWMemcached::__construct
608 */
609 public function set_debug( $dbg ) {
610 $this->_debug = $dbg;
611 }
612
613 // }}}
614 // {{{ set_servers()
615
616 /**
617 * Sets the server list to distribute key gets and puts between
618 *
619 * @param $list Array of servers to connect to
620 *
621 * @see MWMemcached::__construct()
622 */
623 public function set_servers( $list ) {
624 $this->_servers = $list;
625 $this->_active = count( $list );
626 $this->_buckets = null;
627 $this->_bucketcount = 0;
628
629 $this->_single_sock = null;
630 if ( $this->_active == 1 ) {
631 $this->_single_sock = $this->_servers[0];
632 }
633 }
634
635 /**
636 * Sets the timeout for new connections
637 *
638 * @param $seconds Integer: number of seconds
639 * @param $microseconds Integer: number of microseconds
640 */
641 public function set_timeout( $seconds, $microseconds ) {
642 $this->_timeout_seconds = $seconds;
643 $this->_timeout_microseconds = $microseconds;
644 }
645
646 // }}}
647 // }}}
648 // {{{ private methods
649 // {{{ _close_sock()
650
651 /**
652 * Close the specified socket
653 *
654 * @param $sock String: socket to close
655 *
656 * @access private
657 */
658 function _close_sock( $sock ) {
659 $host = array_search( $sock, $this->_cache_sock );
660 fclose( $this->_cache_sock[$host] );
661 unset( $this->_cache_sock[$host] );
662 }
663
664 // }}}
665 // {{{ _connect_sock()
666
667 /**
668 * Connects $sock to $host, timing out after $timeout
669 *
670 * @param $sock Integer: socket to connect
671 * @param $host String: Host:IP to connect to
672 *
673 * @return boolean
674 * @access private
675 */
676 function _connect_sock( &$sock, $host ) {
677 list( $ip, $port ) = explode( ':', $host );
678 $sock = false;
679 $timeout = $this->_connect_timeout;
680 $errno = $errstr = null;
681 for( $i = 0; !$sock && $i < $this->_connect_attempts; $i++ ) {
682 wfSuppressWarnings();
683 if ( $this->_persistant == 1 ) {
684 $sock = pfsockopen( $ip, $port, $errno, $errstr, $timeout );
685 } else {
686 $sock = fsockopen( $ip, $port, $errno, $errstr, $timeout );
687 }
688 wfRestoreWarnings();
689 }
690 if ( !$sock ) {
691 if ( $this->_debug ) {
692 $this->_debugprint( "Error connecting to $host: $errstr\n" );
693 }
694 return false;
695 }
696
697 // Initialise timeout
698 stream_set_timeout( $sock, $this->_timeout_seconds, $this->_timeout_microseconds );
699
700 return true;
701 }
702
703 // }}}
704 // {{{ _dead_sock()
705
706 /**
707 * Marks a host as dead until 30-40 seconds in the future
708 *
709 * @param $sock String: socket to mark as dead
710 *
711 * @access private
712 */
713 function _dead_sock( $sock ) {
714 $host = array_search( $sock, $this->_cache_sock );
715 $this->_dead_host( $host );
716 }
717
718 function _dead_host( $host ) {
719 $parts = explode( ':', $host );
720 $ip = $parts[0];
721 $this->_host_dead[$ip] = time() + 30 + intval( rand( 0, 10 ) );
722 $this->_host_dead[$host] = $this->_host_dead[$ip];
723 unset( $this->_cache_sock[$host] );
724 }
725
726 // }}}
727 // {{{ get_sock()
728
729 /**
730 * get_sock
731 *
732 * @param $key String: key to retrieve value for;
733 *
734 * @return Mixed: resource on success, false on failure
735 * @access private
736 */
737 function get_sock( $key ) {
738 if ( !$this->_active ) {
739 return false;
740 }
741
742 if ( $this->_single_sock !== null ) {
743 $this->_flush_read_buffer( $this->_single_sock );
744 return $this->sock_to_host( $this->_single_sock );
745 }
746
747 $hv = is_array( $key ) ? intval( $key[0] ) : $this->_hashfunc( $key );
748
749 if ( $this->_buckets === null ) {
750 foreach ( $this->_servers as $v ) {
751 if ( is_array( $v ) ) {
752 for( $i = 0; $i < $v[1]; $i++ ) {
753 $bu[] = $v[0];
754 }
755 } else {
756 $bu[] = $v;
757 }
758 }
759 $this->_buckets = $bu;
760 $this->_bucketcount = count( $bu );
761 }
762
763 $realkey = is_array( $key ) ? $key[1] : $key;
764 for( $tries = 0; $tries < 20; $tries++ ) {
765 $host = $this->_buckets[$hv % $this->_bucketcount];
766 $sock = $this->sock_to_host( $host );
767 if ( is_resource( $sock ) ) {
768 $this->_flush_read_buffer( $sock );
769 return $sock;
770 }
771 $hv = $this->_hashfunc( $hv . $realkey );
772 }
773
774 return false;
775 }
776
777 // }}}
778 // {{{ _hashfunc()
779
780 /**
781 * Creates a hash integer based on the $key
782 *
783 * @param $key String: key to hash
784 *
785 * @return Integer: hash value
786 * @access private
787 */
788 function _hashfunc( $key ) {
789 # Hash function must on [0,0x7ffffff]
790 # We take the first 31 bits of the MD5 hash, which unlike the hash
791 # function used in a previous version of this client, works
792 return hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
793 }
794
795 // }}}
796 // {{{ _incrdecr()
797
798 /**
799 * Perform increment/decriment on $key
800 *
801 * @param $cmd String: command to perform
802 * @param $key String: key to perform it on
803 * @param $amt Integer: amount to adjust
804 *
805 * @return Integer: new value of $key
806 * @access private
807 */
808 function _incrdecr( $cmd, $key, $amt = 1 ) {
809 if ( !$this->_active ) {
810 return null;
811 }
812
813 $sock = $this->get_sock( $key );
814 if ( !is_resource( $sock ) ) {
815 return null;
816 }
817
818 $key = is_array( $key ) ? $key[1] : $key;
819 if ( isset( $this->stats[$cmd] ) ) {
820 $this->stats[$cmd]++;
821 } else {
822 $this->stats[$cmd] = 1;
823 }
824 if ( !$this->_safe_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
825 return $this->_dead_sock( $sock );
826 }
827
828 $line = fgets( $sock );
829 $match = array();
830 if ( !preg_match( '/^(\d+)/', $line, $match ) ) {
831 return null;
832 }
833 return $match[1];
834 }
835
836 // }}}
837 // {{{ _load_items()
838
839 /**
840 * Load items into $ret from $sock
841 *
842 * @param $sock Ressource: socket to read from
843 * @param $ret Array: returned values
844 *
845 * @access private
846 */
847 function _load_items( $sock, &$ret ) {
848 while ( 1 ) {
849 $decl = fgets( $sock );
850 if ( $decl == "END\r\n" ) {
851 return true;
852 } elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match ) ) {
853 list( $rkey, $flags, $len ) = array( $match[1], $match[2], $match[3] );
854 $bneed = $len + 2;
855 $offset = 0;
856
857 while ( $bneed > 0 ) {
858 $data = fread( $sock, $bneed );
859 $n = strlen( $data );
860 if ( $n == 0 ) {
861 break;
862 }
863 $offset += $n;
864 $bneed -= $n;
865 if ( isset( $ret[$rkey] ) ) {
866 $ret[$rkey] .= $data;
867 } else {
868 $ret[$rkey] = $data;
869 }
870 }
871
872 if ( $offset != $len + 2 ) {
873 // Something is borked!
874 if ( $this->_debug ) {
875 $this->_debugprint( sprintf( "Something is borked! key %s expecting %d got %d length\n", $rkey, $len + 2, $offset ) );
876 }
877
878 unset( $ret[$rkey] );
879 $this->_close_sock( $sock );
880 return false;
881 }
882
883 if ( $this->_have_zlib && $flags & self::COMPRESSED ) {
884 $ret[$rkey] = gzuncompress( $ret[$rkey] );
885 }
886
887 $ret[$rkey] = rtrim( $ret[$rkey] );
888
889 if ( $flags & self::SERIALIZED ) {
890 $ret[$rkey] = unserialize( $ret[$rkey] );
891 }
892
893 } else {
894 $this->_debugprint( "Error parsing memcached response\n" );
895 return 0;
896 }
897 }
898 }
899
900 // }}}
901 // {{{ _set()
902
903 /**
904 * Performs the requested storage operation to the memcache server
905 *
906 * @param $cmd String: command to perform
907 * @param $key String: key to act on
908 * @param $val Mixed: what we need to store
909 * @param $exp Integer: when it should expire
910 *
911 * @return Boolean
912 * @access private
913 */
914 function _set( $cmd, $key, $val, $exp ) {
915 if ( !$this->_active ) {
916 return false;
917 }
918
919 $sock = $this->get_sock( $key );
920 if ( !is_resource( $sock ) ) {
921 return false;
922 }
923
924 if ( isset( $this->stats[$cmd] ) ) {
925 $this->stats[$cmd]++;
926 } else {
927 $this->stats[$cmd] = 1;
928 }
929
930 $flags = 0;
931
932 if ( !is_scalar( $val ) ) {
933 $val = serialize( $val );
934 $flags |= self::SERIALIZED;
935 if ( $this->_debug ) {
936 $this->_debugprint( sprintf( "client: serializing data as it is not scalar\n" ) );
937 }
938 }
939
940 $len = strlen( $val );
941
942 if ( $this->_have_zlib && $this->_compress_enable &&
943 $this->_compress_threshold && $len >= $this->_compress_threshold )
944 {
945 $c_val = gzcompress( $val, 9 );
946 $c_len = strlen( $c_val );
947
948 if ( $c_len < $len * ( 1 - self::COMPRESSION_SAVINGS ) ) {
949 if ( $this->_debug ) {
950 $this->_debugprint( sprintf( "client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len ) );
951 }
952 $val = $c_val;
953 $len = $c_len;
954 $flags |= self::COMPRESSED;
955 }
956 }
957 if ( !$this->_safe_fwrite( $sock, "$cmd $key $flags $exp $len\r\n$val\r\n" ) ) {
958 return $this->_dead_sock( $sock );
959 }
960
961 $line = trim( fgets( $sock ) );
962
963 if ( $this->_debug ) {
964 $this->_debugprint( sprintf( "%s %s (%s)\n", $cmd, $key, $line ) );
965 }
966 if ( $line == "STORED" ) {
967 return true;
968 }
969 return false;
970 }
971
972 // }}}
973 // {{{ sock_to_host()
974
975 /**
976 * Returns the socket for the host
977 *
978 * @param $host String: Host:IP to get socket for
979 *
980 * @return Mixed: IO Stream or false
981 * @access private
982 */
983 function sock_to_host( $host ) {
984 if ( isset( $this->_cache_sock[$host] ) ) {
985 return $this->_cache_sock[$host];
986 }
987
988 $sock = null;
989 $now = time();
990 list( $ip, /* $port */) = explode( ':', $host );
991 if ( isset( $this->_host_dead[$host] ) && $this->_host_dead[$host] > $now ||
992 isset( $this->_host_dead[$ip] ) && $this->_host_dead[$ip] > $now
993 ) {
994 return null;
995 }
996
997 if ( !$this->_connect_sock( $sock, $host ) ) {
998 return $this->_dead_host( $host );
999 }
1000
1001 // Do not buffer writes
1002 stream_set_write_buffer( $sock, 0 );
1003
1004 $this->_cache_sock[$host] = $sock;
1005
1006 return $this->_cache_sock[$host];
1007 }
1008
1009 function _debugprint( $str ) {
1010 print( $str );
1011 }
1012
1013 /**
1014 * Write to a stream, timing out after the correct amount of time
1015 *
1016 * @return Boolean: false on failure, true on success
1017 */
1018 /*
1019 function _safe_fwrite( $f, $buf, $len = false ) {
1020 stream_set_blocking( $f, 0 );
1021
1022 if ( $len === false ) {
1023 wfDebug( "Writing " . strlen( $buf ) . " bytes\n" );
1024 $bytesWritten = fwrite( $f, $buf );
1025 } else {
1026 wfDebug( "Writing $len bytes\n" );
1027 $bytesWritten = fwrite( $f, $buf, $len );
1028 }
1029 $n = stream_select( $r = null, $w = array( $f ), $e = null, 10, 0 );
1030 # $this->_timeout_seconds, $this->_timeout_microseconds );
1031
1032 wfDebug( "stream_select returned $n\n" );
1033 stream_set_blocking( $f, 1 );
1034 return $n == 1;
1035 return $bytesWritten;
1036 }*/
1037
1038 /**
1039 * Original behaviour
1040 */
1041 function _safe_fwrite( $f, $buf, $len = false ) {
1042 if ( $len === false ) {
1043 $bytesWritten = fwrite( $f, $buf );
1044 } else {
1045 $bytesWritten = fwrite( $f, $buf, $len );
1046 }
1047 return $bytesWritten;
1048 }
1049
1050 /**
1051 * Flush the read buffer of a stream
1052 */
1053 function _flush_read_buffer( $f ) {
1054 if ( !is_resource( $f ) ) {
1055 return;
1056 }
1057 $n = stream_select( $r = array( $f ), $w = null, $e = null, 0, 0 );
1058 while ( $n == 1 && !feof( $f ) ) {
1059 fread( $f, 1024 );
1060 $n = stream_select( $r = array( $f ), $w = null, $e = null, 0, 0 );
1061 }
1062 }
1063
1064 // }}}
1065 // }}}
1066 // }}}
1067 }
1068
1069 // vim: sts=3 sw=3 et
1070
1071 // }}}
1072
1073 class MemCachedClientforWiki extends MWMemcached {
1074 function _debugprint( $text ) {
1075 wfDebug( "memcached: $text" );
1076 }
1077 }