Skip to content
This repository was archived by the owner on Aug 31, 2025. It is now read-only.

The CacheCleaner

Nicholas K. Dionysopoulos edited this page Mar 31, 2015 · 1 revision

CacheCleaner

You have probably heard of Joomla!'s caching system. Conventional wisdom states that it is a mechanism to hold pre-rendered content (usually HTML) and serve it unchanged, without trying to render it again, inside a specified time limit since it was originally created. You'd be forgiven for thinking that this is the only functionality of the Joomla! caching system but, interestingly, it is not.

The Joomla! caching system can be used to store any kind of information, not just pre-rendered content. It is used internally to hold the result of the complex queries used to determine which modules and plugins are activated and need to be loaded at any given time. This has the immediate effect that after change to a plugin's or module's settings directly through the database will have no effect unless the cache expires or you manually clean it.

This is where the CacheCleaner utility class fits into the picture. It lets you purge specific sections of the cache quickly, without going through Joomla! JCache –or whichever method is the designer's favourite this season– to perform this task.

Purging the cache

The helper class has four static methods.

FOF30\Utils\CacheCleaner::clearPluginsAndModulesCache() clears both the plugins and the modules cache in the front-end and the back-end of the site. Remember that Joomla! has distinct caches for the front-end and back-end applications.

The clearPluginsCache and clearModulesCache methods will only clean the plugins or the modules cache respectively. Calling them clears the respective cache in both the front-end and back-end of the site.

The clearCacheGroups method is the most useful one. The first argument to it, $clearGroups, is an array of all cache groups you want to clear. This can be your component (e.g. com_foobar), another component, a custom name, you name it.

How do I set and get items from the cache?

You can add items to the cache using Joomla!'s own JCache API:

$cache = \JFactory::getCache('com_something', '');
$cache->store($dataToStore, $cacheKey, 'com_something');

Where:

  • com_something is the name of your component or, in general, the group name you would like to use. It is best to use your component's name
  • $dataToStore is the data you want to store. It's supposed to be a string or an object with a magic __toString method such as JRegistry
  • $cacheKey is a unique label for the value you're storing. This is the key with which you will be retrieving it.

Getting items from the cache is also performed through Joomla!'s JCache API:

$cache = \JFactory::getCache('com_something', '');
$storedData = $cache->get($cacheKey, 'com_something');

Where:

  • com_something is the name of your component or, in general, the group name you would like to use. It is best to use your component's name
  • $cacheKey is the unique label for the value you're retrieving.

Why would I need to use the cache?

Every time you have a fairly long operation which yields the same results, or when up-to-date results aren't exceedingly important, you should use caching to improve the performance of your applications. For example, collecting download statistics from a very big downloads tracking table tends to take up to several seconds. However, you don't need to get statistics with an accuracy down to the second. 15 minutes old data would do just fine. It's therefore prudent to try to get the data from the cache first, generating afresh (and caching it!) only if the cache yields no result (meaning it has either expired or has not been populated).

Remember, though, that just because you can doesn't mean you should. Before employing cache you should think hard about the impact of your choice in the regular use case of your application. For example, if you don't take care to purge the cached version of an item after editing it you will confuse your users who might wonder why is the old version still displayed. If clearing the cache per edited item is not feasible (just like in com_content, where it's not easy to tell which pages an article apprears) is this confusion scenario acceptable? Think before you code. It's much easier dealing with bugs and user experience issues before writing the code.

Clone this wiki locally