Skip to content

Instantly share code, notes, and snippets.

@mtymek
Last active August 30, 2015 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtymek/d6a422f787098532c084 to your computer and use it in GitHub Desktop.
Save mtymek/d6a422f787098532c084 to your computer and use it in GitHub Desktop.
CachingMiddleware
<?php
class CachingMiddleware
{
protected $cacheDir = 'data/cache/pages';
protected function getCacheFile(RequestInterface $request)
{
$uri = $request->getUri();
return 'cached-'
. trim($uri->getPath(), '/')
. ($uri->getQuery() ? '?' . $uri->getQuery() : '')
. '.html';
}
private function getCachedHtml(RequestInterface $request)
{
$fullPath = $this->cacheDir . '/' . $this->getCacheFile($request);
if (file_exists($fullPath) && filemtime($fullPath) > time() - 4 * 3600) {
return file_get_contents($fullPath);
}
return null;
}
private function cacheResponse(RequestInterface $request, ResponseInterface $response)
{
$cacheFilePath = $this->cacheDir . '/' . $this->getCacheFile($request);
if (!file_exists(dirname($cacheFilePath))) {
mkdir(dirname($cacheFilePath), 0777, true);
}
file_put_contents($cacheFilePath, $response->getBody()->__toString());
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
if ($request->getMethod() != 'GET') {
return $next($request, $response);
}
if ($html = $this->getCachedHtml($request)) {
return new HtmlResponse($html);
}
/** @var ResponseInterface $response */
$response = $next($request, $response);
if ($response->getStatusCode() == 200) {
$this->cacheResponse($request, $response);
}
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment