You are here

function authcache_retrieve_cache_page in Authenticated User Page Caching (Authcache) 7

Send cached page to browser, if found.

Return value

boolean TRUE if page was delivered, FALSE otherwise

1 call to authcache_retrieve_cache_page()
authcache.inc in ./authcache.inc
Attempt to deliver a cached version of a page depending on the users role.

File

./authcache.inc, line 40
Attempt to deliver a cached version of a page depending on the users role.

Code

function authcache_retrieve_cache_page() {
  global $base_root;

  // Do not attempt to serve page from cache, when invoked by drush.
  if (drupal_is_cli()) {
    return FALSE;
  }

  // User is logged in but their role should not receive any cached pages
  // (i.e., cached anonymous pages, since they have no authcache key)

  /* simg: this logic doesn't work, because a visitor can have the drupal_user cookie set whilst not logged in */

  /*if (isset($_COOKIE['drupal_user']) && !isset($_COOKIE['authcache'])) {
  		return FALSE;
  	}*/

  // Caching for browser session was temporarily disabled (most likely from drupal_set_message()/drupal_goto() redirect)
  if (isset($_COOKIE['nocache_temp'])) {
    setcookie('nocache', '', REQUEST_TIME - 36000, ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure') == '1');
    setcookie('nocache_temp', '', REQUEST_TIME - 36000, ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure') == '1');
    return FALSE;
  }

  // Caching disabled for browser session
  if (isset($_COOKIE['nocache'])) {
    return FALSE;
  }

  // Attempt to retrieve page from cache
  if (empty($_POST)) {

    // Connect to database and initialzie configuration if not
    // disabled from within settings.php
    if (!variable_get('page_cache_without_database')) {
      drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES, FALSE);
    }

    // Authenticated cache role(s) key
    if ($authcache_keygen = variable_get('authcache_key_generator', FALSE)) {
      $key = isset($_COOKIE['authcache']) && $_COOKIE['authcache'] ? $_COOKIE['authcache'] : $authcache_keygen();
    }
    else {
      $key = isset($_COOKIE['authcache']) && $_COOKIE['authcache'] ? $_COOKIE['authcache'] : '';
    }
    $cache_key = $key . $base_root . request_uri();
    $cache = cache_get($cache_key, 'cache_page');
    if (!empty($cache)) {

      // Cached page found
      header('X-Drupal-Cache: HIT');

      // render cache benchmark
      if (isset($_COOKIE['authcache_debug'])) {
        setcookie('cache_render', timer_read('page'));
      }
      drupal_serve_page_from_cache($cache);
      return TRUE;
    }
    else {

      //cache miss
      header('X-Drupal-Cache: MISS');
    }
  }
  else {

    //form post detected so allow normal page processing
  }
}