静态缓存案例生成一个html页面

简单案例:
public function l(){
    ob_start();
    if(!is_file("3.html") | time()-filemtime('3.html')>5){
        echo "haoyunyun";
        $content=ob_get_contents();
        $fb=fopen("3.html",'w');
        fwrite($fb,$content);
        fclose($fb);
    }else{
        echo "This is my article";
        echo "</br>";
        echo file_get_contents("3.html");
    }
}

ob缓存
ob_start(); //打开输出缓冲区  
    $cacheTime = 864000; //设置缓存页面过期时间  
    $cacheDir = 'Cache'; //设置缓存页面文件目录  
    if (!is_dir($cacheDir)) mkdir($cacheDir); //判断目录是否存在,否则创建目录  
    $cacheFile = $cacheDir.'/cache_'.$goods_id.'.dwt'; //缓存文件路径,文件以日期命名  
    if (!is_file($cacheFile) || time() - filemtime($cacheFile) > $cacheTime) {
      $smarty->display('goods.dwt');
    $content = ob_get_contents(); //取得php页面输出的全部内容   
    $fp = fopen($cacheFile, "w"); //输出内容写入文件  
    fwrite($fp, $content);  
    fclose($fp);  
    } else {  
    echo '缓存'.$content = file_get_contents($cacheFile); //如果缓存文件已经存在,且未过期则读取  
}  






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值