public function testUpdateResponse() { $this->response->setPublic()->shouldBeCalled(); $this->response->setMaxAge($this->maxAge)->shouldBeCalled(); $this->response->setSharedMaxAge($this->sharedMaxAge)->shouldBeCalled(); $this->structure->getCacheLifeTime()->willReturn(10); $this->response->getAge()->willReturn(50); $this->handler->updateResponse($this->response->reveal(), $this->structure->reveal()); }
/** * {@inheritDoc} */ public function updateResponse(Response $response, StructureInterface $structure) { if (!$structure instanceof PageInterface) { return; } // mark the response as either public or private $response->setPublic(); // set the private and shared max age $response->setMaxAge($this->maxAge); $response->setSharedMaxAge($this->sharedMaxAge); $proxyTtl = $this->usePageTtl ? $response->getAge() + intval($structure->getCacheLifeTime()) : $response->getAge(); // set reverse-proxy TTL (Symfony HttpCache, Varnish, ...) $response->headers->set(HttpCache::HEADER_REVERSE_PROXY_TTL, $proxyTtl); }
/** * Returns the response reverse-proxy cache TTL in seconds. * * @param Response $response * * @return int|null The TTL in seconds */ private function getReverseProxyTtl(Response $response) { if (null !== ($maxAge = $response->headers->get(self::HEADER_REVERSE_PROXY_TTL))) { return $maxAge - $response->getAge(); } return; }
/** * {@inheritdoc} */ public function updateResponse(Response $response, StructureInterface $structure) { if (!$structure instanceof PageInterface) { return; } $cacheLifetimeData = $structure->getCacheLifeTime(); $cacheLifetime = $this->cacheLifetimeResolver->resolve($cacheLifetimeData['type'], $cacheLifetimeData['value']); // when structure cache-lifetime disabled - return if (0 === $cacheLifetime) { return; } // mark the response as either public or private $response->setPublic(); // set the private and shared max age $response->setMaxAge($this->maxAge); $response->setSharedMaxAge($this->sharedMaxAge); $proxyTtl = $this->usePageTtl ? $response->getAge() + $cacheLifetime : $response->getAge(); // set reverse-proxy TTL (Symfony HttpCache, Varnish, ...) $response->headers->set(HttpCache::HEADER_REVERSE_PROXY_TTL, $proxyTtl); }
/** * Returns a rendered xmlsitemap. * * @return Response */ public function indexAction(Request $request) { /** @var RequestAnalyzerInterface $requestAnalyzer */ $requestAnalyzer = $this->get('sulu_core.webspace.request_analyzer'); /** @var SitemapXMLGeneratorInterface $sitemapXMLGenerator */ $sitemapXMLGenerator = $this->get('sulu_website.sitemap_xml_generator'); $sitemap = $this->get('sulu_content.content_repository')->findAll($requestAnalyzer->getWebspace()->getXDefaultLocalization()->getLocalization(), $requestAnalyzer->getWebspace()->getKey(), MappingBuilder::create()->addProperties(['changed'])->setResolveUrl(true)->getMapping()); $webspaceSitemaps = [['localizations' => array_map(function (Localization $localization) { return $localization->getLocalization(); }, $requestAnalyzer->getWebspace()->getAllLocalizations()), 'defaultLocalization' => $requestAnalyzer->getWebspace()->getXDefaultLocalization()->getLocalization(), 'sitemap' => $sitemap]]; $preferredDomain = $request->getHttpHost(); // XML Response $response = new Response(); $response->setMaxAge(240); $response->setSharedMaxAge(960); $response->headers->set(HttpCache::HEADER_REVERSE_PROXY_TTL, $response->getAge() + $this->container->getParameter('sulu_website.sitemap.cache.lifetime')); $response->headers->set('Content-Type', 'text/xml'); $response->setContent($sitemapXMLGenerator->generate($webspaceSitemaps, $preferredDomain, $request->getScheme())); // Generate XML return $response; }
/** * Returns a rendered xmlsitemap. * * @return Response */ public function indexAction(Request $request) { /** @var RequestAnalyzerInterface $requestAnalyzer */ $requestAnalyzer = $this->get('sulu_core.webspace.request_analyzer'); /** @var SitemapGeneratorInterface $sitemapGenerator */ $sitemapGenerator = $this->get('sulu_website.sitemap'); /** @var SitemapXMLGeneratorInterface $sitemapXMLGenerator */ $sitemapXMLGenerator = $this->get('sulu_website.sitemap_xml_generator'); $flatSitemap = true; $webspaceSitemaps = [$sitemapGenerator->generateAllLocals($requestAnalyzer->getWebspace()->getKey(), $flatSitemap)]; $preferredDomain = $request->getHttpHost(); // XML Response $response = new Response(); $response->setMaxAge(240); $response->setSharedMaxAge(960); $response->headers->set(HttpCache::HEADER_REVERSE_PROXY_TTL, $response->getAge() + $this->container->getParameter('sulu_website.sitemap.cache.lifetime')); $response->headers->set('Content-Type', 'text/xml'); $response->setContent($sitemapXMLGenerator->generate($webspaceSitemaps, $preferredDomain)); // Generate XML return $response; }
public function testSetClientTtl() { $response = new Response(); $response->setClientTtl(10); $this->assertEquals($response->getMaxAge(), $response->getAge() + 10); }
/** * Set cache headers. * * @param Response $response * * @return Response */ private function setCacheLifetime(Response $response) { $response->headers->set(HttpCache::HEADER_REVERSE_PROXY_TTL, $response->getAge() + $this->container->getParameter('sulu_website.sitemap.cache.lifetime')); return $response->setMaxAge(240)->setSharedMaxAge(960); }