This article introduces the usage of PHP output cache. The so-called output cache in PHP means that the echo or other output commands in the code are first written to the PHP buffer when executed. After the script is executed or the output cache command is forced to be executed, it is The data will be output to the browser.
1. What is php output cache? The so-called output cache in PHP means that the echo or other output commands in the code are first written to the PHP buffer when executed. The data will be output to the browser only after the script is executed or the output cache command is forced to be executed ( Among them, php buffer is the output_buffering set in php.ini. The default is on, indicating unlimited size, which can be changed to a number to limit the size). Example: echo 'hlmblog.com'; echo 'technology'; echo 'share';These two echoes are inserted into the buffer in order. The data will be output to the browser only after the script execution is completed or cache output is forced. Output echo data in real time: ob_end_flush(); //Turn off the php cache, or execute ob_flush() before flush(), as explained below echo str_pad(" ", 256); for ($i=5; $i>0; $i--) { echo $i. ''; flush(); sleep(1); } Note: 1: The difference between flush and ob_flush: At first glance, the two are very similar, and the explanations in many manuals are unclear and ambiguous. In fact, there is a big difference between the two. When php.ini does not enable the php buffer cache, the content output by the php script will be waiting for output on the server and will not be saved to the output cache because the cache is not turned on. At this time, flush can be used to immediately flush the content waiting for output. The output is sent to the client (browser or other output terminal). When php.ini turns on the php buffer cache, the first step of the php script output content is to store it in the output cache. At this time, there is no data in the output content. Using flush has no effect and the data cannot be obtained. . Therefore, we must first use ob_flush to take out the content in the output cache and put it in a state waiting for output, and then use flush to send the content to the client. The order of execution is ob_flush first and then flush. Therefore, to achieve real-time output, either use ob_end_flush to first turn off the php output cache and then flush directly, or first ob_flush and then flush. 2: The browser cannot output real-time data The modified code is output once in Chrome, Firefox, IE and other browsers: ob_end_flush(); //Turn off the php cache, or ob_flush(); before flushing echo str_pad(" ", 256); for ($i=5; $i>0; $i--) { echo $i; flush(); sleep(1); }Just add an html tag and you can output it in real time. The reason is: it will be output immediately only when it encounters an html tag. It is really magical. Fortunately, the output content usually contains html tags and rarely contains plain text. Solution: Add a carriage return or other html tag to solve the problem. 2. Example of controlling cache output 1. Generate static pages Static pages load quickly echo str_pad('', 1024);//make the buffer overflow ob_start();//Open the buffer $content = ob_get_contents();//Get the content output by the page $f = fopen('./index.html', 'w'); fwrite($f, $content);//Write content into txt file fclose($f); ob_end_clean();//Clear and close the buffer2. Capture output function test($param) { if($param) { ob_start(); eval($param); $contents = ob_get_contents(); ob_end_clean(); }else { echo 'Unfortunately no output'; exit(); } return $contents; } |