-
Notifications
You must be signed in to change notification settings - Fork 0
The 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.
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.
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_somethingis 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 -
$dataToStoreis the data you want to store. It's supposed to be a string or an object with a magic__toStringmethod such asJRegistry -
$cacheKeyis 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_somethingis 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 -
$cacheKeyis the unique label for the value you're retrieving.
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.
FOF (Framework on Framework) and its documentation are Copyright © 2010-2020 Nicholas K. Dionysopoulos / Akeeba Ltd.
FOF is Open Source Software, distributed under the GNU General Public License, version 2 of the license, or (at your option) any later version.
The FOF Wiki content is provided under the GNU Free Documentation License, version 1.3 of the license, or (at your option) any later version.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license can be found on the GNU site.