- <?php
- require "./inc/class/smarty.php";
- require "./inc/class/db.php";
- $smarty->caching = true;
- $db = new Db();
- if(!$smarty->is_cached('index.html'))
- {
- $contents = $db->fetchOne("SELECT username FROM user LIMIT 0,1");
- $smarty->assign("content",$contents);
- }
- $smarty->display("index.html");
- ?>
需要建一个user表。测试的话里面只有一个字段username都可 假设内容是test
上面的是
index.php里面的内容
db类是自己封装的,按个人情况来写
index.html里面只有一句话
<html><body>
{$content}</body></html>
输出 test
这个时候 cache里面多了一个以index.html结尾的缓存文件
内容是
<html><body>
test</body></html>
如果你刷新页面的话。第二次会调用这个缓存文件。
可以试着把这个缓存文件的内容改一下就知道了。
- <?php
- require "./inc/class/smarty.php";
- require "./inc/class/db.php";
- $db = new Db();
- if($_REQUEST["action"] == 'update')
- {
- $sql = "UPDATE user SET username = 'ggs' WHERE id = 2";
- $db->query($sql);
- $folder = opendir("cache");
- /* 这是正确地遍历目录方法 */
- while (false !== ($file = readdir($folder))) {
- if ($file == '.' || $file == '..')
- {
- continue;
- }
- else
- {
- unlink("cache/".$file);
- }
- }
- }
- $smarty->display("update.html")
- ?>
update.php
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <HTML>
- <HEAD>
- <TITLE> New Document </TITLE>
- <META NAME="Generator" CONTENT="EditPlus">
- <META NAME="Author" CONTENT="">
- <META NAME="Keywords" CONTENT="">
- <META NAME="Description" CONTENT="">
- </HEAD>
- <BODY>
- <form action = '#' method = 'post'>
- <input type = 'submit' value = 'update' />
- <input type = 'hidden' id = 'action' name = 'action' value = 'update' />
- </form>
- </BODY>
- </HTML>
update.html
当提交表单之后。
update.php 跟新了数据并且
清除了缓存文件。
你再次访问index.php的时候。里面的内容就改变了。