一起源码网

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
猜你喜欢
查看: 8910|回复: 1
打印 上一主题 下一主题

Smarty模板引擎如何进行缓存的机制详解

[复制链接]

0

主题

0

帖子

1万

积分

钻石会员

Rank: 8Rank: 8

积分
17424
QQ
跳转到指定楼层
楼主
发表于 2020-5-24 11:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本文主要介绍了Smarty模板引擎缓存机制,结合实例形式分析了Smarty模板引擎缓存机制的原理,开启与使用方法以及相关注意事项,需要的朋友可以参考下。希望对大家有所帮助。

具体如下:

首先说下smarty缓存和编译,这是两个不同的概念,编译默认情况下是启动的,而缓存机制需要人为开启,smarty编译过的文件还是php文件,所以执行的时候还是编译的,如果涉及到数据库,还是要访问数据库的所以开销也不小啦,所以需要smarty缓存来解决!

1.开启全局缓存

$smarty->cache_dir = "/caches/"; //缓存目录
$smarty->caching = true; //开启缓存,为flase的时侯缓存无效
$smarty->cache_lifetime = 3600; //缓存时间

2.一个页面使用多个缓存

如:一个文章模板页面会生成多个文章页面,当然是缓存成很多页面,实现起来很简单,只要在display()方法设置第二个参数,指定唯一标识符即可。如下php代码:

$smarty->display('index.tpl',$_GET["article_id"]);

如上,通过第二个参数文章的id缓存一个文章页面。

3.为缓存减小开销

也就是说,已经缓存的页面无需进行数据库的操作处理了,可通过is_cached()方法判断!

if(!$smarty->is_cached('index.tpl')){
 //调用数据库
}
$smarty->display('index.tpl');

4.清除缓存

一般在开发过程中是不开启缓存的,因为在缓存时间内输出结果不变,但是在应用过程中开启缓存能大大提高web性能,清除缓存方法如下:

clear_all_cache();//清除所有缓存
clear_cache('index.tpl');//清除index.tpl的缓存
clear_cache('index.tpl',cache_id);//清除指定id的缓存

5.关闭局部缓存

如果一个页面中一部分缓存,而另一部分不需要缓存,就可以这样做,比如说显示用户登录的名称就需要关闭缓存,smarty提供了如下三种解决方法:

(1)使用insert模板的一部分不被缓存

定义一个inser标签要使用的处理函数,函数名格式为:insert_xx(array $params, object &$smarty)其中的xx是insert的name,也就是说,如果你定义的函数为insert_abc,则模板中使用方法为{insert name=abc}

参数通过$params传入

也可以做成insert插件,文件名命名为:insert.xx.php,函数命名为:smarty_insert_aa($params,&$smarty),xx定义同上

(2)$smarty->register_block($params, &$smarty)使整篇页面中的某一块不被缓存

定义一个block:

smarty_block_name($params,$content, &$smarty){return $content;} 
//name表示区域名

注册block:

$smarty->register_block(name, smarty_block_name, false);
//第三参数false表示该区域不被缓存

模板写法:

{name}内容 {/name}

写成block插件:

第一步:定义一件插件函数:block.cacheless.php,放在smarty的 plugins目录

block.cacheless.php的内容如下:

<?php
function smarty_block_cacheless($param, $content, &$smarty) {
return $content;
}
?>

第二步:编写程序及模板

示例程序:testCacheLess.php

<?php
include(Smarty.class.php);
$smarty = new Smarty;
$smarty->caching=true;
$smarty->cache_lifetime = 6;
$smarty->display(cache.tpl);
?>

所用的模板:cache.tpl

已经缓存的:{$smarty.now}<br>
{cacheless}
没有缓存的:{$smarty.now}
{/cacheless}

现在运行一下,发现是不起作用的,两行内容都被缓存了

第三步:改写Smarty_Compiler.class.php(注:该文件很重要,请先备份,以在必要时恢复)

查找:

复制代码 代码如下:

$this->_plugins[block][$tag_command] = array($plugin_func, null, null, null, true);

修改成:

if($tag_command == cacheless) $this->_plugins[block][$tag_command] = array($plugin_func, null, null, null, false);
else $this->_plugins[block][$tag_command] = array($plugin_func, null, null, null, true);

你也可以直接将原句的最后一个参数改成false,即关闭默认缓存。

(3)使用register_function阻止插件从缓存中输出

index.tpl:

<p>{current_time}{/p}
index.php:
function smarty_function_current_time($params, &$smarty){
  return date("Y-m-d H:m:s");
}
$smarty=new smarty();
$smarty->caching = true;
$smarty->register_function('current_time','smarty_function_current_time',false);
if(!$smarty->is_cached()){
  .......
}
$smarty->display('index.tpl');

注解:

定义一个函数,函数名格式为:smarty_type_name($params, &$smarty)
type为function

name为用户自定义标签名称,在这里是{current_time}

两个参数是必须的,即使在函数中没有使用也要写上。两个参数的功能同上。

分享到:  QQ好友和群QQ好友和群
收藏收藏
回复

使用道具 举报

0

主题

21

帖子

71

积分

注册会员

Rank: 2

积分
71
沙发
发表于 2022-8-15 12:22 | 只看该作者
音乐下载网站免费
回复

使用道具 举报

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表