Skip to content

Commit

Permalink
Cache bypass header now also prevents an already cached response from…
Browse files Browse the repository at this point in the history
… being returned (#407)

* Test that we won't return a cached response if request asks for bypass, even if cache already exists

* Refactor bypass header implementation
  • Loading branch information
fgilio authored Aug 9, 2022
1 parent af96f40 commit b9b5486
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
14 changes: 0 additions & 14 deletions src/CacheProfiles/BaseCacheProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,4 @@ public function isRunningInConsole(): bool

return app()->runningInConsole();
}

public function requestHasCacheBypassHeader(Request $request): bool
{
// Ensure we return if cache_bypass_header is not setup
if (! config('responsecache.cache_bypass_header.name')) {
return false;
}
// Ensure we return if cache_bypass_header is not setup
if (! config('responsecache.cache_bypass_header.value')) {
return false;
}

return $request->header(config('responsecache.cache_bypass_header.name')) === config('responsecache.cache_bypass_header.value');
}
}
4 changes: 0 additions & 4 deletions src/CacheProfiles/CacheAllSuccessfulGetRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ public function shouldCacheRequest(Request $request): bool
return false;
}

if ($this->requestHasCacheBypassHeader($request)) {
return false;
}

return $request->isMethod('get');
}

Expand Down
4 changes: 2 additions & 2 deletions src/Middlewares/CacheResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function handle(Request $request, Closure $next, ...$args): Response
$lifetimeInSeconds = $this->getLifetime($args);
$tags = $this->getTags($args);

if ($this->responseCache->enabled($request)) {
if ($this->responseCache->enabled($request) && ! $this->responseCache->shouldBypass($request)) {
if ($this->responseCache->hasBeenCached($request, $tags)) {
event(new ResponseCacheHit($request));

Expand All @@ -44,7 +44,7 @@ public function handle(Request $request, Closure $next, ...$args): Response

$response = $next($request);

if ($this->responseCache->enabled($request)) {
if ($this->responseCache->enabled($request) && ! $this->responseCache->shouldBypass($request)) {
if ($this->responseCache->shouldCache($request, $response)) {
$this->makeReplacementsAndCacheResponse($request, $response, $lifetimeInSeconds, $tags);
}
Expand Down
14 changes: 14 additions & 0 deletions src/ResponseCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ public function shouldCache(Request $request, Response $response): bool
return $this->cacheProfile->shouldCacheResponse($response);
}

public function shouldBypass(Request $request): bool
{
// Ensure we return if cache_bypass_header is not setup
if (! config('responsecache.cache_bypass_header.name')) {
return false;
}
// Ensure we return if cache_bypass_header is not setup
if (! config('responsecache.cache_bypass_header.value')) {
return false;
}

return $request->header(config('responsecache.cache_bypass_header.name')) === (string) config('responsecache.cache_bypass_header.value');
}

public function cacheResponse(
Request $request,
Response $response,
Expand Down
8 changes: 5 additions & 3 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,17 @@ public function it_can_add_a_cache_age_header()
}

/** @test */
public function it_wont_serve_cached_response_if_request_has_bypass_header()
public function it_wont_cache_nor_serve_a_cached_response_if_request_has_bypass_header()
{
$headerName = 'X-Cache-Bypass';
$headerValue = rand(1, 99999);
$this->app['config']->set('responsecache.cache_bypass_header.name', $headerName);
$this->app['config']->set('responsecache.cache_bypass_header.value', $headerValue);

$response = $this->get('/', ['X-Cache-Bypass' => $headerValue]);
$firstResponse = $this->get('/', ['X-Cache-Bypass' => $headerValue]);
$secondResponse = $this->get('/', ['X-Cache-Bypass' => $headerValue]);

$this->assertRegularResponse($response);
$this->assertRegularResponse($firstResponse);
$this->assertRegularResponse($secondResponse);
}
}

0 comments on commit b9b5486

Please sign in to comment.