3

I'm looking into using Joomla's caching mechanism for a plugin. However I would need to cache the plugin data for a given period of time irrespective of the site-wide cache settings.

Is it possible to override the site-wide cache duration but only for specific cached data?

Question applies to the current Joomla version (3.8.12), but also looking ahead to J4, if there are any differences there.

3

Yes, you can cache your data for your plugin, by creating a new JCache instance and setting your custom options.

Something like below would do that:

// Reading global configuration params
$conf = JFactory::getConfig();

// Setting a location for your cached data.
$cacheBase = JPATH_SITE . '/cache/yourcachelocation';

// Your custom cachegroup
$cacheGroup = 'yourCacheGroup';

// Lifetime for your cache
$lifetime = 60;

// Setting your options
$options = array(
    'defaultgroup'  => $cacheGroup,
    'storage'       => $conf->get('cache_handler', ''),
    'caching'       => true,
    'cachebase'     => $cacheBase,
    'lifetime'      => $lifetime,
);

// Instantiate your cache object
$cache = JCache::getInstance('', $options);

// Create $cacheDataId  
$cacheDataId = 'yourCacheDataId';


// It's time to check for cached data
if ($cache->get($cacheDataId) !== false )
{
    $data = $cache->get($cacheDataId); // We got data from cache
}
else
{
    // No cached data, fetch fresh data
    $data = yourFreshDataGenerationMethod();

    // Cache the data for the next time
    $cache->store($data, $cacheDataId);
}

// Do nice things with your $data

Update to include relevant info found in the comments:

  • You can use $cachebase to select a custom location for your cached data.

  • Instead of specifying that you could use the $cachegroup, which would do a similar thing.

  • What is most important is the $cacheId when you are storing the data, as you will retrieve your stored cache data with this ID.

  • Note that if you give a custom directory path ($cachebase) to store your cached data that does not exist, you will have to create that folder, otherwise it won't work, but using a Cache Group will auto-create the folders for your cache - (or at least this is how I remember it works...).

I recommend to experiment with these options and see exactly how they work.

Helpful Links:

  • Hi. Thanks for the answer. So just to be clear: setting $cachebase as a unique value (specific to my extension) is the key thing I need to do that will trigger JCache::getInstance() to give me a whole new cache that is then independent of the main cache and can have its own lifetime (and presumably other options as well)? That's easier than I was expecting. :-) Thank you. – Spudley Aug 31 '18 at 22:29
  • @Spudley You can use $cachebase to select a custom location for your cached data. Instead of specifying that you could use the $cachegroup, which would do a similar thing. What is most important is the $cacheId when you are storing the data, as you will retrieve your stored cache data with this ID. You can play around with these and see exactly how it works. I will update my answer to include how to fetch your data. – FFrewin Aug 31 '18 at 22:44
  • @Spudley - updated the answer. The above should have you up and running. – FFrewin Aug 31 '18 at 22:56
  • @Spudley - Do note that if you give a custom directory path ($cachebase) to store your cached data that does not exist, you will have to create that folder, otherwise it won't work (or so I remember...), but using a cache group will auto-create the folders for your cache. Again play with these, as I am not testing the code I gave you and can not remember exactly what's the case there. – FFrewin Aug 31 '18 at 23:04
  • Hi. Thanks for the updates, and thanks again for the answer. I'm going to play around with this for a bit as you suggest so I can get a proper handle on how it works. This answer is a great starting point, so thank you very much for taking the time to write it. (I'm sure it will prove helpful to others as well) – Spudley Aug 31 '18 at 23:30

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.