isCacheable() public méthode

Responses marked "private" with an explicit Cache-Control directive are considered uncacheable. Responses with neither a freshness lifetime (Expires, max-age) nor cache validator (Last-Modified, ETag) are considered uncacheable.
public isCacheable ( ) : boolean
Résultat boolean true if the response is worth caching, false otherwise
 /**
  * Update a valid non cacheable Response with http cache headers
  *
  * @see http://symfony.com/fr/doc/current/book/http_cache.html
  */
 public function handleResponse(Response $response)
 {
     // do not handle invalid response
     if (!$response->isOk()) {
         return $response;
     }
     // do not handle response with http cache headers
     if ($response->isCacheable()) {
         return $response;
     }
     // seek for optional configuration
     $this->readRoutingConfiguration();
     // mark the response as private
     $response->setPrivate();
     // set the private or shared max age
     $response->setMaxAge($this->duration);
     $response->setSharedMaxAge($this->duration);
     // set expires
     $date = new \DateTime();
     $date->modify(sprintf('+%d seconds', $this->duration));
     $response->setExpires($date);
     // set a custom Cache-Control directive
     $response->headers->addCacheControlDirective('must-revalidate', true);
     return $response;
 }
 /**
  * {@inheritdoc}
  */
 public function updateMetadata(Response $response, BlockContextInterface $blockContext = null)
 {
     if ($this->currentTtl === null) {
         $this->currentTtl = $response->getTtl();
     }
     if ($response->isCacheable() !== null && $response->getTtl() < $this->currentTtl) {
         $this->currentTtl = $response->getTtl();
     }
 }
Exemple #3
0
 private function updateHeaders(Response $response, Response $subResponse)
 {
     if ($this->response->isCacheable() && $subResponse->isCacheable()) {
         $maxAge = (int) min($response->headers->getCacheControlDirective('max-age'), $subResponse->headers->getCacheControlDirective('max-age'));
         $sMaxAge = (int) min($response->headers->getCacheControlDirective('s-maxage'), $subResponse->headers->getCacheControlDirective('s-maxage'));
         $response->setSharedMaxAge($sMaxAge);
         $response->setMaxAge($maxAge);
     } else {
         $this->response->headers->set('Cache-Control', 'no-cache, must-revalidate');
     }
 }
 /**
  * {@inheritdoc}
  */
 public function getParameters()
 {
     $code = null;
     $codeType = null;
     $cacheable = null;
     if (null !== $this->response) {
         $code = sprintf('%d', $this->response->getStatusCode());
         $cacheable = $this->response->isCacheable() ? 'cacheable' : 'not_cacheable';
         if ($this->response->isInformational()) {
             $codeType = 'informational';
         } elseif ($this->response->isSuccessful()) {
             $codeType = 'successful';
         } elseif ($this->response->isRedirection()) {
             $codeType = 'redirection';
         } elseif ($this->response->isClientError()) {
             $codeType = 'client_error';
         } elseif ($this->response->isServerError()) {
             $codeType = 'server_error';
         } else {
             $codeType = 'other';
         }
     }
     return array('response_code' => $code, 'response_code_type' => $codeType, 'response_cacheable' => $cacheable);
 }
 /**
  * This method is responsible to cascade ttl to the parent block.
  *
  * @param Response              $response
  * @param BlockContextInterface $blockContext
  * @param BlockServiceInterface $service
  *
  * @return Response
  */
 protected function addMetaInformation(Response $response, BlockContextInterface $blockContext, BlockServiceInterface $service)
 {
     // a response exists, use it
     if ($this->lastResponse && $this->lastResponse->isCacheable()) {
         $response->setTtl($this->lastResponse->getTtl());
         $response->setPublic();
     } elseif ($this->lastResponse) {
         // not cacheable
         $response->setPrivate();
         $response->setTtl(0);
         $response->headers->removeCacheControlDirective('s-maxage');
         $response->headers->removeCacheControlDirective('maxage');
     }
     // no more children available in the stack, reseting the state object
     if (!$blockContext->getBlock()->hasParent()) {
         $this->lastResponse = null;
     } else {
         // contains a parent so storing the response
         $this->lastResponse = $response;
     }
     return $response;
 }
 public function testIsCacheableWithSetTtl()
 {
     $response = new Response();
     $response->setTtl(10);
     $this->assertTrue($response->isCacheable());
 }