Joomla 1.5 cache problem fix

Posted by ivantheTerrible
ivantheTerrible has not set their biography yet
User is currently offline
on Friday, 10 October 2008
in Digital Blogs

During my development of a Joomla 1.5 component i had lots of trouble with the implementation of the cache library. As i couldn't find a solution on the forums i produced one myself (and necessarily fixed a bug along the way).

So, in open source spirit, here it is.

Firstly, we'll fix a bug around the cache lifetime, and then we'll show the implementation. Bug is that changing the cache lifetime with the setLifeTime method doesn't work. This is because the lifetime is changed in the cache handler but the lifetime is checked in the cache storage handler. i.e. the update is not transferred to where it is needed. In /libraries/joomla/cache/cache.php (line 173) change

function setLifeTime($lt)
     $this->_options['lifetime'] = $lt;


function setLifeTime($lt)
     $this->_options['lifetime'] = $lt;
     $storage =& $this->_getStorage();

Then in /libraries/joomla/cache/storage.php change the __construct function to (not 100% necessary but better)

function __construct( $options = array() )
     $this->_application = (isset($options['application'])) ? $options['application'] : null;
     $this->_language = (isset($options['language'])) ? $options['language'] : 'en-GB';
     $this->_locking = (isset($options['locking'])) ? $options['locking'] : true;
     $this->_now = time();
     $this->setLifeTime((isset($options['lifetime'])) ? $options['lifetime'] : null  );

And finally (in the same file) add the setLifeTime method

function setLifeTime($lt)
     $this->_lifetime = $lt;
     // Set time threshold value
     if (is_null($this->_lifetime)) {
          $this->_threshold = 0;
     } else {
          $this->_threshold = $this->_now - $this->_lifetime;

And there it is. Then the framework can be implemented in your module/component etc. like this

$conf =& JFactory::getConfig();
$options = array( 'defaultgroup'    => 'mygroup',
     'cachebase'    => $conf->getValue('config.cache_path'),
     'lifetime'       => $conf->getValue('config.cachetime') * 60, // minutes to seconds
     'language'       => $conf->getValue('config.language'),
     'storage'      => 'file');      '$cache =& JCache::getInstance( 'callback' , $options );
$cache->setLifeTime(15 * 60); // 15 minutes to seconds
$cache->get('myfunction', '');
function myfunction()
// query db
// display results or whatever

Substitute mygroup with your own indentifier and myfunction with your function (MyClass::MyMethod() also works).

Hope it helps out. Will post on after my initial punt for MyDL.

Happy coding

Tags: Untagged


Please login first in order for you to submit comments