1 bootstrap.inc backdrop_page_get_cache($check_only = FALSE, $return_stale = FALSE)

Retrieves the current page from the cache.

Note: we do not serve cached pages to authenticated users, or to anonymous users when $_SESSION is non-empty. $_SESSION may contain status messages from a form submission, the contents of a shopping cart, or other user- specific content that should not be cached and displayed to other users.

Parameters

bool $check_only: Set to TRUE to only return whether a previous call found a cache entry.

bool $return_stale: If background fetch is enabled, page cache entries are stored for longer period of time than the page cache maximum age. This allows stale entries to be served while generating new cache entries. If set to TRUE, then these stale cache entries will be returned. If left at the default of FALSE, only cache entries within the maximum cache lifetime will be returned.

Return value

stdClass|NULL: The cache object, if the page was found in the cache, NULL otherwise.

File

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

Code

function backdrop_page_get_cache($check_only = FALSE, $return_stale = FALSE) {
  global $base_root;
  static $cache_hit = array();

  // Keep two separate cache entries for an expired or non-expired hit.
  $cache_type = $return_stale ? 'stale' : 'fresh';
  if (!isset($cache_hit[$cache_type])) {
    $cache_hit[$cache_type] = FALSE;
  }

  // Return any previous value on checks only.
  if ($check_only) {
    return $cache_hit[$cache_type];
  }

  $cache = NULL;
  if (backdrop_page_is_cacheable()) {
    $cache = cache('page')->get($base_root . request_uri());
    if ($cache !== FALSE) {
      $cache_lifetime = config_get('system.core', 'page_cache_maximum_age');
      $cache_valid = $cache->created + $cache_lifetime >= REQUEST_TIME;
      // Return the cache entry if not expired or if an expired entry is okay.
      if ($cache_valid || $return_stale) {
        $cache_hit[$cache_type] = TRUE;
      }
      else {
        $cache_hit[$cache_type] = FALSE;
        $cache = NULL;
      }
    }
  }
  return $cache;
}