专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »PHP教程 » smarty缓存:Smarty的缓存Cache操作窍门技巧 »正文

smarty缓存:Smarty的缓存Cache操作窍门技巧

来源: 发布时间:星期一, 2009年1月12日 浏览:12次 评论:0
  、使用缓存Cache

  要开启smarty缓存Cache,只需将caching设为true,并指定cache_dir即可.

  使用cache_lefetime指定缓存Cache生存时间,单位为秒

  要对相同页面生成多个区别缓存Cache,在display或fetch中加入第 2参数cache_id,如$smarty->display('index.tpl',$my_cache_id);此特性可用于对区别$_GET进行区别缓存Cache

   2、清除缓存Cache

  clear_all_cache;//清除所有缓存Cache

  clear_cache('index.tpl');//清除index.tpl缓存Cache

  clear_cache('index.tpl',cache_id);//清除指定id缓存Cache

   3、使用自定义缓存Cache方式

  设置cache_handler_func使用自定义处理缓存Cache

  如:

  $smarty->cache_handler_func = "myCache";

  function myCache($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){

  }

  该般是根椐$action来判断缓存Cache当前操作:

switch($action){
"read"://读取缓存Cache内容
"write"://写入缓存Cache
"clear"://清空
}


  般使用md5($tpl_file.$cache_id.$compile_id)作为唯cache_id

  如果需要,可使用gzcompress和gzuncompress来压缩和解压

   4、局部关闭缓存Cache

  要在某些区域使缓存Cache失效(只对需要缓存Cache),有几种思路方法:

  inser:

  定义个inser标签要使用处理,名格式为:insert_xx(.gif' /> $params, object &$smarty)其中xx是insertname,也就是说,如果你定义为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){ $content;} //name表示区域名

  注册block:$smarty->register_block('name', 'smarty_block_name', false); //第 3参数false表示该区域不被缓存Cache

  模板写法:{name}内容{/name}

  写成block插件:

  1)定义件插件:block.cacheless.php,放在smartyplugins目录

  block.cacheless.php内容如下:

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


  2) 编写及模板

  举例:testCacheLess.php

<?php
('Smarty..php');
$smarty = Smarty;
$smarty->caching=true;
$smarty->cache_letime = 6;
$smarty->display('cache.tpl');
?>


  所用模板:cache.tpl

  已经缓存Cache:{$smarty.now}<br>

  {cacheless}

  没有缓存Cache:{$smarty.now}

  {/cacheless}

0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: