Section 5.2. Smarty Optimizations

Optimization Techniques > Smarty Optimizations

5.2. Smarty Optimizations

Let's take a look at Smarty cache facilities and see what can be done with them.

Smarty cache is enabled by setting $cache setting to 1 or 2. The difference is that with 2 Smarty uses the lifetime value at the time the cache was generated; this is useful if you need different lifetimes on each template. Notice that if the $force_compile setting is enabled, Smarty will always regenerate the cache, and if $compile_check is enabled, every time the template is regenerated the cache will be, too.

Cache lifetime is defined by the $cache_lifetime setting. This can be defined globally for all the templates, or by individual template.

Here is the cache lifetime defined by template:

<?php // Load the Smarty library. require_once 'Smarty.class.php'; // Instantiate a Smarty object. $smarty = new Smarty; // Use the lifetime value at the time the cache is generated. $smarty->caching = 2; // Set the cache lifetime. $smarty->cache_lifetime = 3600; // 1 hour // Display the header template. $smarty->display('header.html'); // Set another cache lifetime. $smarty->cache_lifetime = 60; // 1 minute // Display the body template. $smarty->display('body.html'); ?>

Now that we have our templates cached, we might want to invalidate the cache before it reaches its lifetime.

This is useful when you have information that does not change very often, but when it does change, it needs to be displayed as soon as possible.

Force cache regeneration:

<?php // Load the Smarty library. require_once 'Smarty.class.php'; // Instantiate a Smarty object. $smarty = new Smarty; // Use the lifetime value at the time the cache is generated. $smarty->caching = 2; // Load the Database library. require_once 'Database.php'; // If there's new data, get it. if (Database::get('hasNewNews');) {        $smarty->clear_cache('breaking_news.html');        Database::update('hasNewNews', 0);        $news = Database::get('news');        $smarty->assign('news', $news); } // Force cache regeneration. $smarty->cache_lifetime = -1; // Display the breaking news template. $smarty->display('breaking_news.html'); ?>

Smarty also provides a way of checking whether the cache is valid. This is done using the is_cached() method. This is a good way of skipping things that we don't need if the template is cached.

Prevent a database access if the template is cached as follows:

<?php // Load the Smarty library. require_once 'Smarty.class.php'; // Instantiate a Smarty object. $smarty = new Smarty; // Use the lifetime value at the time the cache is generated. $smarty->caching = 2; // Check whether the template is not cached. if (!$smarty->is_cached('latest_news.html') {        require_once 'News.php';        $news = new News;        $latestNews = $news->getLatestNews();        $smarty->assign('latestNews', $latestNwes); } // Set cache lifetime. $smarty->cache_lifetime = 60 * 5; // 5 minutes // Display the latest news template. $smarty->display('latest_news.html'); ?>

In some cases, we want to have several versions of the same template cached. This can be done using the cache_id parameter in the display(), is_cached(), and clear_cache() methods.

Here's how to cache with different versions:

<?php // Load the Smarty library. require_once 'Smarty.class.php'; // Instantiate a Smarty object. $smarty = new Smarty; // Use the lifetime value at the time the cache is generated. $smarty->caching = 2; // Assign the cache ID. $articleId = $_GET['articleId']; $cacheId = $articleId; // Check if the template is not cached. if (!$smarty->is_cached('show_article.html', $cacheId) {        require_once 'Article.php';        $article = Article::get($articleId);        $smarty->assign('article', $article); } // Set the cache lifetime. $smarty->cache_lifetime = 60 * 5; // 5 minutes // Display the article template. $smarty->display('show_article.html', $cacheId); ?>

In most cases, we want to cache only certain parts of a template. Smarty provides several ways of doing this: using the {insert} tag, the register_block() trick, and the cache control when registering plug-ins.

The {insert} tag works like {include} tag, but the included content is not cached.

With the register_block() method, you can prevent a block within the template from being cached.

The plug-in cache control is done by setting the $cache parameter to false on register_block(), register_compiler_function(), and register_function(); you can also control to which attributes the plug-ins get cached using the $cache_attrs parameter.

Following is the register block trick:

<?php // Load the Smarty library. require_once 'Smarty.class.php'; // Instantiate a Smarty object. $smarty = new Smarty; // Use the lifetime value at the time the cache is generated. $smarty->caching = 2; // The following function is never cached. function block_dynamic($param, $content, &$smarty) {        return $content; } // Register a Smarty block. $smarty register_block('dynamic', 'block_dynamic', false); // Display the time template. $smarty->display('show_time.html'); ?>

The template:

<html> <body> Current time: {dynamic}{'0':date_format:'%H:%M:%S'}{/dynamic} </body> </html>

Another performance enhancer is using HTTP headers to control cache in the browsers.

Setting the $smarty_modified_check class variable enables Smarty to handle the If-Modified-Since HTTP header. This saves you the trouble of fetching the temple from the cache file, the time the web server spends sending the content, and bandwidth.

If the majority of your content will be cached, it might help to use some sort of web cache system like Squid (http://www.squid-cache.org/) on top of the web server in conjunction with the Cache-Control and Expires HTTP headers. This saves your dynamic web server from being busy serving static content.

 

 



PHP and Smarty on Large-Scale Web Development
PHP and Smarty on Large-Scale Web Development
ISBN: 047008023X
EAN: N/A
Year: 2007
Pages: 20
BUY ON AMAZON

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net