Drupal: disable caching for an entire module

I have a legacy php application that had to be integrated into a Drupal site, so I wrapped it in a simple drupal module. The code needs to run for anonymous users.

The wrapping worked fine, but the Drupal cache really gets in the way sometimes. For instance, my legacy code does not use the drupal_set_message functions when a form submit fails. That means that the page that tells the user they made a mistake gets cached, leading to all sorts of unexpected results.

I looked around for a way to disable the Drupal cache for an entire module, but only found some results that didn’t quite do the trick. But then I stumbled across the CacheExclude module, which does exactly what I want. I didn’t want to include yet another module for something as simple as this, so just duplicated the ‘magic’ lines in my module:

function your_module_name_init() {
  // We don't want caching for this module
  if (drupal_match_path($_GET['q'],'url_of_your_module/*')) {
    $GLOBALS['conf']['cache'] = FALSE;

In that snippet, you’ll want to change ‘url_of_your_module’ to a string unique to the path of your module. For instance, if your module lives a http://my.web.site/the-wrapped-module/, then you could put ‘the-wrapped-module’ in the place of ‘url_of_your_module’.

And that does the trick. Now, I’m by no means a Drupal expert, so there may well be a better way to do this. Feel free to leave a comment if you know how.

This entry was posted in Free Software/Open Source. Bookmark the permalink.

2 Responses to Drupal: disable caching for an entire module

  1. kinjal says:

    if (drupal_match_path($_GET['q'],’url_of_your_module/*’))

    at this line

    url_of_your_module mean which path

    addressbar path or physical path.

    clear more , with an example

  2. ward says:

    It’s part of the url. I’ve added an example.


Leave a Reply