Get Smarty

Donate

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

Advertisement

Name

isCached() — 检查模板是否已经缓存

说明

bool isCached(string template,
              string cache_id,
              string compile_id);
  • 此函数仅在 $caching设置为Smarty::CACHING_LIFETIME_CURRENTSmarty::CACHING_LIFETIME_SAVED的时候启用。 参见 缓存

  • 当你需要使用多个缓存时,需要设置 $cache_id这个可选参数。

  • 你可以传递第三个可选的参数 $compile id。 如果省略了该参数,那么会继续使用之前的 $compile_id,如果它有设置的话。

  • 如果你不希望传递$cache_id但想传递 $compile_id参数,你可以传递一个NULL作为$cache_id.

技术说明

如果isCached()返回 TRUE ,那么系统底层将已经载入了缓存的内容并存储到系统中。 接着执行的display() 或者 fetch() 将直接返回存储在系统中的缓存内容,而不会再从缓存文件内读取。 这个机制避免了在执行isCached()display()的时候重复两次读取缓存内容,导致系统开销。 这也意味着,在isCached() 返回TRUE后,调用的 clearCache() 或者其他缓存设置,将不会影响缓存的内容输出。

Example 14.32. isCached()


<?php
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

if(!$smarty->isCached('index.tpl')) {
// do database calls, assign vars here
}

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

   

Example 14.33. isCached()用于多缓存的模板


<?php
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

if(!$smarty->isCached('index.tpl', 'FrontPage')) {
  // do database calls, assign vars here
}

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

   

参见 clearCache(), clearAllCache(), 和 缓存.

Comments
No comments for this page.

Advertisement

Sponsors [info]

Sponsors