您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

如何通过php 实现BigPipe分块输出

2024/10/31 0:04:25发布170次查看
原理:利用 ob_flush() 与 flush() 将缓冲区的内容提前输出,浏览器可提早加载这部分的内容,无需等待所有输出完成再加载。
将页面内容划分为一个个小块,输出一个后再输出下一个,使用户可尽早看到页面内容,优化用户体验。
首先 head 的内容应该优先加载,尽早加载css,javascript等静态内容,因此在head之后应该用 flush()输出。
例子:先输出head 静态内容,再将每个e388a4556c0f65e1904146cc1a846bee分为一个chunk,每隔一秒输出。
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"><html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title> big pipe </title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <style type="text/css"> body{margin:0px; background:#cccccc;} p{text-align:center; margin:10px;} img{width:450px;} </style> </head> <?php cache_flush() ?> <body> <p><img src="http://image.tianjimedia.com/uploadimages/2013/240/5cpoe4uz2t40.jpg"></p> <?php cache_flush(); ?> <p><img src="http://image.tianjimedia.com/uploadimages/2013/240/6893cy9xeqd1.jpg"></p> <?php cache_flush(); ?> <p><img src="http://image.tianjimedia.com/uploadimages/2013/240/83h52sg02v32.jpg"></p> </body></html><?phpfunction cache_flush($sec=1){ ob_flush(); flush(); usleep($sec*1000000);}?>
需要注意的问题:
1.尽量利用一次输出输出尽可能多的内容。
2.尽量可以同步加载。
3.chunk不是分得越多越好,要看实际需求情况决定。
4.ob_flush() 与 flush() 要同时使用,因有些情况flush()会没有效果。
本篇文章讲解了如何通过php 实现bigpipe分块输出,更多相关内容请关注。
相关推荐:
关于mysql 优化 insert 性能 的相关介绍
如何使用php 常用自定义方法
如何通过php 使用异或(xor)加密/解密文件
以上就是如何通过php 实现bigpipe分块输出的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product