缓存数据,这个并非暂存的缓存,而是写入了内存的缓存php
经过一个例子来书写:缓存数据html
1、书写php和html页面的基本功能前端
既然是用smarty模板,那么前端和后端要分开写了sql
(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
>
|
看下效果post
2、就是编写“缓存”功能spa
进行写入缓存的时候不是写入的上面的php页面,而是这个页面的源代码3d
(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文件
3、缓存的有效时间
缓存要是一直都在的话,那么后台怎么改,前面也不会进行修改,那么就要尽心缓存的有效时间
代码以下:
1
2
|
$time
= 10;
//缓存有效期10秒
if
(
file_exists
(
$filename
) && ((
filemtime
(
$filename
)+
$time
)>= time()) )
//这是对缓存文件的判断
|
当缓存文件有了而且10秒以后就会从新又没有缓存文件时候的输出####
4、进行分页的缓存
首先是分页的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秒后失效
这是第二个页面,刚开始没有缓存文件,因此会有####
随便点一页,回来后就是有了缓存文件,因此没有了###
后面的也是同样的~~~