Caching in Joomla 1.5 with Fault Tolerance

If you've used Joomla before, you may be aware that there are built-in caching features which you can activate to reduce the number of expensive database calls needed to render a page. This feature is also available to your own custom code, whether that be in the form of components, modules, or plugins.
The actual mileage for this feature may vary due to the speed of the server and what Joomla decides to cache. Personally I think Joomla's use of caching, specifically how they use it for articles, leaves something to be desired. The downsides include content that doesn't get flushed out once it has changed or content that has too short of a lifetime to prove useful. Because of this, I don't think lot of developers realize that you can use it to cache your own components, plugins, and modules reliably and without affecting the user experience in a negative way, all the while in a modest environment (i.e. a server environment that doesn't have special software like memcached installed.)
You might consider implementing a little caching if:
- Your code is heavy and slow (Don't take it personally, mine is too sometimes)
- Your code is high-latency requiring network resources that may or may not actually be available
- You have a crazy number of visitors and your database is becoming a significant bottleneck
In my own recent project I was able to improve my access times of some SOAP requests by 500% (~1 second/request down to ~200ms/request).
Let's get our bearings

Here's a basic example of some code which initializes a JCache object and get's some data using it. You'll notice that there are two functions, one which just generically gets some data getData and another function getDataCached which instantiates the cache and calls getData. We need to do this so that our cache is aware of what the inputs and outputs are, that way it can do a lookup on the cache and return data without ever touching getData.
com_cachetest/cachetest.php:
class CacheTest {
function getData($url) {
// ... Get some data
// in this case, I'm just returning some dummy data instead of
// downloading a web page which if we plan for the worst case scenario,
// might not be available.
$data = $url;
// send data back
return $data;
}
function getDataCached($url) {
$group = "cachetest";
$function = array("CacheTest", "getData");
// get a cache object and name it something useful
$cache =& JFactory::getCache( $group );
$cache->setCaching( 1 ); // enable caching (1 = enabled, 0 = disabled)
// call our function "getData" through the cache
$result = $cache->call( $function , $url);
return $result;
}
}
$data = CacheTest::getDataCached( "http://example.com" );
var_dump($data); // to see what we actually get
Fault Tolerant Caching
One issue in the above example is that if getData returns some bogus data, it's going to be stuck in the cache and you won't realize it until it's too late. This isn't a likely scenario for a quick database call but if you have a network request that is waiting and waiting and eventually reaches timeout, the cached data will possibly be empty. This can be problematic but it's also easily avoided. In the following example we do a quick check to see if the data is invalid, remove the old data and get a fresh copy.
com_cachetest/cachetest.php:
class CacheTest {
function getData($url) {
// ... Get some data
// in this case, I'm just returning some dummy data instead of
// downloading a web page which if we plan for the worst case scenario,
// might not be available.
$data = $url;
// send data back
return $data;
}
function getDataCached($url) {
$group = "cachetest";
$function = array("CacheTest", "getData");
// get a cache object and name it something useful
$cache =& JFactory::getCache( $group );
$cache->setCaching( 1 ); // enable caching (1 = enabled, 0 = disabled)
// call our function "getData" through the cache
$result = $cache->call( $function, $url);
// Check if we get sane data. Depending on your data this
// could be more than just an empty check
if ( empty( $result ) ) {
// find the id to remove data for
$id = $cache->_makeId( $function, $url );
// invalidate this entry in the cache
$cache->remove( $id, $group );
// call our function again knowing the cache has been fixed
$result = $cache->call( $function, $url );
}
return $result;
}
}
$data = CacheTest::getDataCached( "http://example.com" );
var_dump($data); // to see what we actually get
In Closing
Joomla's caching API is simple and, by default, just writes these cache files to disk. Although not the fastest approach the default options are really handy for a less-than-stellar server setup. These caching options are available to even shared hosts. At the same time, I should mention that Joomla has done a fantastic job of integrating support for more advanced caching systems like memcache and xdebug so that the above code can be run even faster without any modifications.
joomla-caching-example.git (Programmatic Caching in Joomla 1.5 with Fault Tolerance)
Downloads: 159 File Size: 0.0 KB
Related Posts
-
Thomas Lange
-
Thomas Lange
-
http://garrettbluma.com Garrett Bluma
-
http://garrettbluma.com Garrett Bluma
-
http://garrettbluma.com Garrett Bluma
-
http://garrettbluma.com Garrett Bluma
-
Thomas Lange
-
Thomas Lange
-
http://garrettbluma.com Garrett Bluma
-
skid
-
http://garrettbluma.com Garrett Bluma
-
Guest
-
http://garrettbluma.com Garrett Bluma
-
Guest
-
Guest
-
Guest