view CacheTimeCheck.inc @ 99:d98e315308cd

Improved caching of flag and sitemap.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Sun, 14 Sep 2014 21:11:27 +0200
parents dd4ddedca4c5
children adf7b11921f4
line wrap: on
line source

<?php
$baseDir = dirname(__FILE__);

include_once 'common-functions.inc';
include_once 'FileCacheSet.inc';
$cache = ScriptIncludeCache::instance(__FILE__);
$cache->cache_time("${baseDir}/FileCacheSet.inc");

function toGMTime($time) {
  return gmdate('D, d M Y H:i:s', $time) . ' GMT';
}

/**
 * Checks if a HTTP_IF_MODIFIED_SINCE header is set, if this file is
 * newer, set a Last-Modified header, otherwise abort with an 304
 * status code
 */
function CheckHttpModified($cache)
{
  if (DEBUG_LEVEL >= VERBOSITY_DEBUG)
    var_dump($_SERVER);

  $gmdate_mod = toGMTime($cache->getNewest());

  if ($_SERVER['REDIRECT_URL'] == '/sitemap.xml') {
    //print_r($gmdate_mod);
  }

  if(array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
    $HTTP_IF_MODIFIED_SINCE = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
    $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE);

    if (strtotime($if_modified_since) >= $cache->getNewest()) {
      header("HTTP/1.0 304 Not Modified");
      foreach($cache->cacheControl() as $header => $value) {
	header ("${header}: ${value}");
      }
      return false;
    }
  }

  foreach($cache->cacheControl() as $header => $value) {
    header ("${header}: ${value}");
  }
  header("Last-Modified: $gmdate_mod");
  return true;
}



/**
 * CacheTimeCheck provides a set of functions to enable generating a
 * correct time for the latest update for a given file.
 *
 * @author Tom Fredrik Blenning Klaussen
 */
class CacheTimeCheck extends FileCacheSet
{
  function __construct($filename = False)
  {
    parent::__construct();
    if ($filename)
      $this->cache_time($filename);
    $this->cache_time(__FILE__);
    $this->max_age=0;
  }

  public function addParent($cache)
  {
    parent::addParent($cache);
  }

  /**
   * Convenience function to load a file, and add it to the cacheset
   *
   * @param $path path of the file
   * @return the contents of the file
   */
  function loadFile($path)
  {
    $this->cache_time($path);
    return loadFile($path);
  }

}
?>