4

I have a custom component I've built, and it has a search filter toolbar to filter results. The problem is that when Joomla caching is turned on, the filters get cached along with the page data-- so you submit the form to set new filters, and it simply returns the cached page, pre-filter.

How do you set a no-cache header or otherwise force Joomla to ignore the cached version of a view when you pass new parameters?

EDIT TO ADD:

The behavior happens with both Conservative or Progressive cache, happens with both File and Memcached.

3
+25

You didn't state what cache settings are producing this problem. Joomla has several cache levels https://docs.joomla.org/Cache.

The most aggressive caching option is Page Caching (turned on via the cache system plugin). This will pick out a cached page based on the URI BEFORE your component is even run. Here I think your only option would be to set your form's method to "get", this will pass the form field's data as part of the querystring, so each unique search should generate a unique cached page. This also has the advantage of allowing users to press the browser's back button without the browser asking if they want to repost the form's data. However, the downside is that your page URL's might become less readable as the querystring can quickly become very long depending on the number of filters you have.

If you aren't using the system plugin, but are instead activating the cache via the site's global config then this is called 'view' caching, and can be handled by your component's controller.

In this case you will need to build a cacheid from the posted form data to ensure that a unique cached page is created per set of filters:

// Create the view
$view = $this->getView('myView', 'html');

// Build a cache id which will be unique per search: 
$user = JFactory::getUser();
$uri = JURI::getInstance();
$input = JFactory::getApplication()->input;
$cacheId = serialize(array($uri, $input->post, $user->get('id')));

// Create the cache
$cache = JFactory::getCache('com_mycomponent', 'view');
$cache->get($view, 'display', $cacheId);

I've cut out and condenced the above code from Fabrik's list controller https://github.com/Fabrik/fabrik/blob/joomla3/components/com_fabrik/controllers/list.php which has to deal with a similar issue.

  • Thanks Rob. However, using this code throws a 500 error: Unable to load Cache Controller: myviewname – user101289 Apr 3 '15 at 15:19
  • did you edit 'myView' in line 1 to the correct view name, and 'com_mycomponent' in the second last line? – Rob Clayburn Apr 8 '15 at 9:41
  • How would you do this for Joomla modules? – Olle Härstedt Mar 23 '17 at 11:06
  • It seems that when including jumi code using the jumi module, that code is not cached. – Olle Härstedt Mar 23 '17 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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