Implementing Joomla’s Cache Callback API

It has been a mighty busy few weeks here at ‘corePHP’. We are moving forward with several new projects that you will soon be hearing a lot about.

JAutosearch is one such project that may revolutionize the way visitors utilize your website’s search functionality; about as much as I can say about JAutosearch at this time, although it will likely be released by the time you read this (or very nearly!).

Ok lets get down to some Joomla! extension tips!

While working on JAutosearch (as well as many other projects) I have found that understanding Joomla’s caching system is very much a requirement if you want to design responsive, remote type systems for Joomla! It is important to  cache remote data without the visitor having to wait for it to load every single page view – even more so many remote services frown upon “really realtime” connections and could ban ya! Fearmongering aside, everyone should be able to see the advantages in caching and the freeing up of server resources it entails.

Today I would like to describe an especially useful caching method available to Joomla’s Framework API, this being the Cache Callback Method whereby you can cache the output from a procedural function or class method. Directions are commented to the code below.

//you may need to import the required libraries if they
//are not already available
jimport('joomla.cache.cache');
jimport('joomla.cache.callback');

$cache = JCache::getInstance( 'callback', array(
	//this will be the folder name of your cache
	//inside the "cachebase" folder
	'defaultgroup' => 'my_cache', ///
	'cachebase' => JPATH_BASE . '/cache/',
	//how long to store the data in cache (seconds
	'lifetime' => ( 5*60*60 ), // hours to seconds
	'language' => 'en-GB',
	'storage' => 'file'
));

//you can turn the caching on and off here handy for param control
$cache->setCaching( true );

//your cache id must be unique to what you are caching
//if something is pretty dynamic you can use what ever vars are constructing your method
$cache_id = md5( JRequest::getVar( 'option' ) . JRequest::getVar( 'view' ) .  JRequest::getVar( 'id' ) );			

//there are two ways to call the actual data depending on whether you are
//caching a method of a class or just a plain old function.

$CacheClass = new CacheClass;

//define time so we can check the time of caching
$time = date( 'r', time() );

//now we get our data - if the cache hasn't expired the cache contents will be supplied or the
//output of the function if it has (then cache will be re-populated)

//this is the way to cache a class method - note the first argument uses php's func_get_args function
//the way you populate the first argument depends on what type of function you are calling
//first we call the "cache_method" method of the $CacheClass class we defined earlier
$data = $cache->get( array( $CacheClass, 'cache_method' ), array( $time, $cache_id ), $cache_id );

//now we call a procedural type function
$data = $cache->get( 'cache_function', array( $time, $cache_id ), $cache_id );

echo $data;

//below are the test function and class used

function cache_function( $time, $cache_id ){
	return "CacheId: $cache_id Time: $time";
}

class CacheClass{
	function cache_method( $time, $cache_id ){
		return "CacheId: $cache_id Time: $time";
	}
}

Disclaimer: I wrote this on the fly without testing so if there are any bugs.. well let me know! You should get the concept from what is there anyway

Happy Caching!

Adam Stephen Docherty
Senior Developer – ‘corePHP’

The Core Team
Editorial Staff Members at 'corePHP'
Editorial staff for the Core Technology Blog for 'corePHP' - news, views insights and advice for e-commerce, marketing technology , web design and development.

2 thoughts on “Implementing Joomla’s Cache Callback API

  1. pokoje http://www.msarbinowo.pl

    Thanks for the post, is there any way I can receive an update sent in an email every time you write a new update?

Comments are closed.