So background: I'm making a caching plugin that clears our site and CDN caches when content is saved in the administrator backend. 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->clean();
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...
- Like to understand why it works, and
- Like to know if there is a simpler or superior way to accomplish this.
Update
This problem doesn't seem to be reproducible within a site template, but I still have the problem within a plugin. So I'm going to give an expanded example of the code structure to help spot problems.
<?php
// No direct access.
defined('_JEXEC') or die;
class plgContentFncaching extends JPlugin
{
function onContentAfterSave($context, $article, $isNew) {
// Banner items
if($context == "com_banners.banner") {
// Does not work
plgContentFNCaching::problemClearBannerCache();
// Works
plgContentFNCaching::workingClearBannerCache();
}
}
function problemClearBannerCache() {
$cache = JFactory::getCache('pinnedbanners');
$cache->setCaching(1);
$cache->clean();
}
function workingClearBannerCache() {
$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('pinnedbanners');
}
}
true
. Your question is ambiguous this way.clean()
method returnstrue
.setCaching(1)
.