-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract exception and cache middleware (#4248)
- Loading branch information
Showing
8 changed files
with
99 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
class CacheMiddleware implements Middleware | ||
{ | ||
private CacheInterface $cache; | ||
|
||
public function __construct(CacheInterface $cache) | ||
{ | ||
$this->cache = $cache; | ||
} | ||
|
||
public function __invoke(Request $request, $next): Response | ||
{ | ||
$action = $request->attribute('action'); | ||
|
||
if ($action !== 'DisplayAction') { | ||
// We only cache DisplayAction (for now) | ||
return $next($request); | ||
} | ||
|
||
// TODO: might want to remove som params from query | ||
$cacheKey = 'http_' . json_encode($request->toArray()); | ||
$cachedResponse = $this->cache->get($cacheKey); | ||
|
||
if ($cachedResponse) { | ||
$ifModifiedSince = $request->server('HTTP_IF_MODIFIED_SINCE'); | ||
$lastModified = $cachedResponse->getHeader('last-modified'); | ||
if ($ifModifiedSince && $lastModified) { | ||
$lastModified = new \DateTimeImmutable($lastModified); | ||
$lastModifiedTimestamp = $lastModified->getTimestamp(); | ||
$modifiedSince = strtotime($ifModifiedSince); | ||
// TODO: \DateTimeImmutable can be compared directly | ||
if ($lastModifiedTimestamp <= $modifiedSince) { | ||
$modificationTimeGMT = gmdate('D, d M Y H:i:s ', $lastModifiedTimestamp); | ||
return new Response('', 304, ['last-modified' => $modificationTimeGMT . 'GMT']); | ||
} | ||
} | ||
return $cachedResponse; | ||
} | ||
|
||
/** @var Response $response */ | ||
$response = $next($request); | ||
|
||
if (in_array($response->getCode(), [403, 429, 500, 503])) { | ||
// Cache these responses for about ~20 mins on average | ||
$this->cache->set($cacheKey, $response, 60 * 15 + rand(1, 60 * 10)); | ||
} | ||
|
||
// For 1% of requests, prune cache | ||
if (rand(1, 100) === 1) { | ||
// This might be resource intensive! | ||
$this->cache->prune(); | ||
} | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
class ExceptionMiddleware implements Middleware | ||
{ | ||
private Logger $logger; | ||
|
||
public function __construct(Logger $logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
public function __invoke(Request $request, $next): Response | ||
{ | ||
try { | ||
return $next($request); | ||
} catch (\Throwable $e) { | ||
$this->logger->error('Exception in ExceptionMiddleware', ['e' => $e]); | ||
|
||
return new Response(render(__DIR__ . '/../templates/exception.html.php', ['e' => $e]), 500); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,6 @@ | |
</head> | ||
|
||
<body> | ||
|
||
<div class="container"> | ||
|
||
<h1 class="pagetitle"> | ||
|