使用ob,memcache(小数据)进行页面静态化,和储存数据

商品总览,有无缓存页面进行缓存,使用ob,memcache(存储小数据)

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/19
 * Time: 20:57
 */
header('content-type:text/html;charset=utf-8');
ob_start();
$filename='index.html';
if(file_exists($filename)&&time()-filemtime($filename)<5){
    echo "缓存文件";
  echo file_get_contents($filename);
}else{
    $memcache=new memcache();
    //var_dump($memcache);
    $memcache->connect('127.0.0.1',11211);
    $pdo= new pdo('mysql:host=localhost;dbname=seven7','root','root');
    $sql="select * from nba";
    $pdo->exec("set names utf8");
    $data=$pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
    //print_r($data);
    //@$list=$pdo->query("select * from nba where id=$id")->fetchAll(PDO::FETCH_ASSOC);
    $memcache->set('data',$data);
    //$memcache->set('list',$list);
    $res=$memcache->get('data');
    include "show.html";
    $news=ob_get_contents();
    ob_clean();
    file_put_contents($filename,$news);
    echo $news;
}


商品详情页进行缓存

<?php
header('content-type:text/html;charset=utf-8');
$id=$_GET['id'];
//echo $id;
ob_start();
$filename=$id."msg.html";
if(file_exists($filename)&&time()-filemtime($filename)<20){
    echo "详情缓存页面";
    echo file_get_contents($filename);
}else{
    echo "数据库查询";
    $mem=new  memcache();
    $mem->connect('127.0.0.1',11211);
    $pdo=new PDO('mysql:host=localhost;dbname=seven7','root','root');
    $pdo->exec('set names utf8');
    $sql="select * from nba where id='$id'";
    $list=$pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
    $mem->set('list',$list);
    $list=$mem->get('list');
    include('list.html');
    $newsmsg=ob_get_contents();
    ob_clean();
    file_put_contents($filename,$newsmsg);
    echo $newsmsg;

}

展示页面

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>信息展示</title>
</head>
<body>
<center>
    <table>
        <tr>
            <td>id</td>
            <td>title</td>
            <td>content</td>
            <td>click</td>
        </tr>
        <?php  foreach($res as $k=>$v){ ?>
        <tr>
            <td><?php echo $v['id']; ?></td>
            <td><?php echo $v['title']; ?></td>
            <td><?php echo $v['content']; ?></td>
            <td><a href="list.php?id=<?php echo $v['id']; ?>">详情</a></td>
        </tr>
        <?php } ?>
    </table>
</center>
</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值