當前位置: 首頁>>代碼示例>>PHP>>正文


PHP RequestInterface::hasCacheControlDirective方法代碼示例

本文整理匯總了PHP中Guzzle\Http\Message\RequestInterface::hasCacheControlDirective方法的典型用法代碼示例。如果您正苦於以下問題:PHP RequestInterface::hasCacheControlDirective方法的具體用法?PHP RequestInterface::hasCacheControlDirective怎麽用?PHP RequestInterface::hasCacheControlDirective使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Guzzle\Http\Message\RequestInterface的用法示例。


在下文中一共展示了RequestInterface::hasCacheControlDirective方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: canResponseSatisfyRequest

 /**
  * Check if a cache response satisfies a request's caching constraints
  *
  * @param RequestInterface $request  Request to validate
  * @param Response         $response Response to validate
  *
  * @return bool
  */
 public function canResponseSatisfyRequest(RequestInterface $request, Response $response)
 {
     $responseAge = $response->getAge();
     // Check the request's max-age header against the age of the response
     if ($request->hasCacheControlDirective('max-age') && $responseAge > $request->getCacheControlDirective('max-age')) {
         return false;
     }
     // Check the response's max-age header
     if ($response->isFresh() === false) {
         $maxStale = $request->getCacheControlDirective('max-stale');
         if (null !== $maxStale) {
             if ($maxStale !== true && $response->getFreshness() < -1 * $maxStale) {
                 return false;
             }
         } elseif ($response->hasCacheControlDirective('max-age') && $responseAge > $response->getCacheControlDirective('max-age')) {
             return false;
         }
     }
     // Only revalidate GET requests
     if ($request->getMethod() == RequestInterface::GET) {
         // Check if the response must be validated against the origin server
         if ($request->getHeader('Pragma') == 'no-cache' || $request->hasCacheControlDirective('no-cache') || $request->hasCacheControlDirective('must-revalidate') || $response->hasCacheControlDirective('must-revalidate') || $response->hasCacheControlDirective('no-cache')) {
             // no-cache: When no parameters are present, always revalidate
             // When parameters are present in no-cache and the request includes those same parameters, then the
             // response must re-validate. I'll need an example of what fields look like in order to implement a
             // smarter version of no-cache
             // Requests can decline to revalidate against the origin server by setting the cache.revalidate param:
             // - never - To never revalidate and always contact the origin server
             // - skip  - To skip revalidation and just use what is in cache
             switch ($request->getParams()->get('cache.revalidate')) {
                 case 'never':
                     return false;
                 case 'skip':
                     return true;
                 default:
                     return $this->revalidation->revalidate($request, $response);
             }
         }
     }
     return true;
 }
開發者ID:KANU82,項目名稱:guzzle,代碼行數:49,代碼來源:CachePlugin.php


注:本文中的Guzzle\Http\Message\RequestInterface::hasCacheControlDirective方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。