You are here

function boost_cache_set in Boost 6

Same name and namespace in other branches
  1. 5 boost.api.inc \boost_cache_set()

Replaces/Sets the cached contents of the specified page, if stale.

Parameters

$path: Current URL

$data: URL's contents

$extension: File extension for this mime type

1 call to boost_cache_set()
_boost_ob_handler in ./boost.module
PHP output buffering callback for static page caching.

File

./boost.module, line 3778
Provides static file caching for Drupal text output. Pages, Feeds, ect...

Code

function boost_cache_set($path, $data, $extension = BOOST_FILE_EXTENSION) {

  // Exit if nothing is here to cache
  if (empty($data)) {
    return FALSE;
  }

  // Get custom expiration time if set
  $router_item = _boost_get_menu_router();
  $settings = boost_get_settings_db($router_item);
  $expire = -2;
  foreach ($settings as $value) {
    if ($value != NULL) {
      $expire = $value['lifetime'];
      break;
    }
  }
  $cached_at = date('Y-m-d H:i:s', BOOST_TIME);

  // Code commenting style based on what is being cached.
  // Append the Boost footer with the relevant timestamps
  switch ($extension) {
    case BOOST_FILE_EXTENSION:
      $expire = $expire == -2 ? BOOST_CACHE_LIFETIME : $expire;
      if (variable_get('boost_apache_xheader', 0) < 2) {
        $comment_start = '<!-- ';
        $comment_end = " -->\n";
        $expires_at = date('Y-m-d H:i:s', BOOST_TIME + $expire);
        $comment = $comment_start . str_replace(array(
          '%cached_at',
          '%expires_at',
        ), array(
          $cached_at,
          $expires_at,
        ), BOOST_BANNER) . $comment_end;

        //$data = _boost_inject_code(rtrim($data), "\n" . $comment);
        $data = rtrim($data) . "\n" . $comment;
      }
      break;
    case BOOST_XML_EXTENSION:
      $expire = $expire == -2 ? BOOST_CACHE_XML_LIFETIME : $expire;
      if (variable_get('boost_apache_xheader', 0) < 2) {
        $comment_start = '<!-- ';
        $comment_end = " -->\n";
        $expires_at = date('Y-m-d H:i:s', BOOST_TIME + $expire);
        $comment = $comment_start . str_replace(array(
          '%cached_at',
          '%expires_at',
        ), array(
          $cached_at,
          $expires_at,
        ), BOOST_BANNER) . $comment_end;
        $data = rtrim($data) . "\n" . $comment;
      }
      break;
    case BOOST_JSON_EXTENSION:
      $expire = $expire == -2 ? BOOST_CACHE_JSON_LIFETIME : $expire;
      if (variable_get('boost_apache_xheader', 0) < 2) {
        $comment_start = '/* ';
        $comment_end = " */\n";
        $expires_at = date('Y-m-d H:i:s', BOOST_TIME + $expire);
        $comment = $comment_start . str_replace(array(
          '%cached_at',
          '%expires_at',
        ), array(
          $cached_at,
          $expires_at,
        ), BOOST_BANNER) . $comment_end;
        $data = rtrim($data) . "\n" . $comment;
      }
      break;
  }

  // Invoke hook_boost_preprocess($path, $data, $extension)
  foreach (module_implements('boost_preprocess') as $module) {
    if (($result = module_invoke($module, 'boost_preprocess', $path, $data, $extension)) != NULL) {
      $data = $result;
    }
  }

  // Execute the pre-process function if one has been defined
  if (function_exists(BOOST_PRE_PROCESS_FUNCTION)) {
    $data = call_user_func(BOOST_PRE_PROCESS_FUNCTION, $path, $data, $extension);
  }
  db_set_active();

  // Final check, make sure this page should be cached. Allow for the preprocess
  // function to have a final say in if this page should be cached.
  if (!$GLOBALS['_boost_cache_this'] || empty($data)) {
    return FALSE;
  }

  // Create or update the static files as needed
  if (($filename = boost_file_path($path, TRUE, $extension)) && (BOOST_OVERWRITE_FILE || !file_exists($filename) || boost_db_is_expired($filename))) {

    // Special handling of the front page for aggressive gzip test
    if ($path == '' && BOOST_AGGRESSIVE_GZIP && $extension == BOOST_FILE_EXTENSION) {
      _boost_generate_gzip_test_file();
      boost_cache_write($filename, _boost_inject_code($data, '<script type="text/javascript">
<!--//--><![CDATA[//><!--
function boost_gzip_test_ready() {
  if(boost_xhr.readyState != 4) {
    setTimeout(boost_gzip_test_ready, 1000);
  }
  else if (boost_xhr.getResponseHeader("Content-Encoding") == "gzip" && boost_xhr.responseText.indexOf("</html>") != -1) {
    var date = new Date();
    var days = 14;
    date.setTime(date.getTime() + (days * 24*60*60*1000));
    expires = "; expires=" + date.toUTCString();
    document.cookie = "boost-gzip=true" + expires + "; path=/"
  }
}

var boost_xhr = $.ajax({url: Drupal.settings.basePath + "boost-gzip-cookie-test.html"});
boost_gzip_test_ready();
//--><!]]>
</script>' . "\n"));
    }
    else {
      boost_cache_write($filename, $data);
    }
    if (!BOOST_NO_DATABASE) {
      boost_db_prep($filename, $extension, BOOST_TIME + $expire);
      boost_cache_set_node_relationships(isset($GLOBALS['_boost_relationships']) ? $GLOBALS['_boost_relationships'] : array());
    }
    return TRUE;
  }
  else {
    return FALSE;
  }
}