跳至内容

缓存

缓存用于通过保存和重复使用输出内容来加速模板的渲染速度。

如果缓存版本的调用可用,将显示,而不是重新生成输出内容。缓存可以极大地加速运行速度,特别是计算时间较长的模板。

由于模板可以包含或扩展其他模板,一个缓存文件可能由若干模板文件、配置文件等组成。

** 注意 **

由于模板是动态的,因此非常注意缓存的内容以及缓存多长时间非常重要。例如,如果你显示不经常更改其内容的网站首页,将此页面缓存一个小时或更长时间可能效果很好。另一方面,如果你显示一个页面,其中包含一分钟内的新信息的时间表,那么缓存此页面没有任何意义。

设置缓存

首先是通过调用 Smarty::setCaching()启用缓存,参数为 \Smarty\Smarty::CACHING_LIFETIME_CURRENT\Smarty\Smarty::CACHING_LIFETIME_SAVED。或者使用 \Smarty\Smarty::CACHING_OFF 来再次禁用缓存。

<?php
use Smarty\Smarty;
$smarty = new Smarty;

// enable caching, using the current lifetime (see below)
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

// enable caching, using the lifetime set when the cache was saved (see below)
$smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED);

// disable caching
$smarty->setCaching(Smarty::CACHING_OFF);

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

启用缓存后,对 $smarty->display('index.tpl') 的函数调用会按平常渲染模板,但还保存其输出内容的副本。在对 $smarty->display('index.tpl') 进行的下次调用中,将使用缓存的副本,而不是再次渲染模板。

注意

默认情况下,Smarty 会将缓存作为文件保存在名为 cache 的目录中,该目录相对于当前目录。可以通过 $smarty->setCacheDir('/some/cache/dir'); 更改默认目录。这些文件的名称与模板名称类似。虽然它们以 .php 扩展名结尾,但它们并不是为了直接执行而设计的。不要编辑这些文件!

缓存生命周期

每个缓存页面的有效期都是有限的。默认值为 3600 秒或一小时。时间到期后,将重新生成缓存。

你可以按照以下方式更改有效期

<?php
use Smarty\Smarty;
$smarty = new Smarty;

$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT); 
// or $smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED);

// set the cache_lifetime to 5 minutes
$smarty->setCacheLifetime(5 * 60);

将缓存设置为 \Smarty\Smarty::CACHING_LIFETIME_CURRENT 值,会告知 Smarty 使用当前有效期来确定缓存是否已过期。

\Smarty\Smarty::CACHING\_LIFETIME\_SAVED 的值会告知 Smarty 使用生成缓存时的有效期值。通过这种方式,可以在呈现模板之前设置以细粒度控制特定缓存过期的时间。

一个示例

<?php
use Smarty\Smarty;
$smarty = new Smarty;

// retain current cache lifetime for each specific display call
$smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED);

// set the cache_lifetime for index.tpl to 5 minutes
$smarty->setCacheLifetime(300);
$smarty->display('index.tpl');

// set the cache_lifetime for home.tpl to 1 hour
$smarty->setCacheLifetime(3600);
$smarty->display('home.tpl');

// NOTE: the following $cache_lifetime setting will not work when $caching
// is set to Smarty::CACHING_LIFETIME_SAVED.
// The cache lifetime for home.tpl has already been set
// to 1 hour, and will no longer respect the value of $cache_lifetime.
// The home.tpl cache will still expire after 1 hour.
$smarty->setCacheLifetime(30); // 30 seconds
$smarty->display('home.tpl');

编译检查

默认情况下,将检查与缓存文件相关的每个模板文件和配置文件是否有修改。如果自缓存生成后有任何文件被修改,则会立即重新生成缓存。

这是一个计算开销,因此为了获得最佳性能,请在生产环境中禁用此功能

<?php
use Smarty\Smarty;
$smarty = new Smarty;

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

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

检查模板是否已缓存

可以使用 Smarty 的 `isCached()` 方法来测试模板是否有有效的缓存。如果你有一个需要像数据库获取之类操作的缓存模板,则可以使用此方法来跳过该过程。

<?php
use Smarty\Smarty;
$smarty = new Smarty;

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

if (!$smarty->isCached('index.tpl')) {
    // No cache available, do variable assignments here.
    $smarty->assign('data', do_expensive_database_calls());
}

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

非缓存块

你可以使用 {nocache}{/nocache} 块函数或对大多数模板函数使用 nocache 参数,使页面的部分内容保持动态(禁用缓存)。

假设整个页面都可以缓存,但页面侧边栏显示的横幅除外。通过对横幅使用 {nocache}{/nocache} 块,可以在缓存内容中保持此元素为动态。

清除缓存

可以使用 Smarty 的 clearAllCache() 方法清除所有缓存文件,或使用 clearCache() 方法清除单个缓存文件。

<?php
use Smarty\Smarty;
$smarty = new Smarty;

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

// clear only cache for index.tpl
$smarty->clearCache('index.tpl');

// clear out all cache files
$smarty->clearAllCache();

// clear out all cache files older than one hour
$smarty->clearAllCache(3600);

// or, clear all expired caches
$smarty->clearAllCache(Smarty::CLEAR_EXPIRED);