Home  >  Article  >  Backend Development  >  Smarty模板引擎缓存机制详解,smarty模板引擎详解_PHP教程

Smarty模板引擎缓存机制详解,smarty模板引擎详解_PHP教程

WBOY
WBOYOriginal
2016-07-12 08:50:40679browse

Smarty模板引擎缓存机制详解,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}
{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:

{current_time}{/div} 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}

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

更多关于Smarty相关内容感兴趣的读者可查看本站专题:《smarty模板入门基础教程》、《PHP模板技术总结》、《PHP基于pdo操作数据库技巧总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1133106.htmlTechArticleSmarty模板引擎缓存机制详解,smarty模板引擎详解 本文实例讲述了Smarty模板引擎缓存机制。分享给大家供大家参考,具体如下: 首先说下s...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn