博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
模板引擎缓存
阅读量:7261 次
发布时间:2019-06-29

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

hot3.png

动态网站都是从数据库取出数据,分析数据,并输出数据的。网站的瓶颈通常都是反复连接和大量的SQL语句,所以可以对一些经常不变的信息设置缓存。让以后每次请求改页面,都从缓存中读取。

模板引擎分为编译和缓存。编译功能在默认下是开启的。而缓存机制需要开发人员开启。

1、建立缓存。下面是每个模板多个缓存,

<?php

include "libs/Smarty.class.php";

$smarty=new Smarty;

$smarty->setcachedir("./cache");

$smarty->cache_lifetime=60*60*24*7;

/**

$news= $db->getNews($_GET['newid']);

$smarty->assign('newsid',$news->getNewTitle());

$smarty->assign('newdt',$news->getDataTitle());

$smarty->assign('newContent',$news->getNewContent());

*/

$smarty->display('index.tpl',$_SERVER['REQUEST_URL']);

2、display(a,b,c);

a为模板的文件名或者是路径。b指定一个缓存标识符的名称。c在维护一个页面多个缓存时使用。每个页面都有唯一的url。

3、处理开销,其实就是在PHP脚本中动态获取数据和处理操作等的开销。

通过Smarty对象中的isCached();方法 ,判断指定模板的缓存是否存在。

<?php

$smarty->caching=ture;

if($smarty->iscached('index.tpl')){

}

$smarty->display('index.tpl');

4、清除缓存:

$smarty->clearAllCache();

$smarty->clearCache("index.tpl") ;

$smarty->clearCache("index.tpl","CacheID");

代码:

//建立缓存include "libs/Smarty.class.php";$smarty= new Smarty;$smarty->caching=ture;$smarty->setCacheDir("./cache");$smarty->display('index.php');//处理缓存的生命周期include "libs/Smarty.class.php";$smarty= new Smarty;$smarty->caching=ture;$smarty->setCacheDir("./cache");$smarty->cache_lifetime=60*60*24*7;$smarty->display('index.php');//每个模板多个缓存include "libs/Smarty.class.php";$smarty= new Smarty;$smarty->caching=ture;$smarty->setCacheDir("./cache");$smarty->cache_lifetime=60*60*24*7;/*  $news=$db->getNews($_GET["newsid"]);  $smarty->assign('newsid',$news->getNewTitle());  $smarty->assign('newsdt',$news->getDataTime());  $smarty->assign('newsContent',$news->getNewContent());  */$smarty->display('index.php',$_SERVER['REQUEST_URL']);

转载于:https://my.oschina.net/yuanyichuan/blog/220652

你可能感兴趣的文章
阿里云与Intel开启“TOP游戏”云生态培育计划,共建精品游戏生态
查看>>
世界那么大,我们一起到处去看看
查看>>
从大起到大落 各国的虚拟货币市场有何转变?
查看>>
新西兰天维网:新西兰净移民数量呈下降趋势
查看>>
婴儿患小儿脐疝肚子鼓起 父亲竟一刀划开肚脐“放气”
查看>>
英首相提交“脱欧”替代方案 重申不寻求二次公投
查看>>
不放弃!西班牙两岁男童落井8天 救援队仍钻井营救
查看>>
兰州火车站扩能改造完成 正式投入使用
查看>>
宁夏首票关税保证保险报关单顺利通关
查看>>
贷款增速达12.6% 银行业服务实体经济能力提升
查看>>
南方持续强降雪 京广高铁部分列车晚点1到3小时
查看>>
阿里程序员吐槽:玩命赚钱依旧抵不过拆迁户,奋斗的意义呢
查看>>
「算法」如何实现大整数相乘?(下)
查看>>
Oracle总结【SQL细节、多表查询、分组查询、分页】
查看>>
具有代表性的 HTTP 状态码
查看>>
iOS 组件化 —— 路由设计思路分析
查看>>
扯扯ID
查看>>
mp-redux:解耦小程序中的业务与视图,让测试更容易
查看>>
Sql注入
查看>>
如何用Python写一个贪吃蛇AI
查看>>