3

Is it possible to use JCache to clear the cached data for a specific plugin or all plugins? I am using a plugin to display an image and text on a page and I have to clear the cache manually for any changes to take effect.

Couldn't find any examples of this in my Google searches or the official documentation.

1 Answer 1

5

Without knowing which plugin(s) you're concerned with, here's some code that would get, set and clear a cache for a given item.

$output = '';
$cache = JFactory::getCache('com_something', '');
$cache->setCaching(true);
$cache->setLifeTime(86400);

// See All cached items
echo "<pre>".print_r($cache->getAll(), true)."</pre>";

// Create a Cache ID that works for you :
$cacheid = md5('something'.JFactory::getUser()->id);  // Just an example

// Remove cached output
//$cache->remove($cacheid);

// Get cached output
$output = $cache->get($cacheid);
if (empty($output)) {
    $output = "something I want to cache";
    $cache->store($output, $cacheid);
}

Using the docs at JCache, and echoing the current catched items with $cache->getAll() will look something like this to identify which item(s) you're interested in:

Array
(
    [com_something] => JCacheStorageHelper Object
        (
            [group] => com_something
            [size] => 0.09
            [count] => 2
        )

    [Gantry] => JCacheStorageHelper Object
        (
            [group] => Gantry
            [size] => 332.58
            [count] => 5
        )

    [GantryLess] => JCacheStorageHelper Object
        (
            [group] => GantryLess
            [size] => 354.04
            [count] => 9
        )

    [myplugin] => JCacheStorageHelper Object
        (
            [group] => myplugin
            [size] => 0.09
            [count] => 2
        )

)
3
  • Thanks for the info! I tried to output $cache->getAll() in the way you described and nothing was output, although when I go to Site->Maintenance->Clear Cache, I can see the plugin data in the cache list. Thoughts?
    – Eric
    Nov 9, 2014 at 15:48
  • Not sure what to tell you...just tried it on 4 of our sites without issue...perhaps check the code that you tried from the answer, or perhaps some timing issue with the plugin...sorry, can't replicate to explain it.
    – GDP
    Nov 9, 2014 at 16:17
  • 1
    @Eric, the code above uses com_something as an example so make sure you change this value to the component that applies to you ;)
    – Lodder
    Nov 9, 2014 at 17:40

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.