06 October 2015

Add Cache Param in Your Custom Modules

Written by Valentín García, Posted in Advanced,

Add Cache Param in Your Custom Modules

After a few months of being quiet, I'm back to bring new posts. This time I will share a short tutorial to help you to add cache support for custom modules.

Cache reduces server load and is a good practice, specially for sites that have a huge amount of visits. In Joomla you can add this feature into your custom module with a few lines of code.

Edit the XML file

Edit mod_yourmodule.xml with your favorite code editor. This file is located inside your module’s folder: modules/mod_yourmodule/

Look for the "cache" setting. If that code doesn’t exist, it means your module still doesn't support cache.

Add the code below inside the advanced fieldset:

<field
      name="cache"
      type="list"
      default="1"
      label="COM_MODULES_FIELD_CACHING_LABEL"
      description="COM_MODULES_FIELD_CACHING_DESC">
      <option
             value="1">JGLOBAL_USE_GLOBAL</option>
      <option
             value="0">COM_MODULES_FIELD_VALUE_NOCACHING</option>
</field>

4

If the advanced fieldset doesn’t exist, add it as parent of the new field tag. The end result would look like:

<fieldset name="advanced">
    <field
        name="cache"
                type="list"
                default="1"
                label="COM_MODULES_FIELD_CACHING_LABEL"
                description="COM_MODULES_FIELD_CACHING_DESC">
                <option
                       value="1">JGLOBAL_USE_GLOBAL</option>
                <option
                       value="0">COM_MODULES_FIELD_VALUE_NOCACHING</option>
        </field>
</fieldset>

Test the cache param

Go to Extensions > Modules > Edit your module

In advanced tab, look for the Caching param and set "use global". Enable the module at least in one menu item.

3

Save when you're done.

Go to System > Global configuration > System > Cache, choose ON (conservative or progressive)

1

Visit one of the pages that render the module in frontend to double check your module is being displayed in fact.

As last step, go to your administrator area: System > Clear cache. Look for your module name to confirm the new param is working.

2

About the Author

Valentín García

Valentín García

Web developer and designer that love building sites with Joomla. Currently working at OSTraining as Director of Design.