Tutorials > PHP - Caching in Smarty
PHP - Caching in Smarty
  Forward   PHP/MySQL web developer Tutorials  to Friends and relatives Forward
Caching is a way of improving a Web page's performance by saving the executed version of a page as a static file. This allows the server to handle multiple requests for the same page with only one execution of the underlying PHP script. Caching can be applied to any type of content provided to the browser by the server but it makes particular sense to use it with templated pages. Smarty caching refers to a built-in caching that's easy to use.
 

Setting up cache in Smarty

 
Enable caching in smarty

First thing you have to do, for setting up caching is by setting $caching = 1 or to 2.
 

Example:

<?php

require('Smarty.class.php');

$smarty = new Smarty;

$smarty->caching = 1;

$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.
 

Adjusting the cache expiration or lifetime
 

Each cached page has a limited lifetime determined by $cache_lifetime. The default value is 3600 seconds i.e. an hour. After that time expires, the cache is regenerated. It is possible to give individual caches their own expiration time by setting $caching=2. In below example we set $cache_lifetime per cache.
 

Example:

<?php

require('Smarty.class.php');

$smarty = new Smarty;

$smarty->caching = 2; // lifetime is per cache

// set the cache_lifetime for index.tpl to 5 minutes

$smarty->cache_lifetime = 300;

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

// set the cache_lifetime for home.tpl to 1 hour

$smarty->cache_lifetime = 3600;

$smarty->display('home.tpl');
 
// NOTE: the following $cache_lifetime setting will not work when $caching = 2.

// 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->cache_lifetime = 30; // 30 seconds

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

?>
 

Checking the Cache

 
The is_cached() 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. Below example shows how to use the is_cached() function.
 

Example:

<?php

require('Smarty.class.php');

$smarty = new Smarty;

$smarty->caching = 1;

if(!$smarty->is_cached('index.tpl')) {

    // No cache available, do variable assignments here.

    $contents = get_database_contents();

    $smarty->assign($contents);
}

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

?>
<?php

require('Smarty.class.php');

$smarty = new Smarty;

$smarty->caching = 1;

if(!$smarty->is_cached('index.tpl')) {

    // No cache available, do variable assignments here.

    $contents = get_database_contents();

    $smarty->assign($contents);

}

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

?>

 
Clearing the Cache

You can clear all the cache files with the clear_all_cache()  function, or individual cache files and groups  with the clear_cache() function. Example below shows how to clear the cache.
 

Example:

<?php

require('Smarty.class.php');

$smarty = new Smarty;

$smarty->caching = 1;

// clear only cache for index.tpl

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

// clear out all cache files

$smarty->clear_all_cache();


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

?>


Create multiple caches per page

You can have multiple cache files for a single call to display() or fetch(). Sometimes it’s necessary to maintain multiple caches for a single file. Let’s say file where, page 1 content will be different from page 2 content and so on. In this case, it will be necessary to maintain multiple cache files. You can do this by passing a $cache_id as the second parameter to the function call to display () or fetch ()


Example:

<?php

require('Smarty.class.php');

$smarty = new Smarty;

$smarty->caching = 1;

$my_cache_id = $_GET['article_id'];

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

?>

Above, we are passing the variable $my_cache_id to display() as the $cache_id. For each unique value of $my_cache_id, a separate cache will be generated for index.tpl. In this example, article_id was passed in the URL and is used as the $cache_id.

 
Note :  Be very cautious when passing values from a client (web browser) into Smarty or any PHP application. Although the above example of using the article_id from the URL looks handy, it could have bad consequences. The $cache_id is used to create a directory on the file system, so if the user decided to pass an extremely large value for article_id, or write a script that sends random article_id's at a rapid pace, this could possibly cause problems at the server level. Be sure to sanitize any data passed in before using it. In this instance, maybe you know the article_id has a length of ten characters and is made up of alpha-numerics only, and must be a valid article_id in the database. Check for this!

Passing a cache_id to is_cached()

 
Example:

<?php

require('Smarty.class.php');

$smarty = new Smarty;


$smarty->caching = 1;// enable caching

$my_cache_id = $_GET['article_id'];

if(!$smarty->is_cached('index.tpl',$my_cache_id)) {

    // No cache available, do variable assignments here.

    $contents = get_database_contents();

    $smarty->assign($contents);

}

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

?>

 
You can clear all caches for a particular $cache_id by passing NULL as the first parameter to clear_cache(). Example below shows how to use this function.

 
Example:

<?php

require('Smarty.class.php');

$smarty = new Smarty;

$smarty->caching = 1; //enable caching

// clear all caches with "sports" as the $cache_id

$smarty->clear_cache(null,'sports');

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

?>

 
In the above example, we clear all the caches with “sports” as the cacheid.
Join a Course Now

Contact counselors

Call us now:
+91-9886096424
080-46441086

Email us:

university@siliconindia.com