1 common.inc backdrop_page_create_cache($page_content)

Create a page cache object for a page request.

If page_compression is enabled, a gzipped version of the page is stored in the cache to avoid compressing the output on each request. The cache entry is unzipped on delivery in the relatively rare event that the page is requested by a client without gzip support.

Page compression requires the PHP zlib extension (http://php.net/manual/ref.zlib.php).

Parameters

$page_content: The page content (HTML) that should be entered into a cache entry.

Return value

stdClass|FALSE: A cache entry ready to be written to the cache backend. If the page is not cacheable, FALSE will be returned.

See also

backdrop_page_header()

backdrop_page_save_cache();

File

core/includes/common.inc, line 6081
Common functions that many Backdrop modules will need to reference.

Code

function backdrop_page_create_cache($page_content) {
  global $base_root;

  // Check whether the current page might be compressed.
  $config = config('system.core');
  $page_compressed = $config->get('page_compression') && extension_loaded('zlib');

  // If using background fetch, we keep cache entries for a much longer period.
  // This allows the stale entries to be served while generating a new one.
  if ($config->get('page_cache_background_fetch')) {
    $stale_lifetime = $config->get('page_cache_keep_stale_age');
    // Ensure a stale lifetime is set. Defaults to 2 days.
    if (!$stale_lifetime) {
      $stale_lifetime = 172800;
    }
    $cache_lifetime = $stale_lifetime;
  }
  // If not using background fetch, the page only needs to be kept for as long
  // as the life of the cache lifetime.
  else {
    $cache_lifetime = $config->get('page_cache_maximum_age');
  }

  $cache = (object) array(
    'cid' => $base_root . request_uri(),
    'data' => array(
      'path' => $_GET['q'],
      'body' => $page_content,
      'title' => backdrop_get_title(),
      'headers' => array(),
      // We need to store whether page was compressed or not,
      // because by the time it is read, the configuration might change.
      'page_compressed' => $page_compressed,
    ),
    'expire' => REQUEST_TIME + $cache_lifetime,
    'created' => REQUEST_TIME,
  );

  if ($page_compressed) {
    $cache->data['body'] = gzencode($cache->data['body'], 9, FORCE_GZIP);
  }

  // Restore preferred header names based on the lower-case names returned
  // by backdrop_get_http_header().
  $header_names = _backdrop_set_preferred_header_name();
  foreach (backdrop_get_http_header() as $name_lower => $value) {
    $cache->data['headers'][$header_names[$name_lower]] = $value;
    if ($name_lower == 'cache-control') {
      // Set page cache expiration time based on Cache-Control header.
      $matches = array();
      if (preg_match('/max-age=(\d+)/', $value, $matches)) {
        $cache->expire = REQUEST_TIME + $matches[1];
      }
    }
  }

  return $cache;
}