Dealing with Slow modules and Caching

One thing I often come across is sites that load slowly due to a single module needing to load data from an external source. Caching helps with this considerably, at least until the cache expires.

As a prime example, let's use a Twitter module. To load the feed data, the module needs to access Twitter's API. This happens when the page request is received by the hosting server, and one of two things will then happen. If the cache data exists and is still valid (i.e. hasn't expired) the page will load lightning fast.

Unfortunately, if the cache has expired, or just doesn't exist, the module will need to access Twitter's API. Occasionally the API runs slowly and so the user has to wait for your homepage (or whichever page the module is on) to load. This provides the user with a bad experience, made worse if the module doesn't actually form part of the core content that the user is (im)patiently trying to view.

This documentation details a quick and easy way to resolve this issue.

 

The solution is actually quite simple, we need to ensure that the module's cache is refreshed regularly. If we've a lot of different modules (with different cache lifetimes) it'd be impractical to tie this to actual cache expiry times, so we'll go for a set interval instead.

Note: When Joomla finally includes it's proposed scheduling system, it may be possible to write a component that essentially completes these steps automatically.

 

  1. Log into the back-end of your Joomla! site.
  2. Menus->Menu Manager->Add New Menu
  3. Give your menu a name that you'll remember (We'll use hiddenMenu)
  4. Menus->hiddenMenu -> Add New Menu Item
  5. Create a menu item pointing to an empty search page. No-one real is going to see it, so it doesn't matter too much what you select but I prefer the search module as there's little point in pulling data through un-necessarily
  6. Call the new item ModuleRefreshMenu
  7. Now go to each of the affected modules in Module Manager and assign the module to the new Menu Item (don't remove it from wherever it's currently displaying!)
  8. The next step assumes you are using Joomla!s default SEF, if you're using a more advanced system you'll need to change the URL slightly.
  9. Create a CronJob on your server (whether through CPanel, Plesk or from the Shell)
	*/15 * * * * wget -q -O - "http://mywebsite.com/ModuleRefreshMenu"

The server will now retrieve that page every 15 minutes, causing each of the modules to check the relevant cache and refresh it if it has expired.

It's not a silver bullet, if your module's cache expires at 0917 the next refresh may well be a user visiting at 0918 (in which case they'll get a slow load time). You should therefore probably adjust the cron-job to best reflect the visting pattern.

 

If you want to filter the automation out of your stats, adjust the command to use a noticeable User-Agent so that you can exclude it (mentally or otherwise) from your stats

*/15 * * * * wget -q -U "ModuleRefreshForce" -O - "http://mywebsite.com/ModuleRefreshMenu"