Skip to content

Commit

Permalink
Refactor and support older caching http headers
Browse files Browse the repository at this point in the history
  • Loading branch information
mducharme committed Dec 5, 2018
1 parent 5569a16 commit c3b10a5
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/Charcoal/Cache/Middleware/CacheMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,26 +192,26 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
$response = $next($request, $response);

if (!$this->isResponseStatusValid($response)) {
return $response->withHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
return $this->noCacheResponse($response);
}

if (!$this->isPathIncluded($path)) {
return $response->withHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
return $this->noCacheResponse($response);
}

if ($this->isPathExcluded($path)) {
return $response->withHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
return $this->noCacheResponse($response);
}

if (!$this->isQueryIncluded($query)) {
$queryArr = $this->parseIgnoredParams($query);
if (!empty($queryArr)) {
return $response->withHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
return $this->noCacheResponse($response);
}
}

if ($this->isQueryExcluded($query)) {
return $response->withHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
return $this->noCacheResponse($response);
}

// Nothing has excluded the cache so far: add it to the pool.
Expand Down Expand Up @@ -401,4 +401,22 @@ private function parseIgnoredParams(array $queryParams)

return array_diff_key($queryParams, array_flip((array)$this->ignoredQuery));
}

/**
* Disable the HTTP cache headers
*
* - Cache-Control is the proper HTTP
* - Pragma is for HTTP 1.0 support
* - Expires is an alternative that also is supported by 1.0 proxies
*
* @param ResponseInterface $response
* @return ResponseInterface
*/
private function noCacheResponse(ResponseInterface $response)
{
return $response
->withHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
->withHeader('Pragma', 'no-cache')
->withHeader('Expires', '0');
}
}

0 comments on commit c3b10a5

Please sign in to comment.