- removing support for $cache_attrs for registered plugins,
- removing the undocumented {make_nocache} tag and the deprecated {insert} tag and associated code
- removing support for a compile_id property on include tags.
Fixes a bug in extends: resources by propagating the nocache-hashes between a master template and it's subtemplates in \Smarty\Template::_subTemplateRender. This might need further improvement.
4.9 KiB
Setting Up Caching
The first thing to do is enable caching by setting
$caching to one of
\Smarty\Smarty::CACHING_LIFETIME_CURRENT or Smarty::CACHING_LIFETIME_SAVED.
<?php
require('Smarty.class.php');
$smarty = new Smarty;
// uses the value of $smarty->cacheLifetime() to determine
// the number of seconds a cache is good for
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
$smarty->display('index.tpl');
?>
With caching enabled, the function call to display('index.tpl') will
render the template as usual, but also saves a copy of its output to a
file (a cached copy) in the $cache_dir. On the
next call to display('index.tpl'), the cached copy will be used
instead of rendering the template again.
Note
The files in the
$cache_dirare named similar to the template name. Although they end in the.phpextension, they are not intended to be directly executable. Do not edit these files!
Each cached page has a limited lifetime determined by
$cache_lifetime. The default value is 3600
seconds, or one hour. After that time expires, the cache is regenerated.
It is possible to give individual caches their own expiration time by
setting $caching to
\Smarty\Smarty::CACHING_LIFETIME_SAVED. See
$cache_lifetime for more details.
<?php
require('Smarty.class.php');
$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');
?>
If $compile_check is enabled (default),
every template file and config file that is involved with the cache file
is checked for modification. If any of the files have been modified
since the cache was generated, the cache is immediately regenerated.
This is a computational overhead, so for optimum performance set
$compile_check to FALSE.
<?php
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
$smarty->setCompileCheck(false);
$smarty->display('index.tpl');
?>
If $force_compile is enabled, the cache
files will always be regenerated. This effectively disables caching,
however this also seriously degrades performance.
$force_compile is meant to be used for
debugging purposes. The appropriate way to
disable caching is to set $caching to
\Smarty\Smarty::CACHING_OFF.
The isCached() function can be used to test if a
template has a valid cache or not. If you have a cached template that
requires something like a database fetch, you can use this to skip that
process.
<?php
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
if(!$smarty->isCached('index.tpl')) {
// No cache available, do variable assignments here.
$contents = get_database_contents();
$smarty->assign($contents);
}
$smarty->display('index.tpl');
?>
You can keep parts of a page dynamic (disable caching) with the
{nocache}{/nocache} block function, or by using the
nocache parameter for most template functions.
Let's say the whole page can be cached except for a banner that is
displayed down the side of the page. By using a {nocache}{/nocache}
block for the banner, you can
keep this element dynamic within the cached content.
You can clear all the cache files with the
clearAllCache() function, or individual cache
files and groups with the
clearCache() function.
<?php
require('Smarty.class.php');
$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();
$smarty->display('index.tpl');
?>