Mercurial > SimpleWebPresenter
changeset 14:91ee5f49907e
Correct caching.
We could probably push caching earlier, but more important that it is
correct.
| author | Tom Fredrik "BFG" Klaussen <bfg@blenning.no> |
|---|---|
| date | Wed, 19 Sep 2012 13:55:58 +0200 |
| parents | 9dab5b96b789 |
| children | f51be7b9711a |
| files | cache_check.inc index.php |
| diffstat | 2 files changed, 45 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- a/cache_check.inc Wed Sep 19 12:42:32 2012 +0200 +++ b/cache_check.inc Wed Sep 19 13:55:58 2012 +0200 @@ -1,17 +1,18 @@ <?php -if (DEBUG) - var_dump($_SERVER); +function cache_check($mtime) +{ + if (DEBUG) + var_dump($_SERVER); -$HTTP_IF_MODIFIED_SINCE=$_SERVER['HTTP_IF_MODIFIED_SINCE']; -$if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE); + $HTTP_IF_MODIFIED_SINCE=$_SERVER['HTTP_IF_MODIFIED_SINCE']; + $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE); -$SCRIPT_FILENAME=$_SERVER['SCRIPT_FILENAME']; -$mtime = filemtime($SCRIPT_FILENAME); -$gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT'; + $gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT'; -if ($if_modified_since == $gmdate_mod) { + if ($if_modified_since == $gmdate_mod) { header("HTTP/1.0 304 Not Modified"); exit; + } + header("Last-Modified: $gmdate_mod"); } -header("Last-Modified: $gmdate_mod"); ?> \ No newline at end of file
--- a/index.php Wed Sep 19 12:42:32 2012 +0200 +++ b/index.php Wed Sep 19 13:55:58 2012 +0200 @@ -2,18 +2,30 @@ define(DEBUG,0); define(MAX_RECURSE,50); +function include_with_mtime($file) { + global $newest; + $mtime = filemtime($file); + if ($mtime > $newest) { + $newest = $mtime; + } + include $file; +} + if (DEBUG) { error_reporting(E_ALL); ini_set("display_errors", 1); } +$SCRIPT_FILENAME=$_SERVER['SCRIPT_FILENAME']; +$newest = filemtime($SCRIPT_FILENAME); +$cachable = true; -include 'php/cache_check.inc'; -include 'php/accept-language.inc'; -include 'php/filters.inc'; -include 'php/common-functions.inc'; +include_with_mtime('php/cache_check.inc'); +include_with_mtime('php/accept-language.inc'); +include_with_mtime('php/filters.inc'); +include_with_mtime('php/common-functions.inc'); -$URL_PARAMS=array('name','lang'); +$URL_PARAMS=array('name', 'lang'); #Globals $name = $_GET['name']; @@ -48,14 +60,19 @@ $includes=$doc->getElementsByTagName("include"); $recurse=0; -while($includes->length>0) { - if(++$recurse>MAX_RECURSE) { +while($includes->length > 0) { + if(++$recurse > MAX_RECURSE) { errorPage('Recursion limit exceeded', 500); } foreach ($includes as $include) { $src=$include->getAttribute("src"); $subdoc = new DOMDocument(); - $subdoc->load("${lang}/${src}"); + $fname = "${lang}/${src}"; + $subdoc->load($fname); + $mtime = filemtime($fname); + if ($mtime > $newest) { + $newest = $mtime; + } $parent=$include->parentNode; $xml=getElementByTagName($subdoc,"xml"); foreach($xml->childNodes as $child) { @@ -130,6 +147,7 @@ foreach ($files as $file) { $script=$file->getAttribute("script"); if ($script) { + $cachable = false; $src=""; $cwd=getcwd(); @@ -145,7 +163,12 @@ } else { $src=$file->getAttribute("src"); - $file_content=loadFile("${lang}/${src}"); + $fname = "${lang}/${src}"; + $mtime = filemtime($fname); + if ($mtime > $newest) { + $newest = $mtime; + } + $file_content=loadFile($fname); } if(floatval($file_content)<0) { errorPage("Resource not found '${lang}/${src}'"); @@ -171,6 +194,9 @@ } $out.= $file_content; } +if ($cachable) + cache_check($newest); + $out.=' </div>
