smarty cache缓存的使用

date:星期四, 五月 20th, 2010 at 2:46 上午 Categories:php

机器实在太老了。。
在增加到8W条文章
TAG表已经增加到了30多万条了
10 个表 总计 415,762
在我那个老电脑上。。(p4 1.6 256内存)
很快。。内存不足了。。只剩下4MB剩余内存
于是TAOBAO上买了个 512的SDRAM

MYSQL 真是个费劲的家伙

原本将一些 列表等。缓存了。已减少查询。。在5W文章内。还不是很吃力
没想到 到了8W后。 要等好几秒才刷出来。。而PHP显示的执行时间却依然是不到0.2秒
在加内存后。
已经可以正常运行了。
但是看CPU使用率还是挺高的 于是决定。直接使用SMARTY缓存

首页缓寸一天

$Smarty -> caching=true;
$Smarty->cache_lifetime = 86400;
if(!$Smarty->is_cached('index.html')){
 
......
 
}
 
//文章页 缓存 1周
$Smarty -> caching=true;
$Smarty->cache_lifetime = 7*86400;
if(!$Smarty->is_cached('article.php',$id)){
}

为了防止把执行速度也CACHE了

function nocache_block($params,$content,Smarty $Smarty){
	return $content;
	}
	$Smarty -> register_block('nocache','nocache_block',false);

调用

	<p>
	Processed in <!--{nocache}--><!--{$runtime}--><!--{/nocache}--> </p>

引用另一种防止CACHE的方法

1、使用insert函数使模板的一部分不被缓存

首先在php页面中

<?php
 function insert_get_now_time()
 {
  return date("Y-m-d h:i:s",time()+3600*8);
 }
 
 
 
 
 
?>
//html调用方法
现在时间为:<{insert name="get_now_time"}>

意:首先 函数命名一定要 以 insert_ 开头 后面紧跟着 模版中的函数名字
只要定义了函数 smarty 会自动 加载其函数 。

另一种
2使用register_function阻止插件从缓存中输出

 
index.tpl:
<div>{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}
两个参数是必须的,即使在函数中没有使用也要写上。两个参数的功能同上。

One Response to “smarty cache缓存的使用”

  1. licbNo Gravatar

    学习!
    我在使用Smarty的时候出现这样一个错误:
    /*%%SmartyNocache:1128574894ddc906076ce80-14490853%%*//*/%%SmartyNocache:1128574894ddc906076ce80-14490853%%*/

    请问该怎么解决呢?
    谢谢哈!

    [Reply]

Leave a Reply