码迷,mamicode.com
首页 > Web开发 > 详细

PHP使用缓存生成静态页面

时间:2016-07-04 20:30:53      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

http://www.cnblogs.com/lh460795/archive/2013/04/06/3003105.html

在apache / bin/ab.exe  可以做压力测试,该工具可以模拟多人,并发访问某个页面.

 

基本的用法

 ab.exe –n 10000 –c 10

-n 表示请求多少次

-c 表示多少人

如果要测试php自己的缓存机制, 需要做配置.

php.ini 文件

display_errors=On

output_buffering=Off

error_reproting= 设置错误级别

 

看一段代码,使用缓存时,在发送文件头之前可以显示文字.

<?php

              echo“yyy”;

              header(“content-type:text/htm;charset=utf-8”);

              echo“hello”;

?>

PHP缓存控制的几个函数:

 

技术分享
 1 //PHP缓存控制的几个函数:
 2 //开启缓存 [通过php.ini,也可以在页面 ob_start()]
 3 ob_start();
 4 echo "yyy";
 5 header("content-type:text/htm;charset=utf-8");
 6 echo "hello";
 7 //ob_clean函数可以清空 outputbuffer的内容.
 8 //ob_clean();
 9 //ob_end_clean是关闭ob缓存,同时清空.
10 //ob_clean();
11 //ob_end_flush() 函数是 把ob缓存的内存输出,并关闭ob
12 //ob_end_flush();
13 //ob_end_flush() 函数是 把ob缓存的内存输出,
14 //ob_flush()函数是输出ob内容,并清空,但不关闭.
15 ob_flush();
16         
17 echo "kkk";//=>ob缓存.
18 
19 //header("content-type:text/htm;charset=utf-8");
20 
21 //ob_get_contents() 可以获取output_buffering的内容.
22 //$contents=ob_get_contents();
23 
24 //file_put_contents("d:/log.text",$contents);
技术分享

 

下面来看一个实例,用缓存技术,"假如保存的缓存文件未超过30秒,则直接取出缓存文件":

 

技术分享
 1 <?php
 2                 $id=$_GET[‘id‘];
 3                 $filename="static_id_".$id.".html";
 4                 $status=filemtime($filename)+30>time();//判断文件创建及修改时间距当前时间是否超过30秒
 5                 if(file_exists($filename)&&$status){
 6                     $str=file_get_contents($filename);
 7                     echo $str;
 8                 }else{
 9                     require_once "SqlHelper.class.php";
10                     $sqlHelper=new Sqlhelper();
11                     $arr=$sqlHelper->execute_dql2("SELECT * FROM news1 WHERE id=$id");
12                     if(empty($arr)){
13                         echo "数据为空";
14                     }else{
15                         /***缓存开始***/
16                         ob_start();//下面的内容将存到缓存区中,显示的内容都将存到缓存区
17                         echo $arr[0][‘tile‘];
18                         echo "<br/>";
19                         echo $arr[0][‘content‘];
20                         $content=  ob_get_contents();//从缓存中获取内容
21                         ob_end_clean();//关闭缓存并清空
22                         /***缓存结束***/
23                         file_put_contents($filename, $content);
24                         echo $content;
25                     }
26                 }
27                 
28                 
29             ?>
技术分享

PHP使用缓存生成静态页面

标签:

原文地址:http://www.cnblogs.com/jukan/p/5641349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!