Program a module to use Joomla cache

Find out how a module can leverage Joomla Cache.

Module: the view is invoked in the main controller

/modules/mod_xxx/mod_xxx.php 

passing a parameter with all the variables we wish to use as a key to the cache.

The most common error is not taking into account different module instances (symptom: two modules on the same page have the same content, only when the cache is enabled), or whether its contents are dependant on the current component being shown.

The solution is easy

Just add module->id and/or the category->id to the array of parameters:

PHP Code:
$cacheparams = new stdClass; $cacheparams->cachemode = 'safeuri'; $cacheparams->class = 'modSomethingHelper'; $cacheparams->method = 'getItem'; $cacheparams->methodparams = $params; $cacheparams->modeparams = array('id'=>'int','Itemid'=>'int','catid'=>'int', 'view');   $item = JModuleHelper::moduleCache ($module, $params, $cacheparams);    

Tags: Joomla Cache