You are here

dynamic_cache.module in Dynamic Cache 7

Same filename and directory in other branches
  1. 6 dynamic_cache.module

Dynamic Cache module.

Allows other modules to disable page caching from hook_boot().

Usage: Set $GLOBALS['conf']['cache'] = false in your own module's hook_boot(), as needed.

File

dynamic_cache.module
View source
<?php

/**
 * @file
 * Dynamic Cache module.
 *
 * Allows other modules to disable page caching from hook_boot().
 *
 * Usage: Set $GLOBALS['conf']['cache'] = false in your own module's
 * hook_boot(), as needed.
 */

/**
 * Implements hook_boot().
 */
function dynamic_cache_boot() {
  if (dynamic_cache_should_run()) {

    // Overwrite cache header.
    header('X-Drupal-Cache: MISS');

    // Re-run full bootstrap and menu handler from index.php.
    // PROBLEM: this causes hook_boot() to be invoked a 2nd time, so we use
    // dynamic_cache_bootfix to setup ANOTHER boot hijacking to catch that.
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL, true);

    // Bootstrap is finished, now run menu handler from index.php.
    // (Currently, this code is never actually reached due to the bootfix module
    // hijacking the above bootstrap() call, but leaving it in just in case.)
    menu_execute_active_handler();

    // We're done.
    exit;
  }
}

/**
 * Helper function that tests whether or not bootstrap should be hijacked.
 */
function dynamic_cache_should_run() {

  // If the CURRENT cache setting is TRUE there is no reason to hijack or to
  // do any of the other checks that follow.
  if ($GLOBALS['conf']['cache']) {
    return FALSE;
  }

  // Bootstrap called outside of normal index.php (e.g. cron.php).
  if (strpos($_SERVER['PHP_SELF'], 'index.php') === FALSE) {
    return FALSE;
  }

  // We are in a drush context.
  if (defined('DRUSH_BOOTSTRAP_DRUPAL_FULL')) {
    return FALSE;
  }

  // See if caching is enabled for this request independent of the current
  // "dynamic" state of the cache setting.
  $cache_enabled = FALSE;
  if (variable_get('page_cache_without_database')) {
    $cache_enabled = TRUE;
  }
  elseif (isset($GLOBALS['conf']['dynamic_cache_orig_cache'])) {
    $cache_enabled = $GLOBALS['conf']['dynamic_cache_orig_cache'];
  }

  // Now we need to be sure that this hook_boot is being called from
  // _drupal_bootstrap_page_cache(). If it's not then the conditions are not
  // in place for our hijack to work correctly (and the hijack is not even
  // needed as Drupal is not serving a cached page).
  if (isset($_COOKIE[session_name()]) || !$cache_enabled || !drupal_page_get_cache(TRUE)) {
    return FALSE;
  }

  // If we get to this point we are configured to dynamically override caching
  // and we know that it is safe to do so.
  return TRUE;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function dynamic_cache_form_system_performance_settings_alter(&$form, &$form_state) {

  // The default value for Drupal's cache setting in the performance options
  // should NOT reflect the current state set by dynamic_cache. This means that
  // we can't depend on variable_get('cache') for this and should instead use
  // $GLOBALS['conf']['dynamic_cache_orig_cache'], which was set by
  // dynamic_cache_bootfix.
  $cache_default = variable_get('cache', FALSE);
  if (isset($GLOBALS['conf']['dynamic_cache_orig_cache'])) {
    $cache_default = $GLOBALS['conf']['dynamic_cache_orig_cache'];
  }
  $form['caching']['cache']['#default_value'] = $cache_default;
}

Functions

Namesort descending Description
dynamic_cache_boot Implements hook_boot().
dynamic_cache_form_system_performance_settings_alter Implements hook_form_FORM_ID_alter().
dynamic_cache_should_run Helper function that tests whether or not bootstrap should be hijacked.