Mercurial > SimpleWebPresenter
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 98:f2d52fed708c | 99:d98e315308cd |
|---|---|
| 3 | 3 |
| 4 include_once 'common-functions.inc'; | 4 include_once 'common-functions.inc'; |
| 5 include_once 'FileCacheSet.inc'; | 5 include_once 'FileCacheSet.inc'; |
| 6 $cache = ScriptIncludeCache::instance(__FILE__); | 6 $cache = ScriptIncludeCache::instance(__FILE__); |
| 7 $cache->cache_time("${baseDir}/FileCacheSet.inc"); | 7 $cache->cache_time("${baseDir}/FileCacheSet.inc"); |
| 8 | |
| 9 function toGMTime($time) { | |
| 10 return gmdate('D, d M Y H:i:s', $time) . ' GMT'; | |
| 11 } | |
| 12 | |
| 13 /** | |
| 14 * Checks if a HTTP_IF_MODIFIED_SINCE header is set, if this file is | |
| 15 * newer, set a Last-Modified header, otherwise abort with an 304 | |
| 16 * status code | |
| 17 */ | |
| 18 function CheckHttpModified($cache) | |
| 19 { | |
| 20 if (DEBUG_LEVEL >= VERBOSITY_DEBUG) | |
| 21 var_dump($_SERVER); | |
| 22 | |
| 23 $gmdate_mod = toGMTime($cache->getNewest()); | |
| 24 | |
| 25 if ($_SERVER['REDIRECT_URL'] == '/sitemap.xml') { | |
| 26 //print_r($gmdate_mod); | |
| 27 } | |
| 28 | |
| 29 if(array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) { | |
| 30 $HTTP_IF_MODIFIED_SINCE = $_SERVER['HTTP_IF_MODIFIED_SINCE']; | |
| 31 $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE); | |
| 32 | |
| 33 if (strtotime($if_modified_since) >= $cache->getNewest()) { | |
| 34 header("HTTP/1.0 304 Not Modified"); | |
| 35 foreach($cache->cacheControl() as $header => $value) { | |
| 36 header ("${header}: ${value}"); | |
| 37 } | |
| 38 return false; | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 foreach($cache->cacheControl() as $header => $value) { | |
| 43 header ("${header}: ${value}"); | |
| 44 } | |
| 45 header("Last-Modified: $gmdate_mod"); | |
| 46 return true; | |
| 47 } | |
| 48 | |
| 49 | |
| 8 | 50 |
| 9 /** | 51 /** |
| 10 * CacheTimeCheck provides a set of functions to enable generating a | 52 * CacheTimeCheck provides a set of functions to enable generating a |
| 11 * correct time for the latest update for a given file. | 53 * correct time for the latest update for a given file. |
| 12 * | 54 * |
| 18 { | 60 { |
| 19 parent::__construct(); | 61 parent::__construct(); |
| 20 if ($filename) | 62 if ($filename) |
| 21 $this->cache_time($filename); | 63 $this->cache_time($filename); |
| 22 $this->cache_time(__FILE__); | 64 $this->cache_time(__FILE__); |
| 65 $this->max_age=0; | |
| 23 } | 66 } |
| 24 | 67 |
| 25 public function addParent($cache) | 68 public function addParent($cache) |
| 26 { | 69 { |
| 27 parent::addParent($cache); | 70 parent::addParent($cache); |
| 28 } | |
| 29 | |
| 30 /** | |
| 31 * Checks if a HTTP_IF_MODIFIED_SINCE header is set, if this file is | |
| 32 * newer, set a Last-Modified header, otherwise abort with an 304 | |
| 33 * status code | |
| 34 */ | |
| 35 function CheckHttpModified() | |
| 36 { | |
| 37 if (DEBUG_LEVEL >= VERBOSITY_DEBUG) | |
| 38 var_dump($_SERVER); | |
| 39 | |
| 40 $gmdate_mod = gmdate('D, d M Y H:i:s', $this->getNewest()) . ' GMT'; | |
| 41 | |
| 42 if(array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) { | |
| 43 $HTTP_IF_MODIFIED_SINCE = $_SERVER['HTTP_IF_MODIFIED_SINCE']; | |
| 44 $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE); | |
| 45 | |
| 46 if (strtotime($if_modified_since) >= $this->getNewest()) { | |
| 47 header("HTTP/1.0 304 Not Modified"); | |
| 48 exit; | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 header("Last-Modified: $gmdate_mod"); | |
| 53 } | 71 } |
| 54 | 72 |
| 55 /** | 73 /** |
| 56 * Convenience function to load a file, and add it to the cacheset | 74 * Convenience function to load a file, and add it to the cacheset |
| 57 * | 75 * |
| 61 function loadFile($path) | 79 function loadFile($path) |
| 62 { | 80 { |
| 63 $this->cache_time($path); | 81 $this->cache_time($path); |
| 64 return loadFile($path); | 82 return loadFile($path); |
| 65 } | 83 } |
| 84 | |
| 66 } | 85 } |
| 67 ?> | 86 ?> |
