* API: All pages list
[mediawiki.git] / includes / memcached-client.php
blobb1ba778a1c3f0e2599586d477fa75a33721caa85
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 // +---------------------------------------------------------------------------+
36 // $TCAnet$
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/
43 * Usage example:
45 * require_once 'memcached.php';
47 * $mc = new memcached(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));
55 * $mc->add('key', array('some', 'array'));
56 * $mc->replace('key', 'some random string');
57 * $val = $mc->get('key');
59 * @author Ryan T. Dean <rtdean@cytherianage.net>
60 * @package memcached-client
61 * @version 0.1.2
64 // {{{ requirements
65 // }}}
67 // {{{ constants
68 // {{{ flags
70 /**
71 * Flag: indicates data is serialized
73 define("MEMCACHE_SERIALIZED", 1<<0);
75 /**
76 * Flag: indicates data is compressed
78 define("MEMCACHE_COMPRESSED", 1<<1);
80 // }}}
82 /**
83 * Minimum savings to store data compressed
85 define("COMPRESSION_SAVINGS", 0.20);
87 // }}}
89 // {{{ class memcached
90 /**
91 * memcached client class implemented using (p)fsockopen()
93 * @author Ryan T. Dean <rtdean@cytherianage.net>
94 * @package memcached-client
96 class memcached
98 // {{{ properties
99 // {{{ public
102 * Command statistics
104 * @var array
105 * @access public
107 var $stats;
109 // }}}
110 // {{{ private
113 * Cached Sockets that are connected
115 * @var array
116 * @access private
118 var $_cache_sock;
121 * Current debug status; 0 - none to 9 - profiling
123 * @var boolean
124 * @access private
126 var $_debug;
129 * Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'
131 * @var array
132 * @access private
134 var $_host_dead;
137 * Is compression available?
139 * @var boolean
140 * @access private
142 var $_have_zlib;
145 * Do we want to use compression?
147 * @var boolean
148 * @access private
150 var $_compress_enable;
153 * At how many bytes should we compress?
155 * @var interger
156 * @access private
158 var $_compress_threshold;
161 * Are we using persistant links?
163 * @var boolean
164 * @access private
166 var $_persistant;
169 * If only using one server; contains ip:port to connect to
171 * @var string
172 * @access private
174 var $_single_sock;
177 * Array containing ip:port or array(ip:port, weight)
179 * @var array
180 * @access private
182 var $_servers;
185 * Our bit buckets
187 * @var array
188 * @access private
190 var $_buckets;
193 * Total # of bit buckets we have
195 * @var interger
196 * @access private
198 var $_bucketcount;
201 * # of total servers we have
203 * @var interger
204 * @access private
206 var $_active;
209 * Stream timeout in seconds. Applies for example to fread()
211 * @var integer
212 * @access private
214 var $_timeout_seconds;
217 * Stream timeout in microseconds
219 * @var integer
220 * @access private
222 var $_timeout_microseconds;
225 * Connect timeout in seconds
227 var $_connect_timeout;
230 * Number of connection attempts for each server
232 var $_connect_attempts;
234 // }}}
235 // }}}
236 // {{{ methods
237 // {{{ public functions
238 // {{{ memcached()
241 * Memcache initializer
243 * @param array $args Associative array of settings
245 * @return mixed
246 * @access public
248 function memcached ($args)
250 $this->set_servers(@$args['servers']);
251 $this->_debug = @$args['debug'];
252 $this->stats = array();
253 $this->_compress_threshold = @$args['compress_threshold'];
254 $this->_persistant = array_key_exists('persistant', $args) ? (@$args['persistant']) : false;
255 $this->_compress_enable = true;
256 $this->_have_zlib = function_exists("gzcompress");
258 $this->_cache_sock = array();
259 $this->_host_dead = array();
261 $this->_timeout_seconds = 1;
262 $this->_timeout_microseconds = 0;
264 $this->_connect_timeout = 0.01;
265 $this->_connect_attempts = 3;
268 // }}}
269 // {{{ add()
272 * Adds a key/value to the memcache server if one isn't already set with
273 * that key
275 * @param string $key Key to set with data
276 * @param mixed $val Value to store
277 * @param interger $exp (optional) Time to expire data at
279 * @return boolean
280 * @access public
282 function add ($key, $val, $exp = 0)
284 return $this->_set('add', $key, $val, $exp);
287 // }}}
288 // {{{ decr()
291 * Decriment a value stored on the memcache server
293 * @param string $key Key to decriment
294 * @param interger $amt (optional) Amount to decriment
296 * @return mixed FALSE on failure, value on success
297 * @access public
299 function decr ($key, $amt=1)
301 return $this->_incrdecr('decr', $key, $amt);
304 // }}}
305 // {{{ delete()
308 * Deletes a key from the server, optionally after $time
310 * @param string $key Key to delete
311 * @param interger $time (optional) How long to wait before deleting
313 * @return boolean TRUE on success, FALSE on failure
314 * @access public
316 function delete ($key, $time = 0)
318 if (!$this->_active)
319 return false;
321 $sock = $this->get_sock($key);
322 if (!is_resource($sock))
323 return false;
325 $key = is_array($key) ? $key[1] : $key;
327 @$this->stats['delete']++;
328 $cmd = "delete $key $time\r\n";
329 if(!$this->_safe_fwrite($sock, $cmd, strlen($cmd)))
331 $this->_dead_sock($sock);
332 return false;
334 $res = trim(fgets($sock));
336 if ($this->_debug)
337 $this->_debugprint(sprintf("MemCache: delete %s (%s)\n", $key, $res));
339 if ($res == "DELETED")
340 return true;
341 return false;
344 // }}}
345 // {{{ disconnect_all()
348 * Disconnects all connected sockets
350 * @access public
352 function disconnect_all ()
354 foreach ($this->_cache_sock as $sock)
355 fclose($sock);
357 $this->_cache_sock = array();
360 // }}}
361 // {{{ enable_compress()
364 * Enable / Disable compression
366 * @param boolean $enable TRUE to enable, FALSE to disable
368 * @access public
370 function enable_compress ($enable)
372 $this->_compress_enable = $enable;
375 // }}}
376 // {{{ forget_dead_hosts()
379 * Forget about all of the dead hosts
381 * @access public
383 function forget_dead_hosts ()
385 $this->_host_dead = array();
388 // }}}
389 // {{{ get()
392 * Retrieves the value associated with the key from the memcache server
394 * @param string $key Key to retrieve
396 * @return mixed
397 * @access public
399 function get ($key)
401 $fname = 'memcached::get';
402 wfProfileIn( $fname );
404 if (!$this->_active) {
405 wfProfileOut( $fname );
406 return false;
409 $sock = $this->get_sock($key);
411 if (!is_resource($sock)) {
412 wfProfileOut( $fname );
413 return false;
416 @$this->stats['get']++;
418 $cmd = "get $key\r\n";
419 if (!$this->_safe_fwrite($sock, $cmd, strlen($cmd)))
421 $this->_dead_sock($sock);
422 wfProfileOut( $fname );
423 return false;
426 $val = array();
427 $this->_load_items($sock, $val);
429 if ($this->_debug)
430 foreach ($val as $k => $v)
431 $this->_debugprint(@sprintf("MemCache: sock %s got %s => %s\r\n", serialize($sock), $k, $v));
433 wfProfileOut( $fname );
434 return @$val[$key];
437 // }}}
438 // {{{ get_multi()
441 * Get multiple keys from the server(s)
443 * @param array $keys Keys to retrieve
445 * @return array
446 * @access public
448 function get_multi ($keys)
450 if (!$this->_active)
451 return false;
453 $this->stats['get_multi']++;
455 foreach ($keys as $key)
457 $sock = $this->get_sock($key);
458 if (!is_resource($sock)) continue;
459 $key = is_array($key) ? $key[1] : $key;
460 if (!isset($sock_keys[$sock]))
462 $sock_keys[$sock] = array();
463 $socks[] = $sock;
465 $sock_keys[$sock][] = $key;
468 // Send out the requests
469 foreach ($socks as $sock)
471 $cmd = "get";
472 foreach ($sock_keys[$sock] as $key)
474 $cmd .= " ". $key;
476 $cmd .= "\r\n";
478 if ($this->_safe_fwrite($sock, $cmd, strlen($cmd)))
480 $gather[] = $sock;
481 } else
483 $this->_dead_sock($sock);
487 // Parse responses
488 $val = array();
489 foreach ($gather as $sock)
491 $this->_load_items($sock, $val);
494 if ($this->_debug)
495 foreach ($val as $k => $v)
496 $this->_debugprint(sprintf("MemCache: got %s => %s\r\n", $k, $v));
498 return $val;
501 // }}}
502 // {{{ incr()
505 * Increments $key (optionally) by $amt
507 * @param string $key Key to increment
508 * @param interger $amt (optional) amount to increment
510 * @return interger New key value?
511 * @access public
513 function incr ($key, $amt=1)
515 return $this->_incrdecr('incr', $key, $amt);
518 // }}}
519 // {{{ replace()
522 * Overwrites an existing value for key; only works if key is already set
524 * @param string $key Key to set value as
525 * @param mixed $value Value to store
526 * @param interger $exp (optional) Experiation time
528 * @return boolean
529 * @access public
531 function replace ($key, $value, $exp=0)
533 return $this->_set('replace', $key, $value, $exp);
536 // }}}
537 // {{{ run_command()
540 * Passes through $cmd to the memcache server connected by $sock; returns
541 * output as an array (null array if no output)
543 * NOTE: due to a possible bug in how PHP reads while using fgets(), each
544 * line may not be terminated by a \r\n. More specifically, my testing
545 * has shown that, on FreeBSD at least, each line is terminated only
546 * with a \n. This is with the PHP flag auto_detect_line_endings set
547 * to falase (the default).
549 * @param resource $sock Socket to send command on
550 * @param string $cmd Command to run
552 * @return array Output array
553 * @access public
555 function run_command ($sock, $cmd)
557 if (!is_resource($sock))
558 return array();
560 if (!$this->_safe_fwrite($sock, $cmd, strlen($cmd)))
561 return array();
563 while (true)
565 $res = fgets($sock);
566 $ret[] = $res;
567 if (preg_match('/^END/', $res))
568 break;
569 if (strlen($res) == 0)
570 break;
572 return $ret;
575 // }}}
576 // {{{ set()
579 * Unconditionally sets a key to a given value in the memcache. Returns true
580 * if set successfully.
582 * @param string $key Key to set value as
583 * @param mixed $value Value to set
584 * @param interger $exp (optional) Experiation time
586 * @return boolean TRUE on success
587 * @access public
589 function set ($key, $value, $exp=0)
591 return $this->_set('set', $key, $value, $exp);
594 // }}}
595 // {{{ set_compress_threshold()
598 * Sets the compression threshold
600 * @param interger $thresh Threshold to compress if larger than
602 * @access public
604 function set_compress_threshold ($thresh)
606 $this->_compress_threshold = $thresh;
609 // }}}
610 // {{{ set_debug()
613 * Sets the debug flag
615 * @param boolean $dbg TRUE for debugging, FALSE otherwise
617 * @access public
619 * @see memcahced::memcached
621 function set_debug ($dbg)
623 $this->_debug = $dbg;
626 // }}}
627 // {{{ set_servers()
630 * Sets the server list to distribute key gets and puts between
632 * @param array $list Array of servers to connect to
634 * @access public
636 * @see memcached::memcached()
638 function set_servers ($list)
640 $this->_servers = $list;
641 $this->_active = count($list);
642 $this->_buckets = null;
643 $this->_bucketcount = 0;
645 $this->_single_sock = null;
646 if ($this->_active == 1)
647 $this->_single_sock = $this->_servers[0];
651 * Sets the timeout for new connections
653 * @param integer $seconds Number of seconds
654 * @param integer $microseconds Number of microseconds
656 * @access public
658 function set_timeout ($seconds, $microseconds)
660 $this->_timeout_seconds = $seconds;
661 $this->_timeout_microseconds = $microseconds;
664 // }}}
665 // }}}
666 // {{{ private methods
667 // {{{ _close_sock()
670 * Close the specified socket
672 * @param string $sock Socket to close
674 * @access private
676 function _close_sock ($sock)
678 $host = array_search($sock, $this->_cache_sock);
679 fclose($this->_cache_sock[$host]);
680 unset($this->_cache_sock[$host]);
683 // }}}
684 // {{{ _connect_sock()
687 * Connects $sock to $host, timing out after $timeout
689 * @param interger $sock Socket to connect
690 * @param string $host Host:IP to connect to
692 * @return boolean
693 * @access private
695 function _connect_sock (&$sock, $host)
697 list ($ip, $port) = explode(":", $host);
698 $sock = false;
699 $timeout = $this->_connect_timeout;
700 for ($i = 0; !$sock && $i < $this->_connect_attempts; $i++) {
701 if ($i > 0) {
702 # Sleep until the timeout, in case it failed fast
703 $elapsed = microtime(true) - $t;
704 if ( $elapsed < $timeout ) {
705 usleep(($timeout - $elapsed) * 1e6);
707 $timeout *= 2;
709 $t = microtime(true);
710 if ($this->_persistant == 1)
712 $sock = @pfsockopen($ip, $port, $errno, $errstr, $timeout);
713 } else
715 $sock = @fsockopen($ip, $port, $errno, $errstr, $timeout);
718 if (!$sock) {
719 if ($this->_debug)
720 $this->_debugprint( "Error connecting to $host: $errstr\n" );
721 return false;
724 // Initialise timeout
725 stream_set_timeout($sock, $this->_timeout_seconds, $this->_timeout_microseconds);
727 return true;
730 // }}}
731 // {{{ _dead_sock()
734 * Marks a host as dead until 30-40 seconds in the future
736 * @param string $sock Socket to mark as dead
738 * @access private
740 function _dead_sock ($sock)
742 $host = array_search($sock, $this->_cache_sock);
743 @list ($ip, $port) = explode(":", $host);
744 $this->_host_dead[$ip] = time() + 30 + intval(rand(0, 10));
745 $this->_host_dead[$host] = $this->_host_dead[$ip];
746 unset($this->_cache_sock[$host]);
749 // }}}
750 // {{{ get_sock()
753 * get_sock
755 * @param string $key Key to retrieve value for;
757 * @return mixed resource on success, false on failure
758 * @access private
760 function get_sock ($key)
762 if (!$this->_active)
763 return false;
765 if ($this->_single_sock !== null) {
766 $this->_flush_read_buffer($this->_single_sock);
767 return $this->sock_to_host($this->_single_sock);
770 $hv = is_array($key) ? intval($key[0]) : $this->_hashfunc($key);
772 if ($this->_buckets === null)
774 foreach ($this->_servers as $v)
776 if (is_array($v))
778 for ($i=0; $i<$v[1]; $i++)
779 $bu[] = $v[0];
780 } else
782 $bu[] = $v;
785 $this->_buckets = $bu;
786 $this->_bucketcount = count($bu);
789 $realkey = is_array($key) ? $key[1] : $key;
790 for ($tries = 0; $tries<20; $tries++)
792 $host = $this->_buckets[$hv % $this->_bucketcount];
793 $sock = $this->sock_to_host($host);
794 if (is_resource($sock)) {
795 $this->_flush_read_buffer($sock);
796 return $sock;
798 $hv += $this->_hashfunc($tries . $realkey);
801 return false;
804 // }}}
805 // {{{ _hashfunc()
808 * Creates a hash interger based on the $key
810 * @param string $key Key to hash
812 * @return interger Hash value
813 * @access private
815 function _hashfunc ($key)
817 # Hash function must on [0,0x7ffffff]
818 # We take the first 31 bits of the MD5 hash, which unlike the hash
819 # function used in a previous version of this client, works
820 return hexdec(substr(md5($key),0,8)) & 0x7fffffff;
823 // }}}
824 // {{{ _incrdecr()
827 * Perform increment/decriment on $key
829 * @param string $cmd Command to perform
830 * @param string $key Key to perform it on
831 * @param interger $amt Amount to adjust
833 * @return interger New value of $key
834 * @access private
836 function _incrdecr ($cmd, $key, $amt=1)
838 if (!$this->_active)
839 return null;
841 $sock = $this->get_sock($key);
842 if (!is_resource($sock))
843 return null;
845 $key = is_array($key) ? $key[1] : $key;
846 @$this->stats[$cmd]++;
847 if (!$this->_safe_fwrite($sock, "$cmd $key $amt\r\n"))
848 return $this->_dead_sock($sock);
850 stream_set_timeout($sock, 1, 0);
851 $line = fgets($sock);
852 if (!preg_match('/^(\d+)/', $line, $match))
853 return null;
854 return $match[1];
857 // }}}
858 // {{{ _load_items()
861 * Load items into $ret from $sock
863 * @param resource $sock Socket to read from
864 * @param array $ret Returned values
866 * @access private
868 function _load_items ($sock, &$ret)
870 while (1)
872 $decl = fgets($sock);
873 if ($decl == "END\r\n")
875 return true;
876 } elseif (preg_match('/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match))
878 list($rkey, $flags, $len) = array($match[1], $match[2], $match[3]);
879 $bneed = $len+2;
880 $offset = 0;
882 while ($bneed > 0)
884 $data = fread($sock, $bneed);
885 $n = strlen($data);
886 if ($n == 0)
887 break;
888 $offset += $n;
889 $bneed -= $n;
890 @$ret[$rkey] .= $data;
893 if ($offset != $len+2)
895 // Something is borked!
896 if ($this->_debug)
897 $this->_debugprint(sprintf("Something is borked! key %s expecting %d got %d length\n", $rkey, $len+2, $offset));
899 unset($ret[$rkey]);
900 $this->_close_sock($sock);
901 return false;
904 if ($this->_have_zlib && $flags & MEMCACHE_COMPRESSED)
905 $ret[$rkey] = gzuncompress($ret[$rkey]);
907 $ret[$rkey] = rtrim($ret[$rkey]);
909 if ($flags & MEMCACHE_SERIALIZED)
910 $ret[$rkey] = unserialize($ret[$rkey]);
912 } else
914 $this->_debugprint("Error parsing memcached response\n");
915 return 0;
920 // }}}
921 // {{{ _set()
924 * Performs the requested storage operation to the memcache server
926 * @param string $cmd Command to perform
927 * @param string $key Key to act on
928 * @param mixed $val What we need to store
929 * @param interger $exp When it should expire
931 * @return boolean
932 * @access private
934 function _set ($cmd, $key, $val, $exp)
936 if (!$this->_active)
937 return false;
939 $sock = $this->get_sock($key);
940 if (!is_resource($sock))
941 return false;
943 @$this->stats[$cmd]++;
945 $flags = 0;
947 if (!is_scalar($val))
949 $val = serialize($val);
950 $flags |= MEMCACHE_SERIALIZED;
951 if ($this->_debug)
952 $this->_debugprint(sprintf("client: serializing data as it is not scalar\n"));
955 $len = strlen($val);
957 if ($this->_have_zlib && $this->_compress_enable &&
958 $this->_compress_threshold && $len >= $this->_compress_threshold)
960 $c_val = gzcompress($val, 9);
961 $c_len = strlen($c_val);
963 if ($c_len < $len*(1 - COMPRESSION_SAVINGS))
965 if ($this->_debug)
966 $this->_debugprint(sprintf("client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len));
967 $val = $c_val;
968 $len = $c_len;
969 $flags |= MEMCACHE_COMPRESSED;
972 if (!$this->_safe_fwrite($sock, "$cmd $key $flags $exp $len\r\n$val\r\n"))
973 return $this->_dead_sock($sock);
975 $line = trim(fgets($sock));
977 if ($this->_debug)
979 if ($flags & MEMCACHE_COMPRESSED)
980 $val = 'compressed data';
981 $this->_debugprint(sprintf("MemCache: %s %s => %s (%s)\n", $cmd, $key, $val, $line));
983 if ($line == "STORED")
984 return true;
985 return false;
988 // }}}
989 // {{{ sock_to_host()
992 * Returns the socket for the host
994 * @param string $host Host:IP to get socket for
996 * @return mixed IO Stream or false
997 * @access private
999 function sock_to_host ($host)
1001 if (isset($this->_cache_sock[$host]))
1002 return $this->_cache_sock[$host];
1004 $now = time();
1005 list ($ip, $port) = explode (":", $host);
1006 if (isset($this->_host_dead[$host]) && $this->_host_dead[$host] > $now ||
1007 isset($this->_host_dead[$ip]) && $this->_host_dead[$ip] > $now)
1008 return null;
1010 if (!$this->_connect_sock($sock, $host))
1011 return $this->_dead_sock($host);
1013 // Do not buffer writes
1014 stream_set_write_buffer($sock, 0);
1016 $this->_cache_sock[$host] = $sock;
1018 return $this->_cache_sock[$host];
1021 function _debugprint($str){
1022 print($str);
1026 * Write to a stream, timing out after the correct amount of time
1028 * @return bool false on failure, true on success
1031 function _safe_fwrite($f, $buf, $len = false) {
1032 stream_set_blocking($f, 0);
1034 if ($len === false) {
1035 wfDebug("Writing " . strlen( $buf ) . " bytes\n");
1036 $bytesWritten = fwrite($f, $buf);
1037 } else {
1038 wfDebug("Writing $len bytes\n");
1039 $bytesWritten = fwrite($f, $buf, $len);
1041 $n = stream_select($r=NULL, $w = array($f), $e = NULL, 10, 0);
1042 # $this->_timeout_seconds, $this->_timeout_microseconds);
1044 wfDebug("stream_select returned $n\n");
1045 stream_set_blocking($f, 1);
1046 return $n == 1;
1047 return $bytesWritten;
1051 * Original behaviour
1053 function _safe_fwrite($f, $buf, $len = false) {
1054 if ($len === false) {
1055 $bytesWritten = fwrite($f, $buf);
1056 } else {
1057 $bytesWritten = fwrite($f, $buf, $len);
1059 return $bytesWritten;
1063 * Flush the read buffer of a stream
1065 function _flush_read_buffer($f) {
1066 if (!is_resource($f)) {
1067 return;
1069 $n = stream_select($r=array($f), $w = NULL, $e = NULL, 0, 0);
1070 while ($n == 1 && !feof($f)) {
1071 fread($f, 1024);
1072 $n = stream_select($r=array($f), $w = NULL, $e = NULL, 0, 0);
1076 // }}}
1077 // }}}
1078 // }}}
1081 // vim: sts=3 sw=3 et
1083 // }}}