Detailed explanation of PHP caching mechanism Output Control, outputcontrol_PHP tutorial

WBOY
Release: 2016-07-13 10:23:25
Original
832 people have browsed it

Detailed explanation of PHP caching mechanism Output Control, outputcontrol

In the configuration of the php5.2 version, output_buffering is turned off by default, so a warning will appear when running 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'; 

Copy after login

There are two ways to enable OB cache:

1. Enable output_buffering = 4096 in php.ini

With this directive enabled, each PHP script is equivalent to calling the ob_start() function at the beginning. PHP5.5 has output_buffering = 4096 enabled by default

2. Use ob_start() directly in the program;

Turn on output buffering. When output buffering is activated, the script will not output content (except http headers), instead the content to be output is stored in an internal buffer.

The contents of the internal buffer can be copied to a string variable using the ob_get_contents() function. To output the contents stored in the internal buffer, use the ob_end_flush() function. In addition, using the ob_end_clean() function will silently discard the contents of the buffer.

/**
 * output_buffering = off 情况下测试
 */
ob_start();  //开启ob缓存
echo 'hello1'; //存入ob缓存
header('content-type:text/html;charset=utf-8');//存入程序缓存
//ob_end_clean(); //清空ob缓存,并关闭ob缓存
echo 'hello2'; //存入ob缓存
$str = ob_get_contents(); //返回ob缓存的数据(不清除缓冲内容)
file_put_contents('ob.txt', $str); //把$str保存到文件
//ob_clean(); //清空ob缓存
echo 'hello3'; //存入ob缓存
echo 'hello4'; //存入ob缓存
/* 此脚本将生成ob.txt文件,存入hello1hello2,浏览器输出hello1hello2hello3hello4 */
/* 若ob_clean()注释打开,那么生成的ob.txt文件中将没有内容,浏览器输出hello3hello4 */
/* 若ob_end_clean()注释打开,那么ob.txt中依然没有内容,因为关闭了ob缓存,浏览器输出hello2hello3hello4 */

Copy after login

ob_flush() and ob_end_flush() Example:

ob_start();
echo 'abc';//存入ob缓存
header('content-type:text/html;charset=utf-8'); //存入程序缓存
echo 'hello'; //存入ob缓存
ob_flush();//将ob缓存中的内容输出到程序缓存,清空ob缓存,不关闭ob缓存
//ob_end_flush() //将ob缓存中的内容输出到程序缓存,清空ob缓存,关闭ob缓存
echo 'aa'; //存入ob缓存
echo ob_get_contents();
/* 最后输出abchelloaaaa */
/* 注释ob_flush,打开ob_end_flush,最后输出abchelloaa */

Copy after login

Note:
When output_buffering = 4096 is turned on, ob_end_clean() only closes the ob cache once (that is, when ob_start is turned on), and the system is not closed.
The same applies to ob_end_flush().

Operation principle/principle of OB cache:

1. The ob cache is turned on, and the echo data is first put into the ob cache
2. If it is header information, put it directly in the program cache
3. When the page is executed to the end, the ob cached data will be placed in the program cache, and then returned to the browser once

Finally there is a flush(); to force refresh the PHP program cache to the browser cache.

Features: Some versions of Microsoft Internet Explorer will only start displaying the page after receiving 256 bytes, so some extra spaces must be sent to allow these browsers to display the page content.

echo str_repeat('', 1024);//重复输出多个字符(解决浏览器缓存256字节之后再输出的情况)
for($i=0; $i < 5; $i++)
{
  echo $i;
  flush();    //强制刷新程序缓存到浏览器缓存
  sleep(1);    //休眠1秒钟,http连接未断开,每隔1秒输出$i
}
Copy after login

How to use caching in php and which caching mechanism is best;

This depends on your actual situation. There are file cache, database cache, and memcache cache. . . . .

What is the caching mechanism of php?

Mainly include:
① Universal caching technology ② Page caching ③ Time-triggered cache ④ Content-triggered cache ⑤ Static cache (generate html files)
⑥ Memory cache ⑦ PHP buffer ⑧ MYSQL cache ⑨ Based on reverse proxy Web caching, DNS polling
But the commonly used ones are ①②④. Others are used when the website has a large amount of data and has many interactions.
is used to reduce the pressure on the server. Reference: blog.163.com/.. .44905/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/840629.htmlTechArticleDetailed explanation of PHP caching mechanism Output Control, outputcontrol In the configuration of the php5.2 version, output_buffering is turned off by default, so A warning will appear when running the following three lines of code: ...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!