From 41c341624c16607ac710aac446cc0ead3c53671b Mon Sep 17 00:00:00 2001 From: arno Date: Fri, 7 Aug 2009 23:49:54 +0200 Subject: [PATCH] implements ETag caching for items.php and news.php --- items.php | 21 ++++++++++++++++++--- news.php | 19 +++++++++++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/items.php b/items.php index c363183..e7a9723 100644 --- a/items.php +++ b/items.php @@ -6,9 +6,22 @@ require_once ("./inc/settings.php"); require_once ("./inc/utils.php"); require_once ("./inc/db/mysql.php"); -function main ($features) { +function headers_callback ($output) { + $etag = md5 ($output); + if ((isset ($_SERVER ["HTTP_IF_NONE_MATCH"])) && + ($_SERVER ["HTTP_IF_NONE_MATCH"] == $etag)) { + header ("HTTP/1.1 304 Not Modified"); + exit (); + } - header ("Cache-control: no-cache"); + header ("Content-type: application/vnd.google-earth.kml+xml"); + header ("Cache-control: must-revalidate"); + header (sprintf ("ETag: %s", $etag)); + + return $output; +} + +function main ($features) { echo ' @@ -70,6 +83,8 @@ try { } catch (Exception $e) { exit ("server error"); } -header ("Content-type: application/vnd.google-earth.kml+xml"); + +ob_start ("headers_callback"); main ($features); +ob_end_flush () ?> diff --git a/news.php b/news.php index b729bb7..6eb78e6 100644 --- a/news.php +++ b/news.php @@ -6,8 +6,20 @@ require_once ("./inc/settings.php"); require_once ("./inc/db/mysql.php"); require_once ("./inc/utils.php"); -header ("Content-type: application/atom+xml; charset=UTF-8"); -header("Cache-control: no-cache"); +function headers_callback ($output) { + $etag = md5 ($output); + if ((isset ($_SERVER ["HTTP_IF_NONE_MATCH"])) && + ($_SERVER ["HTTP_IF_NONE_MATCH"] == $etag)) { + header ("HTTP/1.1 304 Not Modified"); + exit (); + } + + header ("Content-type: application/atom+xml; charset=UTF-8"); + header ("Cache-control: must-revalidate"); + header (sprintf ("ETag: %s", $etag)); + + return $output; +} function date3339 ($feature) { $date = date ('Y-m-d\TH:i:s', $feature->date); @@ -118,5 +130,8 @@ try { } catch (Exception $e) { exit ("server error"); } + +ob_start ("headers_callback"); main ($features); +ob_end_flush () ?> -- 2.39.2