Dynamic Cache is an API module which allows developers to dynamically disable Drupal's standard page cache* for arbitrary conditions, by clearing the global $conf['cache'] variable from their own module's hook_boot() implementation. Normally this doesn't work; it is too late to disable caching by the time you get to hook_boot().

Dynamic Cache does not include any user interface (although there is now a test page provided by a separate testing module), it is intended for usage by module developers who wish to have more control over Drupal's standard caching behaviour.

There is an ongoing discussion of modifying core to allow this functionality natively here: #322104: Allow hook_boot() to disable page cache, but this has not even made it into D7, much less D6, which is why I decided to contribute this module, based on code from a live production site I developed, which enables this functionality immediately in D6 (and now, for D7).

* "disables page cache" in this context means: "Guaranteeing a dynamic page is generated, by preventing a cached page from being served.". Dynamic Cache does not concern itself with whether such pages are then STORED in the cache; this can already be prevented by explicitly calling drupal_page_is_cacheable(FALSE); or via the Cache Exclude module.

Usage

Enable the module, then in your own custom module, in hook_boot(), you can clear the global variable $conf['cache'], and this will properly disable Drupal's standard cache.

Technically, you should set the global $conf['cache'] variable to FALSE for D7, or CACHE_DISABLED for D6 (which equals 0) inside of your own module's hook_boot() implementation, however simply setting it to 0 (or false) in either case should work fine.

Standard vs "Aggressive" Caching

Drupal's "standard" caching will invoke hook_boot(), which is needed for Dynamic Cache to function.

Drupal also supports a special "aggressive" caching mode which does NOT invoke hook_boot(), thereby preventing Dynamic Cache from working.

In D6 this option is set from the Performance page (turning on Aggressive Caching will prevent Dynamic Cache from working).

In D7 this option is controlled via the 'page_cache_invoke_hooks' variable, generally set from settings.php (setting this variable to FALSE will prevent Dynamic Cache from working).

Example

Suppose you want to disable page caching if the user has any items stored in "shopping cart" type session data. With the Dynamic Cache module enabled, you could use code similar to the following to make this happen:

MYMODULE_boot(){
  if( !empty( $_SESSION['cart_items'] ) ){ 
    $GLOBALS['conf']['cache'] = false; 
  }
} 

How It Works

When the global cache variable is disabled, this module essentially "hijacks" the standard Drupal bootstrap process by never actually returning from hook_boot(); it completes the rest of the bootstrap process itself.

There is a detailed description of this here: http://drupal.org/node/875152

Important Note

Because Dynamic Cache never returns from hook_boot(), it is critical that Dynamic Cache is executed AFTER any other modules that use hook_boot(). This means Dynamic Cache must be given a higher weight than all other modules which use hook_boot(). Dynamic Cache will install itself with a very high weight, but care should be taken to ensure no other boot modules are given a higher weight. Module weights can be inspected directly in the database's "system" table, or by using Util's Module Weights sub-module: http://drupal.org/project/util

D7 Version

Due to a fundamental difference in D7's bootstrap sequence, Dynamic Cache re-invoking drupal_bootstrap() from within the initial hook_boot() call (invoked from _drupal_bootstrap_page_cache()) causes hook_boot() be be invoked TWICE, the second time from _drupal_bootstrap_page_header().

Because of this, a secondary "bootfix" module is needed, which hijacks the second hook_boot() in order to prevent other modules' hook_boot()'s from being erroneously called. You can read more about this here: #1515474: D7 version.

In contrast to the main Dynamic Cache module (which must run AFTER all other modules), the Bootfix module must run BEFORE all other modules, and as such is installed with a very low weight, and care should be taken to ensure no other boot module is assigned a lower weight than Dynamic Cache Bootfix.

Project Information

Downloads