Commit 8cc3eea8 authored by alexpott's avatar alexpott

Issue #2795391 by effulgentsia: Move most of PageCache::fetch() into a...

Issue #2795391 by effulgentsia: Move most of PageCache::fetch() into a storeResponse() helper that can be independently invoked from a subclass
parent 27b61a9b
......@@ -206,6 +206,26 @@ protected function fetch(Request $request, $type = self::MASTER_REQUEST, $catch
/** @var \Symfony\Component\HttpFoundation\Response $response */
$response = $this->httpKernel->handle($request, $type, $catch);
// Only set the 'X-Drupal-Cache' header if caching is allowed for this
// response.
if ($this->storeResponse($request, $response)) {
$response->headers->set('X-Drupal-Cache', 'MISS');
}
return $response;
}
/**
* Stores a response in the page cache.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* A request object.
* @param \Symfony\Component\HttpFoundation\Response $response
* A response object that should be stored in the page cache.
*
* @returns bool
*/
protected function storeResponse(Request $request, Response $response) {
// Drupal's primary cache invalidation architecture is cache tags: any
// response that varies by a configuration value or data in a content
// entity should have cache tags, to allow for instant cache invalidation
......@@ -228,7 +248,7 @@ protected function fetch(Request $request, $type = self::MASTER_REQUEST, $catch
// so by replacing/extending this middleware service or adding another
// one.
if (!$response instanceof CacheableResponseInterface) {
return $response;
return FALSE;
}
// Currently it is not possible to cache binary file or streamed responses:
......@@ -236,12 +256,12 @@ protected function fetch(Request $request, $type = self::MASTER_REQUEST, $catch
// Therefore exclude them, even for subclasses that implement
// CacheableResponseInterface.
if ($response instanceof BinaryFileResponse || $response instanceof StreamedResponse) {
return $response;
return FALSE;
}
// Allow policy rules to further restrict which responses to cache.
if ($this->responsePolicy->check($response, $request) === ResponsePolicyInterface::DENY) {
return $response;
return FALSE;
}
$request_time = $request->server->get('REQUEST_TIME');
......@@ -273,10 +293,7 @@ protected function fetch(Request $request, $type = self::MASTER_REQUEST, $catch
$this->set($request, $response, $expire, $tags);
}
// Mark response as a cache miss.
$response->headers->set('X-Drupal-Cache', 'MISS');
return $response;
return TRUE;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment