This method adjusts the Cache-Control/max-age directive.
public setClientTtl ( integer $seconds ) : |
||
$seconds | integer | Number of seconds |
return |
public function indexAction() { $response = new Response($this->dumper->dump(), 200, array('Content-Type' => 'application/xml')); $response->setPublic(); $response->setClientTtl($this->httpCache['ttl']); return $response; }
/** * Return the response to the context hash request with a header containing * the generated hash. * * If the ttl is bigger than 0, cache headers will be set for this response. * * @param GetResponseEvent $event */ public function onKernelRequest(GetResponseEvent $event) { if ($event->getRequestType() != HttpKernelInterface::MASTER_REQUEST) { return; } if (!$this->requestMatcher->matches($event->getRequest())) { return; } $hash = $this->hashGenerator->generateHash(); // status needs to be 200 as otherwise varnish will not cache the response. $response = new Response('', 200, array($this->hashHeader => $hash, 'Content-Type' => 'application/vnd.fos.user-context-hash')); if ($this->ttl > 0) { $response->setClientTtl($this->ttl); $response->setVary($this->userIdentifierHeaders); $response->setPublic(); } else { $response->setClientTtl(0); $response->headers->addCacheControlDirective('no-cache'); } $event->setResponse($response); }
public function testSetClientTtl() { $response = new Response(); $response->setClientTtl(10); $this->assertEquals($response->getMaxAge(), $response->getAge() + 10); }
public function rssAction($uri = null) { if (null === $this->application) { throw new FrontControllerException('A valid BackBee application is required.', FrontControllerException::INTERNAL_ERROR); } if (false === $this->application->getContainer()->has('site')) { throw new FrontControllerException('A BackBee\\Site instance is required.', FrontControllerException::INTERNAL_ERROR); } $site = $this->application->getContainer()->get('site'); if (false !== ($ext = strrpos($uri, '.'))) { $uri = substr($uri, 0, $ext); } if ('_root_' == $uri) { $page = $this->application->getEntityManager()->getRepository('BackBee\\NestedNode\\Page')->getRoot($site); } else { $page = $this->application->getEntityManager()->getRepository('BackBee\\NestedNode\\Page')->findOneBy(array('_site' => $site, '_url' => '/' . $uri, '_state' => Page::getUndeletedStates())); } try { $this->application->info(sprintf('Handling URL request `rss%s`.', $uri)); $response = new Response($this->application->getRenderer()->render($page, 'rss', null, 'rss.phtml', false)); $response->headers->set('Content-Type', 'text/xml'); $response->setClientTtl(15); $response->setTtl(15); $this->send($response); } catch (\Exception $e) { $this->defaultAction('/rss/' . $uri); } }