forked from devsam/core
refac: API 캐시 컨트롤에서 max-age, public 지정 가능하도록
This commit is contained in:
@@ -5,6 +5,8 @@ class APICacheResult{
|
||||
function __construct(
|
||||
public ?\DateTimeInterface $lastModified = null,
|
||||
public ?string $etag = null,
|
||||
public int $validSeconds = 60,
|
||||
public bool $isPublic = false,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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}\"");
|
||||
|
||||
Reference in New Issue
Block a user