13

As is typically the case with a CMS, the most hits occur while it is still "new". I'd like to cache the page output the first time it is rendered for a given period as to reduce the heavy workload that is required to generate it.

I've been working from the JCache documentation, and have the basic mechanics worked out as shown here:

$cache = JFactory::getCache('MyCache', '');
$cache->setCaching(true);
$cache->setLifeTime(86400);  //24 hours
$cache_id = 'MyCache_page_123';
$cached_page= $cache->get($cache_id);
if (!empty($cached_page)) {
    $the_page_output = $cached_page;
}else{
    $the_page_output = ...<div>the generated view HTML</div>....
    $cache->store($the_page_output, $cache_id);
}
// echo or return "$the_page_output"

I'm stuck trying to determine where I should create the cache, then the "sweet spot" to use that cache before all the work begins again.

1 Answer 1

10

So far Google-searches make me presume that joomla caches your component automatically if you add display(true,...) to your controller's display method as in following example.
The example also includes some measurement code to check if it's working (JProfiler part).

public function display($cachable = false, $urlparams = array()) {
    $profiler = new JProfiler();//debug

    //Joomla cache only takes format, option, view, layout, tpl, language en id als default cache_id ($urlparams)
    $input  = new Jinput;
    $urlparams['comp_page_specific_id']=$input->getUInt('comp_specific_id');
    $urlparams['comp_page_specific_else']=$input->getUInt('comp_page_specific_else');

    parent::display(true, $urlparams);
    JFactory::getApplication()->enqueueMessage($profiler->mark( ' seconds with caching<br>Only works after someone has visited this page at least once.<br>Can be reset in backend.' ));//debug
}
  • You have to manually set the $urlparams which define which component views need a different cache (urlparams = cache_id so to speak). And Joomla already takes care of common parameters such as format, option, view, layout, tpl, language en id.

  • Also be aware that no updates will be done in your component. If you have a hit counter in your model, the number of hits will stay constant when a cached view is shown.

*Extra info: In joomla library's controller, it's the following line that calls the view cache and at the same time sets the cache if it wasn't yet:
$cache->get($view, 'display'); //689: legacy controller

(EDIT 2015-01-14: Added urlparams)
(EDIT 2015-01-15: Added code extra info)

3
  • but I can't find any core joomla code where the cache is being set. Just a note - it is inside $cache->get method Commented Jan 14, 2015 at 14:24
  • Wow! That's very clever coding. Thanks for the info. I'll update the answer.
    – e-motiv
    Commented Jan 15, 2015 at 13:14
  • Just to be sure the answer is full, here it is Commented Jan 15, 2015 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.