You are here

function boost_cache_set in Boost 5

Same name and namespace in other branches
  1. 6 boost.module \boost_cache_set()

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

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

File

./boost.api.inc, line 118
Implements the Boost API for static page caching.

Code

function boost_cache_set($path, $data = '') {

  // Append the Boost footer with the relevant timestamps
  $time = time();
  $cached_at = date('Y-m-d H:i:s', $time);
  $expires_at = date('Y-m-d H:i:s', $time + variable_get('cache_lifetime', 600));
  $data = rtrim($data) . "\n" . str_replace(array(
    '%cached_at',
    '%expires_at',
  ), array(
    $cached_at,
    $expires_at,
  ), BOOST_BANNER);

  // 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, $data);
  }
  $alias = drupal_get_path_alias($path);
  $path = drupal_get_normal_path($path);

  // normalize path
  // Create or update the static file as needed
  if ($filename = boost_file_path($path)) {
    _boost_mkdir_p(dirname($filename));
    if (!file_exists($filename) || boost_file_is_expired($filename)) {
      if (file_put_contents($filename, $data) === FALSE) {
        watchdog('boost', t('Unable to write file: %file', array(
          '%file' => $filename,
        )), WATCHDOG_WARNING);
      }
    }

    // If a URL alias is defined, create that as a symlink to the actual file
    if ($alias != $path && ($symlink = boost_file_path($alias))) {
      _boost_mkdir_p(dirname($symlink));
      if (!is_link($symlink) || realpath(readlink($symlink)) != realpath($filename)) {
        if (file_exists($symlink)) {
          @unlink($symlink);
        }
        if (!_boost_symlink($filename, $symlink)) {
          watchdog('boost', t('Unable to create symlink: %link to %target', array(
            '%link' => $symlink,
            '%target' => $filename,
          )), WATCHDOG_WARNING);
        }
      }
    }
  }
  return TRUE;
}