From 473f998fb8620a74c9d88417206efe15883c4ab8 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Wed, 6 Oct 2010 06:33:43 +0200 Subject: [PATCH] added caching methods (especially for the capabilities) --- .../lib/Controller/CapabilitiesController.php | 28 ++++++++++----- backend/lib/View/View.php | 34 +++++++++++++++++++ 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/backend/lib/Controller/CapabilitiesController.php b/backend/lib/Controller/CapabilitiesController.php index 29e30d2..0569306 100644 --- a/backend/lib/Controller/CapabilitiesController.php +++ b/backend/lib/Controller/CapabilitiesController.php @@ -40,15 +40,25 @@ class CapabilitiesController extends Controller { * @param string $sub */ public function get($capabilities, $sub) { - if ($capabilities == 'definition' && in_array($sub, array('property', 'entity'))) { - $class = 'Volkszaehler\Definition\\' . ucfirst($sub) . 'Definition'; - $json = $class::getJSON(); - $this->view->add(array('definition' => array($sub => $json))); - } - elseif ($capabilities == 'version') { - } - else { - throw new \Exception('Unknown capability information: ' . implode('/', func_get_args())); + switch ($capabilities) { + case 'definition': + if (in_array($sub, array('property', 'entity'))) { + $class = 'Volkszaehler\Definition\\' . ucfirst($sub) . 'Definition'; + $json = $class::getJSON(); + $this->view->setCaching('expires', time()+2*7*24*60*60); // cache for 2 weeks + $this->view->add(array('definition' => array($sub => $json))); + } + else { + throw new Exception('Unkown definition information'); + } + break; + + case 'version': + // TODO implement + break; + + default: + throw new \Exception('Unknown capability information: ' . implode('/', func_get_args())); } } } diff --git a/backend/lib/View/View.php b/backend/lib/View/View.php index fc909ce..be14230 100644 --- a/backend/lib/View/View.php +++ b/backend/lib/View/View.php @@ -102,6 +102,7 @@ abstract class View { } /** + * Add object to output * * @param mixed $data */ @@ -125,6 +126,39 @@ abstract class View { } } + /** + * Sets caching mode for the browser + * + * @todo implement remaining caching modes + * @param $mode + * @param integer $value timestamp in seconds or offset in seconds + */ + public function setCaching($mode, $value) { + switch ($mode) { + case 'modified': // Last-modified + //$this->response->setHeader('Last-Modified', gmdate('D, d M Y H:i:s', $value) . ' GMT'); + + case 'etag': // Etag + throw new Exception('This caching mode is not implemented'); + + case 'expires': // Expire + $this->response->setHeader('Expires', gmdate('D, d M Y H:i:s', $value) . ' GMT'); + break; + + case 'age': // Cache-control: max-age= + $this->response->setHeader('Cache-control', 'max-age=' . $value); + break; + + case 'off': + case FALSE: + $this->response->setHeader('Cache-control', 'no-cache'); + $this->response->setHeader('Pragma', 'no-cache'); + + default: + throw new Exception('Unknown caching mode'); + } + } + protected abstract function render(); protected abstract function addData(Interpreter\InterpreterInterface $data);