Jump to content

How to use the Smarty cache, my first attempt


Recommended Posts

 

You may choose what template you want to cache by setting the caching control to 2 and the force_compile to false in the config.inc file.

 

Now go to the top of the display function (classes/Module.php file, line 500) and replace the first lines with this :

 

 

	public static function display($file, $template,$cache_lifetime = 0)
{
	global $smarty;

	$smarty->cache_lifetime = $cache_lifetime;

 

 

To finish, edit the block you want to cache... /modules/blocktags/blocktags.php for example. Replace the entire hookLeftColumn function (from line 32 to line 55) with this :

 

 

	function hookLeftColumn($params)
{
	global $smarty;
	if(!$smarty->is_cached(dirname(__FILE__).'/blocktags.tpl')) {

		$tags = Tag::getMainTags(intval($params['cookie']->id_lang),50);

		if (!sizeof($tags))
			return '';
		$maxFontSize = 18;
		$minFontSize = 10;
		$maxNumber = intval($tags[0]['times']);
		$classPrefix = 'tag_level';
		for ($i = 0; $i < sizeof($tags); ++$i)
		{
			$tags[$i]['fontSize'] = floor(($maxFontSize * $tags[$i]['times']) / $maxNumber);
			if ($tags[$i]['fontSize'] < $minFontSize)
				$tags[$i]['fontSize'] = $minFontSize;
			// 2nd version: use CSS class
			$tags[$i]['class'] = $classPrefix.$tags[$i]['times'];
			if ($tags[$i]['times'] > 3)
				$tags[$i]['class'] = $classPrefix;
		}
		$smarty->assign('tags', $tags);
	}
	return $this->display(__FILE__, 'blocktags.tpl',180);
}

 

180 represents the number of seconds the block will be cached...

 

Well, you can now insert some "$smarty->is_cached" tests is your modules (the ones with db calls)...

 

Yes, it is faster !

 

Now, note that it is not really a good idea to cache a products list because of stock refresh...

 

Have fun :)

 

Link to comment
Share on other sites

  • 3 months later...
×
×
  • Create New...