5

I'm updating an old bit of custom Joomla plugin, and there's a deprecated line saying JResponse::allowCache(false); -- what's the "right" way to handle this for Joomla 3.x compatibility?

Also, perhaps this could be a separate question, but is there a way to set key => value pairs manually in the Joomla cache? For example, this plugin does lookups for US states. So if it's displaying a lot of information, it has to hit the DB over and over looking for the state abbreviation, when we could simply cache this response.

2 Answers 2

5

JResponse::allowCache(false); replacement is

JFactory::getApplication()->allowCache(false);

To set key => value cache items:

// Get cache instance
$cache = JFactory::getCache();

// Store in cache
$cacheItemStored = $cache->store($value, $key, $group = null); // boolean

// Get from cache
$value = $cache->get($key, $group = null);

As you can see you can set optional $group. This is useful for cleaning all cache in specific group or not in specific group.

For example:

$cache->clean('groupName'); // will clean all cache IN "groupName" group
$cache->clean('groupName', 'notgroup'); // will clean all cache NOT IN "groupName" group
2
  • @Rene-- thanks! If you have a moment, could you explain a bit about how the cache callback type is used?
    – user101289
    Feb 26, 2016 at 7:40
  • 3
    It's actually nice feature and way longer story for comment. But in short, you can cache calls to functions or methods of classes. So if cache isn't expired, if will return cached data, therefore not hiting DB or API again. Pretty nice? :) You don't have to store it, just use get and it will store response automatically, if it dosen't exist. Example here: corephp.com/blog/implementing-joomlas-cache-callback-api
    – Rene Korss
    Feb 26, 2016 at 7:52
4

Sorry to bump this older question but the accepted response isn't totally correct. Most everything in JResponse is replaced by a similarly named method call in JApplicationWeb instances (of which the CMS web application classes are). See https://api.joomla.org/cms-3/classes/JResponse.html for more details.

So JResponse::allowCache(false); is replaced by JFactory::getApplication()->allowCache(false);.

3
  • Thanks Michael-- the Joomla docs are saying to use JApplicationWeb::allowCache(false);, though this throws a warning since you're calling a non-static method statically (found here: api.joomla.org/cms-3/deprecated.html)
    – user101289
    Mar 13, 2016 at 5:56
  • Thanks Michael for pointing this out. You are absolutely right, I somehow mixed it up with regular cache. Edited my answer accordingly.
    – Rene Korss
    Mar 13, 2016 at 14:55
  • 1
    @user101289 That's right. The documentation doesn't explicitly say how to access it, but it does at least reference the new method you should use. Generally the context is JFactory::getApplication()->allowCache() but that assumes a lot too (though in 99% of cases because Joomla and extensions don't do well with non web applications you're fine).
    – Michael
    Mar 14, 2016 at 20:53

Your Answer

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

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