smarty_smarty缓存的配置、使用和清除

来源:互联网 发布:天使爱美丽影评 知乎 编辑:程序博客网 时间:2024/05/08 12:06

smarty缓存的配置、使用和清除

1、smarty缓存的配置
$smarty->cache_dir = "/caches/";   //缓存目录
$smarty->caching = true; //开启缓存,为false时为无效
$smarty->cache_lifetime = 60; //缓存时间

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

1>cache.php代码如下:
<?php

include ("smarty_inc.php");
function insert_shijian(){
   return date("y-m-d H:i:s");
}
           //实现实时更新时间
             //以"insert_"开头的函数在PHP中是不缓存的,这样就可以在实现某些内容的实时更新


$id = $_GET['id'];
$value = array(
   '姓名' => '肖红阳',
   '性别' => '男',
   '籍贯' => '湖南'
);
$smarty->assign("mess",$value);
$smarty->assign("id",$id);
$smarty->display("cache.htm",$id);
//$smarty->clear_cache('cache.htm',$id); //删除cache.htm页面的缓存和$id生成的缓存
?>


2>smarty_inc.php代码如下:
<?php
/*
* Created on 2010-6-30
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
include_once("Smarty/Smarty.class.php");//包含smarty类文件
$smarty = new smarty();//建立Smarty实例对象$Smarty
$smarty->config_dir="Smarty/Config_File.class.php";//目录变量
$smarty->caching=false;//是否使用缓存,项目在调试期间,不建议使用缓存
$smarty->template_dir = "./templates";//设置模板目录
$smarty->compile_dir = "./templates_c";//设置编译目录
$smarty->cache_dir="./caches";//缓存文件夹
$smarty->caching = true; //开启缓存
$smarty->cache_lifetime = 600;//缓存时间
//------------------------
//左右边界符,默认为{},但实际应用当中容易与Javascript冲突
//-------------------------------
$smarty->left_delimiter="{";
$smarty->right_delimiter= "}";
?>

3>cache.htm代码如下:
{foreach from = $mess item =item}
{$item}<br>
{foreachelse}
{/foreach}
{insert name='shijian'}   <!-- 引入实时更新的时间 -->

运行结果:
肖红阳

湖南
10-07-10 11:35:55