fix,refac: ETag 오기록 수정, Cache 설정을 WebUtil로

This commit is contained in:
2022-04-04 01:44:35 +09:00
parent b67f4daf3a
commit 31d6349d71
4 changed files with 71 additions and 50 deletions
+7 -13
View File
@@ -31,17 +31,12 @@ $now = time();
if($mapInfo && ($now - $mapInfo['timestamp'] < 600)){ if($mapInfo && ($now - $mapInfo['timestamp'] < 600)){
$mapEtag = $mapInfo['etag']; $mapEtag = $mapInfo['etag'];
$mapModified = $mapInfo['timestamp']; $mapModified = $mapInfo['timestamp'];
WebUtil::setCacheHeader(new APICacheResult(TimeUtil::secondsToDateTime($mapModified), $mapEtag));
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $mapModified)." GMT"); $reqMapEtag = WebUtil::parseETag();
header("Etag: $mapEtag"); $reqModifiedSince = WebUtil::parseLastModified();
if($mapEtag === $reqMapEtag || $mapModified === $reqModifiedSince){
if ( WebUtil::dieWithNotModified();
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']??'2000-01-01') === $mapModified || }
trim($_SERVER['HTTP_IF_NONE_MATCH']??'') === $mapEtag
) {
header("HTTP/1.1 304 Not Modified");
die();
}
Json::die($mapInfo['data'], 0); Json::die($mapInfo['data'], 0);
} }
@@ -76,7 +71,6 @@ $map = [
'data'=>$rawMap, 'data'=>$rawMap,
]; ];
$cache->save("recent_map", $map); $cache->save("recent_map", $map);
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $now)." GMT"); WebUtil::setCacheHeader(new APICacheResult(TimeUtil::secondsToDateTime($now), $etag));
header("Etag: $etag");
Json::die($map['data'], 0); Json::die($map['data'], 0);
+7
View File
@@ -281,6 +281,13 @@ class GetConst extends \sammo\BaseAPI
$storage = new \Nette\Caching\Storages\FileStorage($cacheDir); $storage = new \Nette\Caching\Storages\FileStorage($cacheDir);
$cache = new Cache($storage); $cache = new Cache($storage);
$currentCacheKey = $this->tryCache();
if($modifiedSince !== null && $currentCacheKey->lastModified == $modifiedSince){
return null;
}
if($reqEtag !== null && $currentCacheKey->etag == $reqEtag){
return null;
}
$constCache = $this->readCache($cache); $constCache = $this->readCache($cache);
if ($constCache !== null) { if ($constCache !== null) {
+15 -37
View File
@@ -9,18 +9,6 @@ class APIHelper
//static only //static only
} }
private static function setCacheHeader(){
header('cache-control: private, max-age=60');
header("Pragma: cache");
header_remove('expires');
}
private static function DieWithNotModified(): never{
static::setCacheHeader();
header("HTTP/1.1 304 Not Modified");
die();
}
public static function launch(string $rootPath, string $actionPath, array $eParams = [], bool $loadRawInput = true) public static function launch(string $rootPath, string $actionPath, array $eParams = [], bool $loadRawInput = true)
{ {
if($loadRawInput){ if($loadRawInput){
@@ -73,51 +61,41 @@ class APIHelper
} }
} }
$modifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) $modifiedSince = WebUtil::parseLastModified();
? new \DateTimeImmutable($_SERVER['HTTP_IF_MODIFIED_SINCE'], new \DateTimeZone("UTC")) $reqEtags = WebUtil::parseETag();
: null;
$reqEtags = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : null;
$result = $obj->launch($session, $modifiedSince, $reqEtags); $result = $obj->launch($session, $modifiedSince, $reqEtags);
if (is_string($result)) { if (is_string($result)) {
Json::dieWithReason($result); Json::dieWithReason($result);
} }
$cacheResult = $obj->tryCache(); $cache = $obj->tryCache();
$setCache = false; $setCache = false;
if ($cacheResult !== null) { if ($cache !== null) {
$lastModified = $cacheResult->lastModified; if($cache->lastModified !== null || $cache->etag !== null){
$etag = $cacheResult->etag;
if ($lastModified !== null) {
header("Last-Modified: " . gmdate("D, d M Y H:i:s", Util::toInt(TimeUtil::DateTimeToSeconds($lastModified, true))) . " GMT");
$setCache = true;
}
if ($etag !== null) {
header("Etag: $etag");
$setCache = true; $setCache = true;
WebUtil::setCacheHeader($cache);
} }
if ($modifiedSince !== null && $lastModified !== null && TimeUtil::DateIntervalToSeconds($modifiedSince->diff($lastModified)) == 0) {
static::DieWithNotModified(); if ($modifiedSince !== null && $cache->lastModified !== null){
$lastModifiedUnixTime = Util::toInt(TimeUtil::DateTimeToSeconds($cache->lastModified, true));
$modifiedSinceUnixTime = Util::toInt(TimeUtil::DateTimeToSeconds($modifiedSince));
if($lastModifiedUnixTime === $modifiedSinceUnixTime){
WebUtil::dieWithNotModified();
}
} }
if ($reqEtags !== null && $reqEtags === $etag) { if ($reqEtags !== null && $reqEtags === $cache->etag) {
static::DieWithNotModified(); WebUtil::dieWithNotModified();
} }
} }
if ($result === null) { if ($result === null) {
if ($setCache) {
static::setCacheHeader();
}
Json::die([ Json::die([
'result' => true, 'result' => true,
'reason' => 'success' 'reason' => 'success'
], $setCache ? 0 : Json::NO_CACHE); ], $setCache ? 0 : Json::NO_CACHE);
} }
if ($setCache) {
static::setCacheHeader();
}
Json::die($result, $setCache ? 0 : Json::NO_CACHE); Json::die($result, $setCache ? 0 : Json::NO_CACHE);
} catch (\Throwable $e) { } catch (\Throwable $e) {
Json::dieWithReason($e->getMessage()); Json::dieWithReason($e->getMessage());
+42
View File
@@ -36,6 +36,48 @@ class WebUtil
return strtolower($_SERVER['HTTP_X_REQUESTED_WITH'] ?? null) === 'xmlhttprequest'; return strtolower($_SERVER['HTTP_X_REQUESTED_WITH'] ?? null) === 'xmlhttprequest';
} }
public static function setCacheHeader(?APICacheResult $cache, int $maxAge=60){
header_remove('expires');
header("Cache-Control: private, max-age={$maxAge}");
header("Pragma: cache");
if($cache->etag !== null){
header("ETag: \"{$cache->etag}\"");
}
if($cache->lastModified !== null){
$lastModifiedUnixTime = Util::toInt(TimeUtil::DateTimeToSeconds($cache->lastModified, true));
$lastModified = gmdate("D, d M Y H:i:s", $lastModifiedUnixTime);
header("Last-Modified: $lastModified GMT");
}
}
public static function dieWithNotModified(): never{
header("HTTP/1.1 304 Not Modified");
die();
}
public static function parseETag(): ?string{
$etag = $_SERVER['HTTP_IF_NONE_MATCH']??null;
if($etag === null){
return $etag;
}
$etag = trim($etag);
if(str_starts_with($etag, 'W/"') && str_ends_with($etag, '"')){
return substr($etag, 3, strlen($etag) - 4);
}
if(str_starts_with($etag, '"') && str_ends_with($etag, '"')){
return substr($etag, 1, strlen($etag) - 2);
}
return $etag;
}
public static function parseLastModified(): ?\DateTimeInterface{
$modifiedSinceStr = $_SERVER['HTTP_IF_MODIFIED_SINCE']??null;
if($modifiedSinceStr === null){
return null;
}
return new \DateTimeImmutable($modifiedSinceStr, new \DateTimeZone("UTC"));
}
public static function requireAJAX(): void public static function requireAJAX(): void
{ {
if (!static::isAJAX()) { if (!static::isAJAX()) {