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->call

$cache->setCaching joomla

displaying remote RSS feeds joomla 1.5

joomla jfactory cache clean

joomla 1.5 cachowanie

using cache function joomla 1.5

?cachecleaner= joomla query

joomla cache->call

how to refresh cache in joomla

joomla rss refresh

joomla and cache 2009

clean cache rss joomla

joomla code cache

JFactory::getConfig() expensive?

Newsletter without refresh page for joomla 1.5

joomla cache function results

dont clear cache joomla

joomla JFactory::getCache

joomla 1.5 clean cache

using cache in joomla

abilita cache in joomla feed

joomla cache->setCaching