Skip to content
This repository has been archived by the owner on Jun 27, 2022. It is now read-only.

Revalidation should allow stale response to be used #58

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
11 changes: 6 additions & 5 deletions src/Guzzle/Plugin/Cache/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,28 +230,29 @@ public function canResponseSatisfyRequest(RequestInterface $request, Response $r
$responseAge = $response->calculateAge();
$reqc = $request->getHeader('Cache-Control');
$resc = $response->getHeader('Cache-Control');
$valid = true;

// Check the request's max-age header against the age of the response
if ($reqc && $reqc->hasDirective('max-age') &&
$responseAge > $reqc->getDirective('max-age')) {
return false;
$valid = false;
}

// Check the response's max-age header
if ($response->isFresh() === false) {
$maxStale = $reqc ? $reqc->getDirective('max-stale') : null;
if (null !== $maxStale) {
if ($maxStale !== true && $response->getFreshness() < (-1 * $maxStale)) {
return false;
$valid = false;
}
} elseif ($resc && $resc->hasDirective('max-age')
&& $responseAge > $resc->getDirective('max-age')
) {
return false;
$valid = false;
}
}

if ($this->revalidation->shouldRevalidate($request, $response)) {
if (!$valid && $this->revalidation->shouldRevalidate($request, $response)) {
try {
return $this->revalidation->revalidate($request, $response);
} catch (CurlException $e) {
Expand All @@ -260,7 +261,7 @@ public function canResponseSatisfyRequest(RequestInterface $request, Response $r
}
}

return true;
return $valid;
}

/**
Expand Down