Output Control and outputcontrol of PHP Cache Mechanism
Bytes ---------------------------------------------------------------------------------------------------------
In php5.2 configuration, output_buffering is disabled by default. A warning is displayed when you run the following three lines of code:Warning: Cannot modify header information-headers already sent
echo 'hello1';header('content-type:text/html;charset=utf-8');echo 'hello2';
Two Methods for enabling OB cache =>
1. Enable output_buffering = 4096 in php. ini
// # If this command is enabled, every PHP script calls the ob_start () function at the beginning. output_buffering = 4096 is enabled by default in PHP5.5.
2. directly in the program ob_start ();
// # Enable the output buffer. When the output buffer is activated, the script does not output content (except the http header). On the contrary, the content to be output is stored in the internal buffer.
// # The content of the internal buffer can be copied to a string variable using the ob_get_contents () function. To output the content stored in the internal buffer, you can use the ob_end_flush () function. In addition, using the ob_end_clean () function will silently discard the buffer content.
/**
* Output_buffering = off
* @ Chenwei <www. chenwei. ws>
*/
Ob_start (); // enable ob cache echo 'hello1'; // store the ob cache header ('content-type: text/html; charset = UTF-8 '); // store the program Cache
// Ob_end_clean (); // clear the ob cache and disable the ob cache.
Echo 'hello2'; // store the ob cache $ str = ob_get_contents (); // return the data stored in the obslowly State (the buffer content is not cleared) file_put_contents('ob.txt ', $ str); // save $ str to a file
// Ob_clean (); // clear the ob Cache
Echo 'hello3'; // save to ob cache echo 'hello4'; // save to ob Cache
/* This script will generate the ob.txt file and store it to hello1hello2. the browser outputs hello1hello2hello3hello4 */
/* If ob_clean(folder is opened, the generated ob.txt file will have no content, and the browser outputs hello3hello4 */
/* If ob_end_clean('ob_clean' is enabled, there is still no content in 'ob.txt '. Because ob cache is disabled, the browser outputs hello2hello3hello4 */
Example of ob_flush () and ob_end_flush:
Ob_start (); echo 'abc'; // store the ob cache header ('content-type: text/html; charset = UTF-8 '); // echo 'hello' in the program cache; // Save the ob_flush () in the ob cache; // output the content in the ob cache to the program cache, clear the ob cache, and disable the ob cache.
// Ob_end_flush () // output the content in the ob cache to the program cache, clear the ob cache, and disable echo 'A' in the ob cache; // Save the echo ob_get_contents () to the ob cache ();
/* Finally output abchelloaaaa */
/* Comment on ob_flush, open ob_end_flush, and output abchelloaa */
------------------------------------- @ Blackeye poet <www. chenwei. ws> ----------------------------------
Note: When output_buffering = 4096 is enabled
Ob_end_clean () Only closes ob cache once (that is, if ob_start is enabled), the system does not close
The same is true for ob_end_flush.
Operating principle/principle of OB cache =>
1. When ob cache is enabled, echo data is first put into ob Cache
2. Put the header information directly in the program cache.
3. When the page is executed to the end, the ob cached data is put in the program cache, and then returned to the browser once.
There is also a flush (); force refresh the PHP program cache to the browser cache.
// # Features: in some versions of Microsoft Internet Explorer, this page is displayed only after the receipt of 256 bytes, therefore, some extra spaces must be sent to display the page content in these browsers.
Echo str_repeat ('', 1024); // repeated output of multiple characters (to solve the problem that the browser caches 256 bytes before outputting) for ($ I = 0; $ I <5; $ I ++) {echo $ I; flush (); // force refresh the program cache to the browser cache sleep (1); // sleep for 1 second, http connection not closed, $ I} output every 1 second}
Bytes ---------------------------------------------------------------------------------------------------------
What is the caching mechanism of php?
Mainly include:
① General Cache Technology ② page cache ③ Time-triggered cache ④ content-triggered cache ⑤ static cache (generate html files)
6. Memory Cache 7. php Buffer 2. MYSQL cache 2. Web cache based on reverse proxy, DNS round-robin
However, it is generally used for ① ② ④. Others are websites with large data volumes and many interactions, which can be used to reduce server pressure.
Reference: blog.163.com/..44905/
Which cache mechanism is the best to use in php;
This depends on your actual situation, including File Cache, database cache, and memcache cache .....