You are here

function page_set_cache in Drupal 4

Same name and namespace in other branches
  1. 5 includes/common.inc \page_set_cache()
  2. 6 includes/common.inc \page_set_cache()

Store the current page in the cache.

We try to store a gzipped version of the cache. This requires the PHP zlib extension (http://php.net/manual/en/ref.zlib.php). Presence of the extension is checked by testing for the function gzencode. There are two compression algorithms: gzip and deflate. The majority of all modern browsers support gzip or both of them. We thus only deal with the gzip variant and unzip the cache in case the browser does not accept gzip encoding.

See also

drupal_page_header

Related topics

1 call to page_set_cache()
drupal_page_footer in includes/common.inc
Perform end-of-request tasks.

File

includes/common.inc, line 1470
Common functions that many Drupal modules will need to reference.

Code

function page_set_cache() {
  global $user, $base_root;
  if (!$user->uid && $_SERVER['REQUEST_METHOD'] == 'GET') {

    // This will fail in some cases, see page_get_cache() for the explanation.
    if ($data = ob_get_contents()) {
      $cache = TRUE;
      if (function_exists('gzencode')) {

        // We do not store the data in case the zlib mode is deflate.
        // This should be rarely happening.
        if (zlib_get_coding_type() == 'deflate') {
          $cache = FALSE;
        }
        else {
          if (zlib_get_coding_type() == FALSE) {
            $data = gzencode($data, 9, FORCE_GZIP);
          }
        }

        // The remaining case is 'gzip' which means the data is
        // already compressed and nothing left to do but to store it.
      }
      ob_end_flush();
      if ($cache && $data) {
        cache_set($base_root . request_uri(), $data, CACHE_TEMPORARY, drupal_get_headers());
      }
    }
  }
}