本文整理汇总了PHP中Guzzle\Http\Message\Response::getMaxAge方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::getMaxAge方法的具体用法?PHP Response::getMaxAge怎么用?PHP Response::getMaxAge使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Guzzle\Http\Message\Response
的用法示例。
在下文中一共展示了Response::getMaxAge方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: cache
public function cache(RequestInterface $request, Response $response)
{
$currentTime = time();
$ttl = $request->getParams()->get('cache.override_ttl') ?: $response->getMaxAge() ?: $this->defaultTtl;
if ($cacheControl = $response->getHeader('Cache-Control')) {
$stale = $cacheControl->getDirective('stale-if-error');
$ttl += $stale == true ? $ttl : $stale;
}
// Determine which manifest key should be used
$key = $this->getCacheKey($request);
$persistedRequest = $this->persistHeaders($request);
$entries = array();
if ($manifest = $this->cache->fetch($key)) {
// Determine which cache entries should still be in the cache
$vary = $response->getVary();
foreach (unserialize($manifest) as $entry) {
// Check if the entry is expired
if ($entry[4] < $currentTime) {
continue;
}
$entry[1]['vary'] = isset($entry[1]['vary']) ? $entry[1]['vary'] : '';
if ($vary != $entry[1]['vary'] || !$this->requestsMatch($vary, $entry[0], $persistedRequest)) {
$entries[] = $entry;
}
}
}
// Persist the response body if needed
$bodyDigest = null;
if ($response->getBody() && $response->getBody()->getContentLength() > 0) {
$bodyDigest = $this->getBodyKey($request->getUrl(), $response->getBody());
$this->cache->save($bodyDigest, (string) $response->getBody(), $ttl);
}
array_unshift($entries, array($persistedRequest, $this->persistHeaders($response), $response->getStatusCode(), $bodyDigest, $currentTime + $ttl));
$this->cache->save($key, serialize($entries));
}
示例2: cache
/**
* {@inheritdoc}
*/
public function cache($key, Response $response, $ttl = null)
{
if ($ttl === null) {
$ttl = $this->defaultTtl;
}
$ttl += $response->getMaxAge();
if ($ttl) {
$response->setHeader('X-Guzzle-Cache', "key={$key}; ttl={$ttl}");
// Remove excluded headers from the response (see RFC 2616:13.5.1)
foreach ($this->excludeResponseHeaders as $header) {
$response->removeHeader($header);
}
// Add a Date header to the response if none is set (for validation)
if (!$response->getDate()) {
$response->setHeader('Date', gmdate(ClientInterface::HTTP_DATE));
}
$this->cache->save($key, array($response->getStatusCode(), $response->getHeaders()->getAll(), $response->getBody(true)), $ttl);
}
}
示例3: canResponseSatisfyFailedRequest
/**
* Check if a cache response satisfies a failed request's caching constraints
*
* @param RequestInterface $request Request to validate
* @param Response $response Response to validate
*
* @return bool
*/
public function canResponseSatisfyFailedRequest(RequestInterface $request, Response $response)
{
$reqc = $request->getHeader('Cache-Control');
$resc = $response->getHeader('Cache-Control');
$requestStaleIfError = $reqc ? $reqc->getDirective('stale-if-error') : null;
$responseStaleIfError = $resc ? $resc->getDirective('stale-if-error') : null;
if (!$requestStaleIfError && !$responseStaleIfError) {
return false;
}
if (is_numeric($requestStaleIfError) && $response->getAge() - $response->getMaxAge() > $requestStaleIfError) {
return false;
}
if (is_numeric($responseStaleIfError) && $response->getAge() - $response->getMaxAge() > $responseStaleIfError) {
return false;
}
return true;
}
示例4: canResponseSatisfyFailedRequest
/**
* Check if a cache response satisfies a failed request's caching constraints
*
* @param RequestInterface $request Request to validate
* @param Response $response Response to validate
*
* @return bool
*/
public function canResponseSatisfyFailedRequest(RequestInterface $request, Response $response)
{
$requestStaleIfError = $request->getCacheControlDirective('stale-if-error');
$responseStaleIfError = $response->getCacheControlDirective('stale-if-error');
if (!$requestStaleIfError && !$responseStaleIfError) {
return false;
}
if ($requestStaleIfError !== true && $requestStaleIfError !== null && $response->getAge() - $response->getMaxAge() > $requestStaleIfError) {
return false;
}
if ($responseStaleIfError !== true && $responseStaleIfError !== null && $response->getAge() - $response->getMaxAge() > $responseStaleIfError) {
return false;
}
return true;
}