Symfony2 HttpCache setSharedMaxAge() always 0

977 views Asked by At

I'm trying to get my Symfony2 application to cache a page using the expiration model but it doesn't see to be setting the s-maxage properly and the browser always returns s-maxage=0 regardless what value I set it to.

I'm currently using

public function indexAction()
{
    $response = $this->render('HtgPageBundle:Default:fullPage.html.twig');
    $response->setMaxAge(60);
    $response->setSharedMaxAge(60);

    return $response;
}

I've not set used $response->setPublic() as this is being done automatically using the setSharedMaxAge().

The response I'm getting from the browser is always

Cache-Control:max-age=60, public, s-maxage=0
X-Symfony-Cache:GET /page/privacy-policy: miss

But I was expecting:-

Cache-Control:max-age=60, public, s-maxage=60
X-Symfony-Cache:GET /page/privacy-policy: store

Digging into it I found that setSharedMaxAge is getting called twice, once when I'm calling it as above, and at this point it is setting the right value. Subsequently it's getting called again via the Symfony\Component\HttpFoundation\Response::setTtl() method but here it's setting the value to 0 but I'm not sure why it's doing it.

I'm using Symfony v2.4.4

Any suggestions

2

There are 2 answers

0
mremi On BEST ANSWER
0
Chris Shennan On

Turns out this issue was being introduced by a event listener within the Sonata Block Bundle. You can disable to cache for the sonata block bundle by adding the following to your config.yml file

sonata_block:
    http_cache:
        listener: false

Full details - http://www.mist-labs.com/blog/using-setsharedmaxage-with-httpcache-in-symfony2-always-returns-s-maxage-0