/** * * @param MvcEvent $e */ public function injectCacheHeader(MvcEvent $e) { $cacheEvent = clone $e; $cacheEvent->setTarget($this); $cacheEvent->setName(self::EVENT_DETERMINE_TTL); if ($this->varnishOptions->isCacheEnabled()) { $result = $this->getEventManager()->trigger($cacheEvent, function ($result) { return $result instanceof CachingStrategyInterface; }); /** @var CachingStrategyInterface $strategy */ $strategy = $result->last(); $ttl = $strategy->getTtl(); } else { $ttl = 0; } $headers = $e->getResponse()->getHeaders(); if ($this->varnishOptions->getDebug()) { $debugValue = isset($strategy) ? get_class($strategy) : 'caching disabled'; $debug = new GenericHeader(self::HEADER_CACHE_DEBUG, $debugValue); $headers->addHeader($debug); } $cacheControl = new CacheControl(); $directives = ['no-store' => true, 'no-cache' => true, 'must-revalidate' => true, 'post-check' => 0, 'pre-check' => 0, 's-maxage' => $ttl]; foreach ($directives as $directive => $value) { $cacheControl->addDirective($directive, $value); } $headers->addHeader($cacheControl); $this->setTtl($ttl); }
public function testOptions() { $options = new VarnishOptions(); $options->setCacheEnabled(true); $this->assertTrue($options->isCacheEnabled()); $options->setCacheableRoutes(['test/route' => 60]); $this->assertEquals(['test/route' => 60], $options->getCacheableRoutes()); $options->setPolicy(VarnishOptions::POLICY_ALLOW); $this->assertEquals(VarnishOptions::POLICY_ALLOW, $options->getPolicy()); $options->setDebug(true); $this->assertTrue($options->getDebug()); $options->setDefaultTtl(3600); $this->assertEquals(3600, $options->getDefaultTtl()); $options->setServers(['server1' => ['host' => '127.0.0.1']]); $this->assertEquals(['server1' => ['host' => '127.0.0.1']], $options->getServers()); }