added caching methods (especially for the capabilities)

This commit is contained in:
Steffen Vogel 2010-10-06 06:33:43 +02:00
parent af75d9a0ff
commit 473f998fb8
2 changed files with 53 additions and 9 deletions

View file

@ -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()));
}
}
}

View file

@ -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);