In several previous tips(like Turn caching on!, Caching reloaded, Give a boost to your Joomla site and couple of others) I urged you to turn caching on wherever is appropriate. Recently one of my readers had criticized me, because he tried to follow the tip, but on several modules he used simply he didn't found the option to turn cache on (or off) in module settings.

Indeed, this might be a problem, since some modules does not have this option! this little detail don't need to stop you to do so!

Let's make our hans dirty with some code! Beware, we are talking about hacking some code, so, seat your belts, make backups! And remember - we don't guarantee anything, but some fun!

If you take a look to the Joomla module structure, you can see, that each module, regardless the Joomla version, have at least 2 files in their directory, a similarly named PHP and XML file, like:

mymodule.php
mymodule.xml

Generally, the .php file contains the code executed by the module, and the .xml file is used to store the definition and parameters using during the install and setup of that particular module. We will hack the XML file, to add the caching control to the module.

The code used in Mambo, Joomla 1.0 and 1.5 is the same, since the arrival of Joomla 1.6 the syntax slightly changed, but the logic is the same. We will add couple of lines to the XML file, and for correctly add them you need to have only minimal knowledge about the structure. There is a standard parameter called "cache" available of all these versions of Joomla family which is telling to core Joomla engine whether to cache the module or not. We will add the code needed to have the option available in the Module Admin interface.

Just to recall, why is might help your site to become faster? What does exactly happens when Joomla cache the module? For example, we have a module to display the latest 10 articles in your shopping component. If the developer is good, the module might have executing one single, well optimized query against the database, but if he's not paying attention to optimization, there might be as much as 10 or more queries to display one single module. You might happen to have a commercial component written that stupid way... but anyway, most of your modules (with some notable exceptions, as a shopping cart module) are pretty well working if they are showing cached content, reducing seriously the load on your server and page execution time alike.

So when Joomla cache this module, it means the module will only execute the query once when it initially loads the page that displays the module. When a second visitor visits that page, all it's doing is reading the cache (it's a file from your cache directory) from your server instead of executing those queries again and again. There are no query calls and thus improves your website performance. It will only execute the query again when the cache expires. You can set this expiration time in the Site Global Configuration.

But let's make long story short, and see the code!

For Mambo/Joomla versions up to 1.5.*

Open the mymodule.xml file with your favorite code editor, and locate the <params> section:

<params>
    .
    .
    .
</params>

Insert the following lines right before the closing </params> tag, to be sure you don't destroy the special formatting of the XML file

<params>
    .
    .
    .
    <param name="cache" type="radio" default="0" label="Enable Cache" description="Select whether to cache the content of this module">
    <option value="0">No</option>
    <option value="1">Yes</option>
    </param>
</params>

For Joomla versions 1.6+ (including Joomla 1.7, Joomla 2.5 and the upcoming Joomla 3.*

Open the mymodule.xml file with your favorite code editor, and locate the <config> section

<config>
  <fields name="params">
    <fieldset name="basic">
    .
    .
    .
    .
    </fieldset>
  </fields>
</config>

Here you have more options, you can set individually the caching time at module level for example. Once again, to be easier to locate a proper location for the code, locate the last occurance of the closing </fieldset> tag and insert your code there:

<config>
  <fields name="params">
    <fieldset name="basic">
    .
    .
    .
    .
                <field name="cache" type="list" default="1" label="Caching" description="Description of caching">
			<option value="1">Use global settings</option>
			<option value="0">No caching</option>
		</field>
		<field name="cache_time" type="text" default="900" label="Caching time" description="Caching time description" />
		<field name="cachemode" type="hidden" default="itemid">
			<option value="itemid"></option>
		</field>
    </fieldset>
  </fields>
</config>

Save the files, and check your module. Now in module administrator your new options should be shown and also should be working! You have nothing to do, just use them and enjoy the gained performance boost. Of course, you can (and you should) add the strings above as a translateable Joomla strings in the module's language file, but that is another story. The hack works withouth that too.

0
0
0
s2sdefault
Category: Module crafting