Caching Smarty Database Templates

The Smarty manual explains how to use Templates from other sources such as databases. It doesn’t however give an example of caching templates from these sources. I ran into a spot of trouble when I tried to be clever about checking the freshness of a db based template.
I didn’t want to interogate the db on every request just to check if a template is stale or not. Why have caching when you have to hit the db anyway? The db_get_timestamp() function has to be modified to check the freshness in some other way then.
I used a 0-byte file in a web-server writeable directory. Unfortunately it didn’t work. The is_cached() function would return false and I’d assign Smarty variables to prepare a new version of the web page, but when I called the display() function it displayed the last cached version!
It took me a while to figure out that both is_cached() and display() call the db_get_timestamp() function. As the file I used to flag a refresh was deleted on the first run I had to retain the timestamp somehow. That’s where the global $filetimestamp array comes in.

  1. is_cached() checks the freshness of the cache and returns false.
  2. I prepare the new Smarty variables.
  3. The page is displayed using the Smarty display() function
  4. Use touch scratch/index.tpl to update the template cache.
function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
{
  global $filetimestamp;

  if( is_file( 'scratch/' . $tpl_name ) == false )
  {
    if( $filetimestamp[ $tpl_name ] != '' )
    {
      $tpl_timestamp = $filetimestamp[ $tpl_name ];
    }
    else
    {
      $tpl_timestamp = mktime (0,0,0,date("m")-1,date("d"), date("Y"));
    }
    return true;
  }
  else
  {
    $tpl_timestamp = filemtime( 'scratch/' . $tpl_name );
    $filetimestamp[ $tpl_name ] = $tpl_timestamp;
    unlink( 'scratch/' . $tpl_name );
  }
  return true;
}
if( $smarty->is_cached( 'db:index.tpl' ) == false )
{
  print "regenerating cache!<br>";
  $smarty->assign( 'date', time() );
}

$smarty->display("db:index.tpl");


You might also like

If you like this post then please subscribe to my full RSS feed. You can also click here to subscribe by email. There are also my fabulous photos to explore too!

5 thoughts on “Caching Smarty Database Templates


  1. Thanks for sharing the code.

    When I loads the page for the first time it takes data from table & display also create a cache file and the second time when i refresh the page also it goes fine and picks the cache file & display it.

    But when I do change in the table where original template stores and then I refresh the page it doesn’t takes the updated data & display the old cache data.

    May be you are not getting this type of problem, if yes then plz let me how you did it !

    Jimmy


  2. Yeah, I did as you said & it works.

    But can please tell me as your saying as soon as I update the database I also again created scratch/index.tpl file (empty file) and Smarty update the cache with with updated data of table. But How this happens ?

    After creating scratch/index.tpl file (empty file) how Smarty comes to know that it should call the function db_get_template($tpl_name, &$tpl_source, &$smarty_obj) because database has updated ?


  3. I request you while giving answer to above Question please also answer this:

    If Smarty updates the cache file as soon as we create scratch/index.tpl file (empty file) when the database updated then I think there is no use of below IF Construct because the program also runs fine after commenting the below IF construct. Except this it displays message: regenerating cache

    Also if according to the default $cache_lifetime value(3600 or 1 hr) the cache expires then below code runs but except generating cache with displaying ‘regenerating cache’ & date it doesn’t update the cache with latest database info.

    Am I right ?

    if( $smarty->is_cached(‘db:index.tpl’) == false )
    {

    print ‘regenerating cache!’;
    $smarty->assign( ‘myDate’, time() );
    }

    I’ll greatful to you !

Leave a Reply

Loading Facebook Comments ...