Get Smarty

Donate

Donate Bitcoin Bitcoin
Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Buy cheap eyeglasses from Cheapglasses123.com and save up to 80%.

Buy prescription glasses from www.australiaglasses.com and save.

Cheap Glasses Now On Sale at GlassesPeople.com. Starts At $7.95.

Where to buy discount wedding dresses and cheap smart dresses free shipping - Weddingdresstrend.com

Brautkleider auf Topwedding.de

Find free files to download on allwhatyouwant.net

Looking For Affordable Wedding Dresses 2015 at Best Prices On TDBridal.com

Shop high quality cheap prom dresses on Dresswe.co.uk

Buy New Arrival Cheap Prom Dresses 2015 at JDBRIDAL Prom Dress Store

Advertisement

Chapter 15. 缓存

通过将输出内容�存到文件内,缓存机制�以用�加速 display() 或者fetch()的执行。 如果缓存被开�,那么显示时缓存的输出将替代�新生�显示内容的�作。 缓存�以�大�高程�的执行速度,尤其当模�需�很长的计算时间的情况下。 当display() 或者 fetch()开�缓存, Smarty将生�对应的缓存文件,缓存文件包�了多个模�文件的内容,�置文件内容等等。

当页�是动�的内容,那么必须谨慎考虑你的缓存和缓存时间。 比如说,你的网站首页通常�会更新得太快,那么�以将其缓存一个�时或者更多,以便加速其显示。 从�一方�讲,如果你的页�上有按秒�显示新内容的功能,那么该页�就�应该被缓存。

�置缓存时间

缓存�以通过设置 $caching为:Smarty::CACHING_LIFETIME_CURRENT 或 Smarty::CACHING_LIFETIME_SAVED�开�。

Example 15.1. 开�缓存


<?php
require('Smarty.class.php');
$smarty = new Smarty;

// 使用$smarty->cacheLifetime()�以更精确定义缓存时间

$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

$smarty->display('index.tpl');
?>

    

开�了缓存,调用display('index.tpl') 会正常渲染模�,但也会�存一份输出的内容到$cache_dir目录下的文件中(缓存副本)。 在下一次调用display('index.tpl'), 缓存文件会替代渲染模�的过程。

技术说明

在$cache_dir下的文件,文件�与模��称相似。 虽然这些文件也有.php �缀,但它们�会被直接执行。请��编辑这些文件�

�个缓存页�都有一个缓存过期时间$cache_lifetime。 默认是3600秒,也就是一�时。 当超过了此时间,缓存将被�新生�。 当设置$caching为Smarty::CACHING_LIFETIME_SAVED时,�以给�个缓存设置其�独的缓存时间。 ��$cache_lifetime。

Example 15.2. 为�个缓存设置$cache_lifetime


<?php
require('Smarty.class.php');
$smarty = new Smarty;

// 让�个缓存的过期时间都�以在display执行��独设置。
$smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED);

// 设置index.tpl的过期时间为5分钟
$smarty->setCacheLifetime(300);
$smarty->display('index.tpl');

// 设置home.tpl的过期时间为1�时
$smarty->setCacheLifetime(3600);
$smarty->display('home.tpl');

// 注�:当$caching设置了Smarty::CACHING_LIFETIME_SAVED�,
// 下�的$cache_lifetime将�会起效。
// home.tpl已�设置了过期时间为1�时,
// 所以�会��循下�的$cache_lifetime值,
// home.tpl的过期时间还是1�时。
$smarty->setCacheLifetime(30); // 30 ç§’
$smarty->display('home.tpl');
?>

    

当 $compile_check开�的时候(默认开�), �个模�文件和�置文件都会在缓存检查的时候执行编译检查。 如果这些文件在缓存生��被修改,那么缓存会马上�新生�。 这是一个覆盖的选项,所以更好的性能建议把 $compile_check 设置�false。

Example 15.3. 关闭 $compile_check


<?php
require('Smarty.class.php');
$smarty = new Smarty;

$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
$smarty->setCompileCheck(false);

$smarty->display('index.tpl');
?>

    

如果开�了 $force_compile, 缓存文件将总是会�新生�。效果和关闭缓存是一样的, 而且还会�低性能。 $force_compile 一般用于 调试的目的。 更确当的方�是把缓存$caching 设置�Smarty::CACHING_OFF。

isCached() 函数�以检查模�的缓存是�存在。 如果你的模�是需�读��些数�(如数�库), 那么你�以用它�跳过这个过程。

Example 15.4. 使用 isCached()


<?php
require('Smarty.class.php');
$smarty = new Smarty;

$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

if(!$smarty->isCached('index.tpl')) {
	// 找�到缓存,这里进行一些赋值�作
    $contents = get_database_contents();
    $smarty->assign($contents);
}

$smarty->display('index.tpl');
?>

    

你�以使用{nocache}{/nocache}�设置页�上部分区�是动�的(�缓存), �样你也�以使用 {insert}函数,或者nocache�数�达到�样目的。

比如说我们希望把整个页�缓存,除了页�两边显示的banner广告。 那么我们�以用{insert}�显示banner广告, 这样就�以在缓存的内容里�,�存广告部分的动�效果。 �� {insert}的详细文档。

你�以通过clearAllCache()�删除全部缓存,或者 用clearCache() �删除特定的缓存组 的缓存内容。

Example 15.5. 删除缓存


<?php
require('Smarty.class.php');
$smarty = new Smarty;

$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

// 仅删除index.tpl的缓存
$smarty->clearCache('index.tpl');

// 删除全部缓存
$smarty->clearAllCache();

$smarty->display('index.tpl');
?>

    

Comments
No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 16 plus 19? (Are you human?)

Advertisement