[core] Returning 304 http code when returning cached data (#793)
This commit is contained in:
parent
059656c370
commit
422c125d8e
3 changed files with 33 additions and 0 deletions
|
@ -195,6 +195,7 @@ try {
|
|||
try {
|
||||
$bridge->setCache($cache);
|
||||
$bridge->setCacheTimeout($cache_timeout);
|
||||
$bridge->dieIfNotModified();
|
||||
$bridge->setDatas($params);
|
||||
} catch(Error $e) {
|
||||
http_response_code($e->getCode());
|
||||
|
@ -211,6 +212,7 @@ try {
|
|||
$format = Format::create($format);
|
||||
$format->setItems($bridge->getItems());
|
||||
$format->setExtraInfos($bridge->getExtraInfos());
|
||||
$format->setLastModified($bridge->getCacheTime());
|
||||
$format->display();
|
||||
} catch(Error $e) {
|
||||
http_response_code($e->getCode());
|
||||
|
|
|
@ -292,4 +292,27 @@ abstract class BridgeAbstract implements BridgeInterface {
|
|||
public function getCacheTimeout(){
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ abstract class FormatAbstract implements FormatInterface {
|
|||
$contentType,
|
||||
$charset,
|
||||
$items,
|
||||
$lastModified,
|
||||
$extraInfos;
|
||||
|
||||
public function setCharset($charset){
|
||||
|
@ -27,11 +28,18 @@ abstract class FormatAbstract implements FormatInterface {
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function setLastModified($lastModified){
|
||||
$this->lastModified = $lastModified;
|
||||
}
|
||||
|
||||
protected function callContentType(){
|
||||
header('Content-Type: ' . $this->contentType);
|
||||
}
|
||||
|
||||
public function display(){
|
||||
if ($this->lastModified) {
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s ', $this->lastModified) . 'GMT');
|
||||
}
|
||||
echo $this->stringify();
|
||||
|
||||
return $this;
|
||||
|
|
Loading…
Reference in a new issue