Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache bypass header now also prevents an already cached response from being returned #407

Merged
merged 2 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactor bypass header implementation
  • Loading branch information
fgilio committed Aug 8, 2022
commit 456be005fcee797a1dd47259c7106badb23d0d64
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