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

PHP smarty缓存

时间:2016-07-28 15:45:23      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:

test.php

<?php

//定义该页面缓存文件存放的路径
$filename = "../cache/cachetest.html";

//定义缓存有效期
$cachetime = 5;

//判断缓存文件是否存在
if(!file_exists($filename) || filemtime($filename)+$cachetime<time())
{
    //开启内存缓存
    ob_start();
    
    include("../init.inc.php");
    include("../DBDA.php");
    $db = new DBDA();
    
    $sql = "select * from car";
    
    $attr = $db->Query($sql);
    
    $smarty->assign("car",$attr);
    $smarty->display("test.html");
    
    //从内存缓存中获取页面代码
    $content = ob_get_contents();
    
    //将获取到的内容存放到缓存文件
    file_put_contents($filename,$content);
    
    //清掉内存缓存
    ob_flush();
    
    echo "######################################";

}
else
{
    include($filename);
}

test.html

<body>
<h1>汽车信息</h1>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td>代号</td>
        <td>汽车名称</td>
        <td>油耗</td>
        <td>价格</td>
    </tr>
    
    <{foreach $car as $v}>
    <tr>
        <td><{$v[0]}></td>
        <td><{$v[1]}></td>
        <td><{$v[4]}></td>
        <td><{$v[7]}></td>
    </tr>
    <{/foreach}>
    
</table>
</body>

技术分享技术分享

技术分享

分页缓存

testa.php

<?php

//取当前页
$p=1;
if(!empty($_GET["page"]))
{
    $p = $_GET["page"];
}
//定义缓存文件存放路径
$filename = "../cache/cahetesta{$p}.html";

//判断
if(!file_exists($filename) || filemtime($filename)+30<time())
{
    ob_start();
    
    
    include("../init.inc.php");
    include("../DBDA.php");
    $db = new DBDA();
    
    include("page.class.php");
    
    $szs = "select count(*) from car";
    $zs = $db->StrQuery($szs);
    
    $page = new Page($zs,5);
    $xinxi = $page->fpage();
    
    $sql = "select * from car ".$page->limit;
    
    $attr = $db->Query($sql);
    
    $smarty->assign("car",$attr);
    $smarty->assign("xinxi",$xinxi);
    $smarty->display("testa.html");
    
    $nr = ob_get_contents();
    
    file_put_contents($filename,$nr);
    
    ob_flush();
    
    echo "################################################";
}
else
{
    include($filename);
}

testa.heml

<body>
<h1>汽车信息</h1>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td>代号</td>
        <td>汽车名称</td>
        <td>油耗</td>
        <td>价格</td>
    </tr>
    
    <{foreach $car as $v}>
    <tr>
        <td><{$v[0]}></td>
        <td><{$v[1]}></td>
        <td><{$v[4]}></td>
        <td><{$v[7]}></td>
    </tr>
    <{/foreach}>
    
</table>
<div><{$xinxi}></div>
</body>

技术分享

技术分享

 

PHP smarty缓存

标签:

原文地址:http://www.cnblogs.com/yy01/p/5714662.html

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