商品总览,有无缓存页面进行缓存,使用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>