刷新PHP缓冲区为你的站点加速,刷新php缓冲区站点
在当前 PHP 版本的默认配置下,“输出缓冲(Output Buffering)”是被打开的。旧版本则不是这样,在旧版本的 PHP 中,字符串在每次被输出的时候(通过 echo 或 print 函数),都会触发一次发送到客户端浏览器的动作。
“输出缓冲”的引入,使得这一过程更加快速、更加高效。缓冲区实际上是在内存中开辟了一块区域,可以认为是内存中的一个大的字符串。当程序中有字符要输出的时候,会把要输出的内容附加到该缓冲区中,用来替代旧版本 PHP 中每次都直接输出到浏览器的方式。当缓冲区被“刷新”的时候,再统一输入到用户浏览器。以下几种情况下,会引起缓冲区的“刷新”操作:
1、PHP 程序执行完毕;
2、缓存区的大小超过了 php.ini 配置文件中设置的 output_buffering 值;
3、flush() 或者 ob_flush() 函数被调用的时候。
在实际的产品环境下,我们可以通过在 head 标签后马上刷新 PHP 的缓冲区,来达到加速你的站点的目的,示例代码如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Buffer flushing in action</title> <link rel="stylesheet" type="text/css" href="styles.css" /> <link rel="shortcut icon" href="favicon.ico" /> </head> <?php // 这里强制刷新缓冲区 flush(); ?> <body> ...
接下来来解释上述代码的作用:
当浏览器接收到页面 head 部分的代码的时候,就可以开始下载 head 段中的包括的资源了,类似 CSS 文件、站点收藏图标(Favicon)等。这些内容的下载可以与浏览器接受 body 段的内容时的时间同步进行。
具体能加速多少,需要因地制宜。这取决于很多客观条件,包括服务端的响应速度、你的页面大小、你的 CSS 文件大小和数量、浏览器是否有本地缓存等等。因素固然有很多,不过这样一个小小的优化,就可以显而易见的为你的站点加速,何乐而不为呢?
我很希望各位把这个小技巧运用在自己网站上,并跟踪所产生的实际效果。期待大家的反馈。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use