`
xubaoguo
  • 浏览: 77385 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

php smarty 缓存使用

阅读更多
一、使用缓存
 
要开启smarty的缓存,只需将caching设为true,并指定cache_dir即可.
使用cache_lefetime指定 缓存生存时间,单位为秒
要对相同页面生成多个不同的缓存,在display或fetch中加入第二参数cache_id, 如$smarty->display('index.tpl',$my_cache_id);此特性可用于对不同的$_GET进行不同的缓存
 
二、清除缓存

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

三、使用自定义缓存方式

设置cache_handler_func使用自定义的函数处理缓存
如:
$smarty->cache_handler_func = "myCache";
function myCache($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){
}
该 函数的一般是根椐$action来判断缓存当前操作:
switch($action){
case "read"://读取缓存内容
case "write"://写入缓存
case "clear"://清空
}
一般使用 md5($tpl_file.$cache_id.$compile_id)作为唯一的cache_id
如果需要,可使用gzcompress和 gzuncompress来压缩和解压
 
四、局部关闭缓存

要在某些区域使缓存失效(只对需要的缓存),有几种方法:
inser:
定 义一个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定义同上
register_block:
定 义一个block:smarty_block_name($params,$content, &$smarty){return $content;} //name表示区域名
注册block:$smarty->register_block('name', 'smarty_block_name', false); //第三参数false表示该区域不被缓存
模板写法:{name}内容 {/name}
写成block插件:
1)定义一件插件函数:block.cacheless.php,放在smarty的plugins目录
block.cacheless.php 的内容如下:

<?php
function smarty_block_cacheless($param, $content, &$smarty) {
return $content;
}
?>
2) 编写程序及模板
示例程序: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}
现在运行一下,发现是不起作用的,两行内容都被缓存了
3)改写Smarty_Compiler.class.php(注:该文件很重 要,请先备份,以在必要时恢复)
查找$this->_plugins['block'][$tag_command] = array($plugin_func, null, null, null, true); //我的在705行
修改成:
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,我对smarty的内部机制不太了解,所以加了一个判断,只要block是 cacheless的才不作缓存
4)OK,现在清除template_c里的编译文件,重新运行,起作用了吧?

 

分享到:
评论

相关推荐

    PHP100视频教程 30:PHP模板引擎Smarty缓存应用

     //缓存时间2、Smarty缓存的使用和清除 $smarty-&gt;display('cache.tpl', cache_id); //创建带ID的缓存 $smarty-&gt;clear_all_cache(); //清除所有缓存 $smarty-&gt;clear_cache('index.htm'); //清除index.tpl的缓存...

    (第30讲) PHP模板引擎Smarty缓存应用

    1、Smarty缓存的配置 2、Smarty缓存的使用和清除 3、Smarty局部缓存 4、MYSQL与Smarty的应用 暂时30张视频讲解,有空再上传!

    PHP100视频教程 30:PHP模板引擎Smarty缓存应用.rar

    2、Smarty缓存的使用和清除  $smarty-&gt;display('cache.tpl', cache_id); //创建带ID的缓存  $smarty-&gt;clear_all_cache(); //清除所有缓存  $smarty-&gt;clear_cache('index.htm'); //清除index.tpl的缓存  $...

    PHP100视频教程30:PHP模板引擎Smarty缓存应用.rar

    PHP100视频教程30:PHP模板引擎Smarty缓存应用.rar

    PHP模板引擎Smarty的缓存用法_.docx

    PHP模板引擎Smarty的缓存用法_.docx

    PHP模板引擎Smarty的缓存使用总结

    大家应该都知道合理使用缓存能有效的减轻网站的服务器压力,php Smarty作为一个非常优秀的php模板引擎,它为我们提供了非常简单而多样化的缓存操作,下面就让我们学习一下smarty缓存操作方面的一些技巧

    smarty模板中文手册

    3. 缓存技术:Smarty选用的一种缓存技术,它可以将用户最终看到的HTML文件缓存成一个静态的HTML页,当设定Smarty的cache属性为true时,在Smarty设定的cachetime期内将用户的WEB请求直接转换到这个静态的HTML文件中来...

    PHP文件缓存smarty模板应用实例分析

    主要介绍了PHP文件缓存smarty模板应用方法,结合实例形式较为详细的分析了smarty模板缓存的相关使用技巧,需要的朋友可以参考下

    php模板解析类文件缓存

    php类smarty解析的过程以及文件缓存

    Smarty中文手册 chm版

    Smarty/PHP errors [Smarty/PHP 错误] 18. Tips & Tricks [使用技巧和经验] Blank Variable Handling [空白变量处理] Default Variable Handling [默认变量处理] Passing variable title to header template ...

    smarty手册.chm

    Smarty/PHP errors [Smarty/PHP 错误] 18. Tips & Tricks [使用技巧和经验] Blank Variable Handling [空白变量处理] Default Variable Handling [默认变量处理] Passing variable title to header template ...

    《php开发典型模块大全》读书笔记和调试源代码 第四章smarty (张迅雷闪击PHP系列)

    测试使用的是Smarty-2.6.26\libs 公用的smarty类库和相关信息,将smarty放在服务器根目录下,配置信息写在一个文件中,用的时候include下配置文件。 smarty的变量有以下三部分: a 来自php页面中的变量,即assign()...

    麦兜多主题投票系统V2.0版本(php+mysql+smarty)

    较之前1.1版本,功能没增加,只是改用smarty模版分离,模版放在tm文件夹下,tp为编译后文件夹,smarty_cache缓存文件夹, Smarty/Smarty文件夹为Smarty模板引擎所有文件,Smarty文件夹下smarty_inc.php为smarty的配制...

    PHP Smarty 模板引擎手册 CHM.rar

    Smarty 模板引擎教程 chm格式,内容主要有基本语法、变量、组合修改器、内建函数、自定义函数、配置文件、控制台调试、缓存、插件扩展、使用技巧和经验等,比较详细的一个中文手册,如上图示。

    php smarty模版引擎中的缓存应用

    php smarty模版引擎中的缓存应用实例代码。

    PHP100视频教程全集112集BT种子【PHP经典】

    PHP100视频教程30:PHP模板引擎Smarty缓存应用 PHP100视频教程31:PHP在线编辑器fckeditor应用 PHP100视频教程32:PHP5中Cookie与 Session详解 PHP100视频教程33:PHP5中图片验证码的制作(上) PHP100视频教程...

    smarty3.x完全中文手册.7z

    模板设计者篇 Table of Contents[内容列表] 3. Basic Syntax [基本语法] 4. Variables [变量] 5. Variable Modifiers [变量修改器] ...16. Extending Smarty With Plugins [利用插件扩展Smarty]

    明仔中文网のPHP SMARTY留言本(全注释版本).rar

    全程序采用摸版引擎制作,应用了摸版分离,缓存应用 虽然小 也算是五幛具全 功能如下---------------- 1==有2个类== 数据库类 lib/ 目录下放着一个入门及的数据库类 分页类 Page/ 目录下放着一个 强大的一个...

Global site tag (gtag.js) - Google Analytics