Categories
CMS Joomla Mastering Development

What is the simplest way to clear a group’s cache in Joomla via php, when caching disabled?

So background: I’m making a caching plugin that clears our site and CDN caches when content is saved. In this instance, I want to clear the site cache for one group.

My original attempt looked something like this:

$cache = JFactory::getCache('cachegroup');
$cache->clear();

That didn’t work. I did some research and saw SharkyKZ in the Joomla forums recommend this structure:

$cache = JCache::getInstance('callback');
$cache->clean('cachegroup');

That also didn’t work for me. I suspected it had something to do with the fact that caching is disabled on our site, so I tried adding $cache->setCaching(1); to both sets of code and that didn’t work either.

Finally I dug into the Joomla code and looked at how their cache component handles it; and ended up with this working code:

$conf = JFactory::getConfig();

$options = array(
    'defaultgroup' => '',
    'storage'      => $conf->get('cache_handler', ''),
    'caching'      => true,
    'cachebase'    => $conf->get('cache_path', JPATH_SITE . '/cache')
);

$cache = JCache::getInstance('', $options);
$cache->clean('cachegroup');

So I have working code, but I would…

  1. Like to understand why it works, and
  2. Like to know if there is a simpler or superior way to accomplish this.

Leave a Reply

Your email address will not be published. Required fields are marked *