private function handle200Response(RequestInterface $request, ResponseInterface $validateResponse, CompleteEvent $event) { // Store the 200 response in the cache if possible if (Utils::canCacheResponse($validateResponse)) { $this->storage->cache($request, $validateResponse); } $event->intercept($validateResponse); }
private function addResponseHeaders(RequestInterface $request, ResponseInterface $response) { $params = $request->getConfig(); $lookup = $params['cache_lookup'] . ' from GuzzleCache'; $response->addHeader('X-Cache-Lookup', $lookup); if ($params['cache_hit'] === true) { $response->addHeader('X-Cache', 'HIT from GuzzleCache'); } elseif ($params['cache_hit'] == 'error') { $response->addHeader('X-Cache', 'HIT_ERROR from GuzzleCache'); } else { $response->addHeader('X-Cache', 'MISS from GuzzleCache'); } $freshness = Utils::getFreshness($response); // Only add a Warning header if we are returning a stale response. if ($params['cache_hit'] && $freshness !== null && $freshness <= 0) { $response->addHeader('Warning', sprintf('%d GuzzleCache/' . ClientInterface::VERSION . ' "%s"', 110, 'Response is stale')); } }
private function getTtl(ResponseInterface $response) { $ttl = 0; if ($cacheControl = $response->getHeader('Cache-Control')) { $stale = Utils::getDirective($response, 'stale-if-error'); $ttl += $stale == true ? $ttl : $stale; $ttl += Utils::getDirective($response, 'max-age'); } return $ttl ?: $this->defaultTtl; }
/** * Return the TTL to use when caching a Response (extracted from guzzle/cache-subscriber). * * @param ResponseInterface $response Guzzle 4/5 response message. * * @link https://github.com/guzzle/cache-subscriber/blob/master/src/CacheStorage.php * * @return int */ private function getTtl(ResponseInterface $response) { $ttl = 0; if ($response->getHeader('Cache-Control')) { return $ttl; } $maxAge = Utils::getDirective($response, 'max-age'); if (is_numeric($maxAge)) { $ttl += $maxAge; } // According to RFC5861 stale headers are *in addition* to any // max-age values. $stale = Utils::getDirective($response, 'stale-if-error'); if (is_numeric($stale)) { $ttl += $stale; } return $ttl; }
public static function isResponseValid(RequestInterface $request, ResponseInterface $response) { $responseAge = Utils::getResponseAge($response); $maxAge = Utils::getDirective($response, 'max-age'); // Increment the age based on the X-Guzzle-Cache-Date if ($cacheDate = $response->getHeader('X-Guzzle-Cache-Date')) { $responseAge += time() - strtotime($cacheDate); $response->setHeader('Age', $responseAge); } // Check the request's max-age header against the age of the response if ($maxAge !== null && $responseAge > $maxAge) { return false; } // Check the response's max-age header against the freshness level $freshness = Utils::getFreshness($response); if ($freshness !== null) { $maxStale = Utils::getDirective($request, 'max-stale'); if ($maxStale !== null) { if ($freshness < -1 * $maxStale) { return false; } } elseif ($maxAge !== null && $responseAge > $maxAge) { return false; } } return true; }
public static function isResponseValid(RequestInterface $request, ResponseInterface $response) { $responseAge = Utils::getResponseAge($response); $maxAge = Utils::getDirective($response, 'max-age'); // Check the request's max-age header against the age of the response if ($maxAge !== null && $responseAge > $maxAge) { return false; } // Check the response's max-age header against the freshness level $freshness = Utils::getFreshness($response); if ($freshness !== null) { $maxStale = Utils::getDirective($request, 'max-stale'); if ($maxStale !== null) { if ($freshness < -1 * $maxStale) { return false; } } elseif ($maxAge !== null && $responseAge > $maxAge) { return false; } } return true; }
/** * Return the TTL to use when caching a Response. * * @param ResponseInterface $response The response being cached. * * @return int The TTL in seconds. */ private function getTtl(ResponseInterface $response) { $ttl = 0; if ($cacheControl = $response->getHeader('Cache-Control')) { $maxAge = Utils::getDirective($response, 'max-age'); if (is_numeric($maxAge)) { $ttl += $maxAge; } // According to RFC5861 stale headers are *in addition* to any // max-age values. $stale = Utils::getDirective($response, 'stale-if-error'); if (is_numeric($stale)) { $ttl += $stale; } } elseif ($expires = $response->getHeader('Expires')) { $ttl += strtotime($expires) - time(); } return $ttl ?: $this->defaultTtl; }
/** * Test that responses that can't have freshness determined return null. */ public function testGetFreshnessNull() { $messageFactory = new MessageFactory(); $response = $messageFactory->createResponse(200); $this->assertSame(null, Utils::getFreshness($response)); }