1.20.x bootstrap.inc _backdrop_bootstrap_page_cache()

Attempts to serve a page from the cache.


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


function _backdrop_bootstrap_page_cache() {
  global $user;
  $site_config = config('system.core');

  // 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';
  // Load the cache backends.
  // Old-style loading for compatibility.
  foreach (settings_get('cache_backends', array()) as $include) {
    require_once BACKDROP_ROOT . '/' . $include;

  try {
    $cache_enabled = $site_config->get('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 = $site_config->get('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 = $site_config->get('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().

      // 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);

        // Load bootstrap modules.
        require_once BACKDROP_ROOT . '/core/includes/module.inc';

      // Deliver the cached page.

      // 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)) {

      // 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).

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

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

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