博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
smarty模板基础2 缓存文件
阅读量:4677 次
发布时间:2019-06-09

本文共 3251 字,大约阅读时间需要 10 分钟。

缓存数据,这个并不是暂存的缓存,而是写入了内存的缓存

通过一个例子来书写:缓存数据

一、书写php和html页面的基本功能

既然是用smarty模板,那么前端和后端要分开写了

(1)php页面

1
2
3
4
5
6
7
8
9
10
11
12
<?php
$filename 
"../cache/huancun.html"
;  
//这个是放缓存的页面(缓存的不是代码,而是页面的源信息)
 
include
(
"../init.inc.php"
);  
//引入入口文件
include
(
"../DBDA.php"
);   
//引入数据库,要用到数据库的内容
  
$db 
new 
DBDA();  
//造新对象
$sql 
"select * from chinastates"
;  
//这是查找Chinastates表中的信息
$attr 
$db
->Query(
$sql
);  
//执行这个语句
 
$smarty
->assign(
"shuzu"
,
$attr
);  
//注册变量信息
$smarty
->display(
"huancun.html"
);  
//显示模板

(2)html页面

可以用表来显示数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<
h1
>数据列表</
h1
>
<
table 
width="50%" border="1" cellpadding="0" cellspacing="0">
        
<
tr
>
            
<
td
>代号</
td
>
            
<
td
>名称</
td
>
            
<
td
>操作</
td
>
        
</
tr
>
             
        
<{foreach $shuzu as $v}>   
<!--遍历显示这个表中的相应信息-->
            
<
tr
>
                
<
td
><{$v[0]}></
td
>
                
<
td
><{$v[1]}></
td
>
                
<
td
>操作</
td
>
            
</
tr
>
        
<{/foreach}>
             
</
table
>

看下效果

 

二、就是编写“缓存”功能

进行写入缓存的时候不是写入的上面的php页面,而是这个页面的源代码

(1)这个cache文件中要判断这个huancun.html文件存不存在,存在怎么样?不存在怎么样?

结果就是:如果缓存文件存在:直接调用缓存;如果缓存文件不存在:重新缓存。

A.如果这个文件存在(判断文件存不存在用的是:file_exists()方法)语句:

1
2
3
4
5
if(file_exists($filename))
{
    
//直接调用缓存   
    
include($filename);
}

B.否则(文件不存在)

 重要的是要在上面读取数据库的内容加上这几项:

1.开启内存缓存;

2.关闭内存缓存;

它们是成对的!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
else
{
    
//重新缓存 
    
ob_start();   
//开启内存缓存
     
    
include
(
"../init.inc.php"
);
    
include
(
"../DBDA.php"
);
     
    
$db 
new 
DBDA();
    
$sql 
"select * from chinastates"
;
    
$attr 
$db
->Query(
$sql
);
     
    
$smarty
->assign(
"shuzu"
,
$attr
);
    
$smarty
->display(
"huancun.html"
);
     
    
$str 
= ob_get_contents();  
//获取内存中的缓存内容
    
file_put_contents
(
$filename
,
$str
);  
//将字符串中的内容放入$filename的文件中
     
    
ob_flush(); 
//关闭内存缓存
     
    
echo 
"#######################################"
;  
//这个是为了区别哪个是刚出来的缓存文件
 
}

看下效果如下图,因为cache文件夹中没有hunacun.html才会走上面的“否则”语句,输出一长串的######

再刷新一下就没有了,因为cache文件夹中已经有了huancun.html文件

 

三、缓存的有效时间

缓存要是一直都在的话,那么后台怎么改,前面也不会进行修改,那么就要尽心缓存的有效时间

代码如下:

1
2
$time 
= 10;  
//缓存有效期10秒
if
(
file_exists
(
$filename
) && ((
filemtime
(
$filename
)+
$time
)>= time()) )  
//这是对缓存文件的判断

当缓存文件有了并且10秒之后就会重新又没有缓存文件时候的输出####  

 

四、进行分页的缓存

首先是分页的php页面的书写,在上面的更新的php页面中进行再一次修改,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
$p 
= 1;
if
(!
empty
(
$_GET
[
"page"
]))
{
    
$p 
$_GET
[
"page"
];
}
 
$filename 
"../cache/huancun{$p}.html"
;   
//缓存文件存放的位置
 
$time 
= 10;  
//缓存有效期10秒
if
(
file_exists
(
$filename
) && ((
filemtime
(
$filename
)+
$time
)>= time()) )
{
    
//直接调用缓存   
    
include
(
$filename
);
}
else
{
    
//重新缓存 
    
ob_start();   
//开启内存缓存
     
    
include
(
"../init.inc.php"
);
    
include
(
"../DBDA.php"
);
     
    
$db 
new 
DBDA();
    
$sqll 
"select count(*) from chinastates"
;
    
$zts 
$db
->StrQuery(
$sqll
);  
//总条数的执行语句
     
    
include
(
"../page.class.php"
);
    
$page 
new 
Page(
$zts
,10);  
//分页显示的条数
     
    
$sql 
"select * from chinastates "
.
$page
->limit;  
//分页的$page->limit
    
$attr 
$db
->Query(
$sql
);
     
    
$smarty
->assign(
"fpage"
,
$page
->fpage());  
//分页信息的显示
    
$smarty
->assign(
"shuzu"
,
$attr
);
    
$smarty
->display(
"huancun.html"
);
     
    
$str 
= ob_get_contents();  
//获取内存中的缓存内容
    
file_put_contents
(
$filename
,
$str
);  
//将$str的内容写入$filename的文件中
     
    
ob_flush(); 
//关闭内存缓存
     
    
echo 
"#######################################"
;
 
}

结果就是入下,每一个页面都会从缓存开始有,10秒后失效

这是第二个页面,刚开始没有缓存文件,所以会有####

随便点一页,回来后就是有了缓存文件,所以没有了###

转载于:https://www.cnblogs.com/ordinaryk/p/6522880.html

你可能感兴趣的文章
Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) B. Bear and Blocks 水题
查看>>
Codeforces Beta Round #8 C. Looking for Order 状压
查看>>
SCOJ 4484 The Graver Robbers' Chronicles 后缀自动机
查看>>
PHP+jquery+ajax实现分页
查看>>
务虚的全栈式开发
查看>>
[Coci2015]Divljak
查看>>
centos7中keepalived+nginx做双机热备和反向代理
查看>>
HDU 1250
查看>>
Introduction to my galaxy engine 6: Differed Lighting 2
查看>>
Python_深浅拷贝
查看>>
Oracle 中 not exists (select 'X' ...) 的含义
查看>>
Android必备的Java知识点
查看>>
ASP.NET网站实现中英文转换(本地化资源)【转】
查看>>
vue中 关于$emit的用法
查看>>
计算机基本介绍
查看>>
使用Flickr的图片拼出你的句子
查看>>
Visual Studio中web应用程序和网站区别
查看>>
浅析/dev/shm
查看>>
BZOJ4010 HNOI2015 菜肴制作 拓扑排序+贪心
查看>>
处理打拼音时触发input事件bug
查看>>