<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>QLJ's Blog &#187; smarty</title>
	<atom:link href="http://qlj.sh.cn/tag/smarty/feed/" rel="self" type="application/rss+xml" />
	<link>http://qlj.sh.cn</link>
	<description>搜集网络信息,风云磅实验</description>
	<lastBuildDate>Thu, 26 Jan 2012 21:20:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>smarty cache缓存的使用</title>
		<link>http://qlj.sh.cn/php/20100520/smarty-cache/</link>
		<comments>http://qlj.sh.cn/php/20100520/smarty-cache/#comments</comments>
		<pubDate>Wed, 19 May 2010 18:46:00 +0000</pubDate>
		<dc:creator>qlj</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[smarty]]></category>
		<category><![CDATA[缓存]]></category>

		<guid isPermaLink="false">http://qlj.sh.cn/?p=1080</guid>
		<description><![CDATA[机器实在太老了。。
在增加到8W条文章
TAG表已经增加到了30多万条了
10 个表  总计 415,762
在我那个老电脑上。。（p4 1.6   256内存）
很快。。内存不足了。。只剩下4MB剩余内存
于是TAOBAO上买了个 512的SDRAM

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

$Smarty -&#62; caching=true;
$Smarty-&#62;cache_lifetime = 86400;
if&#40;!$Smarty-&#62;is_cached&#40;'index.html'&#41;&#41;&#123;
&#160;
......
&#160;
&#125;
&#160;
//文章页 缓存 1周
$Smarty -&#62; caching=true;
$Smarty-&#62;cache_lifetime = 7*86400;
if&#40;!$Smarty-&#62;is_cached&#40;'article.php',$id&#41;&#41;&#123;
&#125;

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

function nocache_block&#40;$params,$content,Smarty $Smarty&#41;&#123;
	return $content;
	&#125;
	$Smarty -&#62; register_block&#40;'nocache','nocache_block',false&#41;;

调用

	&#60;p&#62;
	Processed in &#60;!--{nocache}--&#62;&#60;!--{$runtime}--&#62;&#60;!--{/nocache}--&#62; &#60;/p&#62;

引用另一种防止CACHE的方法
1、使用insert函数使模板的一部分不被缓存
首先在php页面中

&#60;?php
 function insert_get_now_time&#40;&#41;
 &#123;
  return date&#40;&#34;Y-m-d h:i:s&#34;,time&#40;&#41;+3600*8&#41;;
 &#125;
&#160;
&#160;
&#160;
&#160;
&#160;
?&#62;
//html调用方法
现在时间为：&#60;{insert name=&#34;get_now_time&#34;}&#62;

意：首先 函数命名一定要 以 insert_ 开头 后面紧跟着 模版中的函数名字
      只要定义了函数 smarty [...]]]></description>
			<content:encoded><![CDATA[<p>机器实在太老了。。<br />
在增加到8W条文章<br />
TAG表已经增加到了30多万条了<br />
10 个表  总计 415,762<br />
在我那个老电脑上。。（p4 1.6   256内存）<br />
很快。。内存不足了。。只剩下4MB剩余内存<br />
于是TAOBAO上买了个 512的SDRAM<br />
<span id="more-1080"></span><br />
MYSQL 真是个费劲的家伙</p>
<p>原本将一些 列表等。缓存了。已减少查询。。在5W文章内。还不是很吃力<br />
没想到 到了8W后。 要等好几秒才刷出来。。而PHP显示的执行时间却依然是不到0.2秒<br />
在加内存后。<br />
已经可以正常运行了。<br />
但是看CPU使用率还是挺高的 于是决定。直接使用SMARTY缓存</p>
<p>首页缓寸一天</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$Smarty</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">caching</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cache_lifetime</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">86400</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_cached</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index.html'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #339933;">......</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//文章页 缓存 1周</span>
<span style="color: #000088;">$Smarty</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">caching</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cache_lifetime</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">86400</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_cached</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'article.php'</span><span style="color: #339933;">,</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>为了防止把执行速度也CACHE了</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> nocache_block<span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #339933;">,</span><span style="color: #000088;">$content</span><span style="color: #339933;">,</span>Smarty <span style="color: #000088;">$Smarty</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$Smarty</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">register_block</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'nocache'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'nocache_block'</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>调用</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	Processed in <span style="color: #808080; font-style: italic;">&lt;!--{nocache}--&gt;&lt;!--{$runtime}--&gt;&lt;!--{/nocache}--&gt;</span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>引用另一种防止CACHE的方法</p>
<p>1、使用insert函数使模板的一部分不被缓存</p>
<p>首先在php页面中</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
 <span style="color: #000000; font-weight: bold;">function</span> insert_get_now_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d h:i:s&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">3600</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
//html调用方法
现在时间为：&lt;{insert name=&quot;get_now_time&quot;}&gt;</pre></div></div>

<p>意：首先 函数命名一定要 以 insert_ 开头 后面紧跟着 模版中的函数名字<br />
      只要定义了函数 smarty 会自动 加载其函数 。</p>
<p>另一种<br />
2使用register_function阻止插件从缓存中输出</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
index<span style="color: #339933;">.</span>tpl<span style="color: #339933;">:</span>
<span style="color: #339933;">&lt;</span>div<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span>current_time<span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
index<span style="color: #339933;">.</span>php<span style="color: #339933;">:</span>
<span style="color: #000000; font-weight: bold;">function</span> smarty_function_current_time<span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$smarty</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d H:m:s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$smarty</span><span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> smarty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">caching</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">register_function</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'current_time'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'smarty_function_current_time'</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_cached</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">.......</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">display</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index.tpl'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>注解：<br />
定义一个函数，函数名格式为：<a href="http://qlj.sh.cn/tag/smarty/">smarty</a>_type_name($params, &#038;$smarty)<br />
type为function<br />
name为用户自定义标签名称，在这里是{current_time}<br />
两个参数是必须的，即使在函数中没有使用也要写上。两个参数的功能同上。</p>
]]></content:encoded>
			<wfw:commentRss>http://qlj.sh.cn/php/20100520/smarty-cache/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

