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).
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 (it will not work for aggressive caching, which does not even invoke hook_boot).
Technically, you should set the global $conf['cache'] variable to FALSE for D7, or CACHE_DISABLED for D6 (which equals 0) inside of their own module's hook_boot() implementation, however simply setting it to 0 (or false) in either case should work fine.
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:
<?php
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
How Stable & Well Tested Is This?
Very. This code has been in use on a large production site since August 2010 with no issues, and at least 3-4 other people have reported using this with no issues in the original project proposal thread: #916552: Dynamic Cache.
D7 Version
A working D7 version is now available. Please note this has not been tested to the same extent on production servers as the D6 version, but works along the exact same (relatively simple) principles and passes all tests in development.
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.
Downloads
Project Information
- Maintenance status: Actively maintained
- Development status: Maintenance fixes only
- Reported installs: 8 sites currently report using this module. View usage statistics.
- Downloads: 17
- Last modified: May 19, 2012