From 49cbf9c5bf14be5ee490d9cee5244be16017c310 Mon Sep 17 00:00:00 2001 From: Hide_D Date: Thu, 21 Apr 2022 01:17:20 +0900 Subject: [PATCH] =?UTF-8?q?refac:=20API=20=EC=BA=90=EC=8B=9C=20=EC=BB=A8?= =?UTF-8?q?=ED=8A=B8=EB=A1=A4=EC=97=90=EC=84=9C=20max-age,=20public=20?= =?UTF-8?q?=EC=A7=80=EC=A0=95=20=EA=B0=80=EB=8A=A5=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sammo/APICacheResult.php | 2 ++ src/sammo/TimeUtil.php | 10 ++++++++++ src/sammo/WebUtil.php | 5 +++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/sammo/APICacheResult.php b/src/sammo/APICacheResult.php index c5dc01b9..8713dfea 100644 --- a/src/sammo/APICacheResult.php +++ b/src/sammo/APICacheResult.php @@ -5,6 +5,8 @@ class APICacheResult{ function __construct( public ?\DateTimeInterface $lastModified = null, public ?string $etag = null, + public int $validSeconds = 60, + public bool $isPublic = false, ) { } diff --git a/src/sammo/TimeUtil.php b/src/sammo/TimeUtil.php index c441af5a..29b10b54 100644 --- a/src/sammo/TimeUtil.php +++ b/src/sammo/TimeUtil.php @@ -181,6 +181,16 @@ class TimeUtil return $seconds; } + public static function nowDateTime(): \DateTime{ + $now = time(); + return static::secondsToDateTime($now, false, true); + } + + public static function nowDateTimeImmutable(): \DateTimeImmutable{ + $now = time(); + return static::secondsToDateTime($now, true, true); + } + public static function format(\DateTimeInterface $dateTime, bool $withFraction): string{ if (!$withFraction) { return $dateTime->format('Y-m-d H:i:s'); diff --git a/src/sammo/WebUtil.php b/src/sammo/WebUtil.php index caed96a9..d1a69bad 100644 --- a/src/sammo/WebUtil.php +++ b/src/sammo/WebUtil.php @@ -36,9 +36,10 @@ class WebUtil return strtolower($_SERVER['HTTP_X_REQUESTED_WITH'] ?? null) === 'xmlhttprequest'; } - public static function setCacheHeader(?APICacheResult $cache, int $maxAge=60){ + public static function setCacheHeader(?APICacheResult $cache){ + $control = $cache->isPublic?'public':'private'; header_remove('expires'); - header("Cache-Control: private, max-age={$maxAge}"); + header("Cache-Control: {$control}, max-age={$cache->validSeconds}"); header("Pragma: cache"); if($cache->etag !== null){ header("ETag: \"{$cache->etag}\"");