[core] Returning 304 http code when returning cached data (#793)

This commit is contained in:
Eugene Molotov 2018-08-26 00:00:38 +05:00 committed by Teromene
parent 059656c370
commit 422c125d8e
3 changed files with 33 additions and 0 deletions

View file

@ -195,6 +195,7 @@ try {
try { try {
$bridge->setCache($cache); $bridge->setCache($cache);
$bridge->setCacheTimeout($cache_timeout); $bridge->setCacheTimeout($cache_timeout);
$bridge->dieIfNotModified();
$bridge->setDatas($params); $bridge->setDatas($params);
} catch(Error $e) { } catch(Error $e) {
http_response_code($e->getCode()); http_response_code($e->getCode());
@ -211,6 +212,7 @@ try {
$format = Format::create($format); $format = Format::create($format);
$format->setItems($bridge->getItems()); $format->setItems($bridge->getItems());
$format->setExtraInfos($bridge->getExtraInfos()); $format->setExtraInfos($bridge->getExtraInfos());
$format->setLastModified($bridge->getCacheTime());
$format->display(); $format->display();
} catch(Error $e) { } catch(Error $e) {
http_response_code($e->getCode()); http_response_code($e->getCode());

View file

@ -292,4 +292,27 @@ abstract class BridgeAbstract implements BridgeInterface {
public function getCacheTimeout(){ public function getCacheTimeout(){
return isset($this->cacheTimeout) ? $this->cacheTimeout : static::CACHE_TIMEOUT; return isset($this->cacheTimeout) ? $this->cacheTimeout : static::CACHE_TIMEOUT;
} }
public function getCacheTime(){
return !is_null($this->cache) ? $this->cache->getTime() : false;
}
public function dieIfNotModified(){
if ((defined('DEBUG') && DEBUG === true)) return; // disabled in debug mode
$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false;
if (!$if_modified_since) return; // If-Modified-Since value is required
$last_modified = $this->getCacheTime();
if (!$last_modified) return; // did not detect cache time
if (time() - $this->getCacheTimeout() > $last_modified) return; // cache timeout
$last_modified = (gmdate('D, d M Y H:i:s ', $last_modified) . 'GMT');
if ($if_modified_since == $last_modified) {
header('HTTP/1.1 304 Not Modified');
die();
}
}
} }

View file

@ -7,6 +7,7 @@ abstract class FormatAbstract implements FormatInterface {
$contentType, $contentType,
$charset, $charset,
$items, $items,
$lastModified,
$extraInfos; $extraInfos;
public function setCharset($charset){ public function setCharset($charset){
@ -27,11 +28,18 @@ abstract class FormatAbstract implements FormatInterface {
return $this; return $this;
} }
public function setLastModified($lastModified){
$this->lastModified = $lastModified;
}
protected function callContentType(){ protected function callContentType(){
header('Content-Type: ' . $this->contentType); header('Content-Type: ' . $this->contentType);
} }
public function display(){ public function display(){
if ($this->lastModified) {
header('Last-Modified: ' . gmdate('D, d M Y H:i:s ', $this->lastModified) . 'GMT');
}
echo $this->stringify(); echo $this->stringify();
return $this; return $this;