Using Joomla 1.5 to cache expensive computations

Monday, 01 June 2009
Sometimes you need to preserve some computation results for a while, and have it refresh automatically. You may use this technique for periodic RSS fetching, or a really heavy SQL query. There are many more applications, of course.

Joomla's cache system allows you to automatically cache the output and result of a function(in this example the '_fetch' static method from the 'Currency' class). Cache's purpose is to offload your servers (and remote servers if fetching remote data), and speed up page display.

The $res will return the cached function result(if fresh enough). If the result is older than 'lifetime' seconds, the cache call will execute the function again, and store the result for another 'lifetime' seconds. In other words, the cache refreshes every 'lifetime' seconds.

Take note that the class is not instantiated, we're just using it for namespace purposes. The actual call  will be the same as calling Currency::_fetch() from PHP.

The code below creates and uses a 'Currency' subfolder in Jomla's cache folder(if the cache system was configured for file storage in Joomla administration). To clear the cache, you may use Tools->Clean cache from Joomla administration.

// cache subfolder(group) 'Currency', cache method: callback
$cache= & JFactory::getCache('Currency','callback');
 
// save configured lifetime
 
@$lifetime=$cache->lifetime;
 
$cache->setLifeTime(86400/2); // check twice per day
 
// save cache conf
 
$conf =& JFactory::getConfig();
 
// check if cache is enabled in configuration
 
$cacheactive = $conf->getValue('config.caching');
 
$cache->setCaching(1); //enable caching
 
// Currency=classname, _fetch=static method. The $res will hold the cached return value
 
// if the cache expired, the method will be called again and the result will be stored for 'lifetime' seconds
 
$res = $cache->call(array('Currency','_fetch')); 
 
// revert configuration
 
$cache->setCaching($cacheactive);

Are you a Joomla/PHP/Web developer? You may want to check our list of freelance jobs websites.



Give us some social love (it really works now)!

Reddit! Del.icio.us! StumbleUpon! Yahoo! Swik!



Be first to comment this article
RSS comments

Write Comment
  • Please keep the topic of messages relevant to the subject of the article.
  • Please don't use comments to plug your web site. Links are rel='nofollow'-ed
  • Please refresh the page if you're having trouble with the security image code
Name:
E-mail
Homepage
Title:
Comment:

:) :grin ;) 8) :p
:roll :eek :upset :zzz :sigh
:? :cry :( :x
Code:* Code

Last Updated ( Thursday, 18 June 2009 )
 

Europe freelancer directory

Newsletter

Subscribe to TeachMeJoomla's newsletter
Name:
Email:


Auto tags

joomla cache

joomla caching

Joomla 1.5 Cache

joomla clean cache

joomla 1.5 caching

using joomla cache

Joomla cache lifetime

joomla clear cache

JFactory::getCache

Joomla getCache

joomla auto clean cache

joomla rss cache

newsletter joomla 1.5

joomla $cache->call

newsletter for joomla 1.5

$cache->call Joomla

joomla refresh cache

joomla cache how to

joomla cache folder

how to use joomla cache

joomla caching system

Subscribe to newsletter joomla 1.5