1 bootstrap.inc _backdrop_bootstrap_page_cache()

Attempts to serve a page from the cache.

File

core/includes/bootstrap.inc, line 3027
Functions that need to be loaded on every Backdrop request.

Code

function _backdrop_bootstrap_page_cache() {
  global $user;

  // Allow specifying special cache handlers in settings.php, like
  // using memcached or files for storing cache information.
  require_once BACKDROP_ROOT . '/core/includes/cache.inc';
  foreach (settings_get('cache_backends', array()) as $include) {
    require_once BACKDROP_ROOT . '/' . $include;
  }

  try {
    $cache_enabled = config_get('system.core', 'cache') ? TRUE : FALSE;
  }
  catch (ConfigException $e) {
    $cache_enabled = FALSE;
  }

  if (!backdrop_page_is_cacheable()) {
    $cache_enabled = FALSE;
  }

  // If there is no session cookie and cache is enabled, serve a cached page.
  if (!isset($_COOKIE[session_name()]) && $cache_enabled) {
    // Make sure there is a user object because its timestamp will be
    // checked, hook_boot might check for anonymous user etc.
    $user = backdrop_anonymous_user();
    // Get the page from the cache.
    $background_fetch_enabled = config_get('system.core', 'page_cache_background_fetch');
    $cache = backdrop_page_get_cache(FALSE, $background_fetch_enabled);
    // If there is a cached page, display it.
    if (is_object($cache)) {
      header('X-Backdrop-Cache: HIT');

      // Although a cached page was found, it may be expired if background
      // fetch is enabled. So we calculate if it is within the max age based
      // on its creation date.
      $lifetime = config_get('system.core', 'page_cache_maximum_age');
      $expires_on = $cache->created + $lifetime;
      $cache_expired = REQUEST_TIME > $expires_on;

      // Start a buffer for hook_boot(), the cached page, and hook_exit().
      ob_start();

      // Calls to hook_boot() on page cache requests is deprecated, though they
      // can still be enabled via settings.php
      if (settings_get('page_cache_invoke_hooks', FALSE)) {
        // Restore the metadata cached with the page for hook_boot().
        $_GET['q'] = $cache->data['path'];
        backdrop_set_title($cache->data['title'], PASS_THROUGH);
        date_default_timezone_set(backdrop_get_user_timezone());

        // Load bootstrap modules.
        require_once BACKDROP_ROOT . '/core/includes/module.inc';
        module_load_all(TRUE);
        bootstrap_invoke_all('boot');
      }

      // Deliver the cached page.
      backdrop_serve_page_from_cache($cache);

      // Like hook_boot(), hook_exit() is no longer called on page cache
      // requests unless enabled via settings.php.
      if (settings_get('page_cache_invoke_hooks', FALSE)) {
        bootstrap_invoke_all('exit');
      }

      // Close the connection immediately if we are about to serve an expired
      // cache entry. This allows for background processing without holding up
      // the browser.
      if ($cache_expired) {
        header('Connection: close');
      }

      // End the request and send the response to the browser. This call has
      // error suppression on it as some PHP versions may require it (PHP 5.3)
      // but other PHP versions throw a warning (PHP 5.5).
      @ob_end_flush();

      // Flushing for PHP-FPM based installations.
      if (function_exists('fastcgi_finish_request')) {
        fastcgi_finish_request();
      }
      // Flushing for Apache mod_php installations.
      else {
        flush();
      }

      // Continue processing to generate the new cache entry for the next user.
      if ($cache_expired) {
        // Enter background mode (this suppresses any output of headers).
        backdrop_is_background(TRUE);

        // Restore the current path and page title.
        $_GET['q'] = request_path();
        backdrop_static_reset('backdrop_set_title');
        backdrop_static_reset('backdrop_http_headers');
      }
      // No new cache entry needed. Exit immediately after serving the page.
      else {
        exit();
      }
    }
    else {
      header('X-Backdrop-Cache: MISS');
    }
  }
}