PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

有关smarty缓存的应用

原创
2016-07-25 09:10:28 769浏览
  1. $smarty->cache-dir="目录名"; //创建缓存目录名
  2. $smarty->caching=true; //开启缓存,为false的时候缓存无效
  3. $smarty->cache_lifetime=60; //缓存时间,单位是秒
复制代码

二、Smarty缓存的使用与清除

  1. $marty->display("cache.tpl",cache_id); //创建带ID的缓存
  2. $marty->clear_all_cache(); //清楚所有缓存
  3. $marty->clear_cache("index.php"); //清楚index.php中的缓存
  4. $marty->clear_cache("index.php',cache_id); //清楚index.php中指定ID的缓存
复制代码

三、Smarty的局部缓存 第一个: insert_函数默认是不缓存,这个属性是不能修改 使用方法:例子 index.php中,

  1. function insert_get_time(){
  2. return date("Y-m-d H:m:s");
  3. }
复制代码

index.html中,

  1. {insert name="get_time"}
复制代码

第二个: smarty_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的内容如下:

  1. function smarty_block_cacheless($param, $content, &$smarty) {
  2. return $content;
  3. }
  4. ?>
复制代码

2) 编写程序及模板 示例程序:testCacheLess.php

  1. include('Smarty.class.php');
  2. $smarty = new Smarty;
  3. $smarty->caching=true;
  4. $smarty->cache_lifetime = 6;
  5. $smarty->display('cache.tpl');
  6. ?>
复制代码

所用的模板:cache.tpl 已经缓存的:{$smarty.now}
{cacheless} 没有缓存的:{$smarty.now} {/cacheless} 四、自定义缓存 设置cache_handler_func使用自定义的函数处理缓存 如:

  1. $smarty->cache_handler_func = "myCache";
  2. function myCache($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){
  3. }
复制代码

该函数的一般是根椐$action来判断缓存当前操作:

  1. switch($action){
  2. case "read"://读取缓存内容
  3. case "write"://写入缓存
  4. case "clear"://清空
  5. }
复制代码

一般使用md5($tpl_file.$cache_id.$compile_id)作为唯一的cache_id 如果需要,可使用gzcompress和gzuncompress来压缩和解压。



声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。