Article

Using setSharedMaxAge() with HttpCache in Symfony2 always returns s-maxage=0

When implementing HttpCache within one of our Symfony2 projects I came across a curious problem in that regardless what I set the shared max age to it would always show as s-maxage=0 in the network inspector and the cache result was always a miss.  This puzzled me and so to try and isolate the issue I broke it down to the most basic example I could, but even then I still experienced the same s-maxage=0 problem.

My simple example was

public function indexAction()
{
   $response = $this->render('HtgPageBundle:Default:fullPage.html"https://symfony.com/doc/current/book/http_cache.html">Symfony2 documentation as this is being done automatically within the setSharedMaxAge() method. Still, regardless of what value I tried to set the shared max age to, I always got a response like:-

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

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

Evenutally I posted our problem onto the Symfony2 issue list on github and got a response from Jakub Zalas which confirmed that the Symfony2 framework was not the cause of the issue (which I was able to verify in the same manner as Jakub) and he helpfully suggested that an event listener may be causing the issue.

Although this gave me some direction to where to look the problem was it could be one of our custom event listeners or an event listener in any of the 3rd party bundles which we had included within the project.

Next I took a closer look at what the logs in the Symfony2 profiler was telling me to see if I could identify an event listener which was causing the problem and I came across the following line

DEBUG - Notified event "kernel.response" to listener "Sonata\BlockBundle\Cache\HttpCacheHandler::onKernelResponse".

This method is supposed to be resetting the ttl of the current response to the ttl of the sonata block, however I’m not using a sonata block in this request so it appears to be resetting the ttl for every request rather than just requests related to sonata blocks.  I found this had already been raised as an issue in the Sonata Block Bundle github respository and a solution was also posted there which was to disable the cache listener for the Sonata Block Bundle.

sonata_block:
    http_cache:
        listener: false

Now the s-maxage shows as 60 in the network inspector as I had intended in my example above and I can see shared cached responses returned my subsequent requests from different clients.

me.jpeg

I am a web developer specialising in web driven applications using PHP, MySQL, Symfony and Zend and I am currently working for the The Drum in Glasgow, Scotland.

Most days I can be found frantically coding away with EuroDance in my ears and consuming what I hope to be a never ending supply of coffee... happy days!

Want more like this?

Keep up-to-date with latest news from Inside the Agency using any of the following services

Advertisement

Digital Ocean

Chris's Reading List

www.thoughtworks.com

BFF @ SoundCloud

Our friends at SoundCloud embarked on a journey a few years ago that changed their system and moved it from a monolithic Rails application towards an ecosystem of microservices. Read more

The Drum

The Drum

Google'€™s AMP is bringing some much needed speed to mobile but the transition won'€™t be easy for publishers

A week after launching, the Google-led Accelerated Mobile Pages (AMP) initiative has raised more questions than answers for publishers... Read more

SitePoint

SitePoint

Quick Tip: How to Permanently Change SQL Mode in MySQL

MySQL 5.7 introduced some awkward changes for older codebases and tends to break apps. Here's how to temporarily (or permanently) lower the strictness level Read more

SitePoint

SitePoint

How to Build Your Own Dependency Injection Container

Andrew Carter builds a Dependency Injection container from scratch, showing you how that rocket science is really just fireworks strapped to lego figures! Read more

AuthorityLabs

AuthorityLabs

13 Reasons Your Organic Traffic is in Decline | It's Not a Penalty

The landscape of search engine optimization has changed drastically in the last 3-4 years. Even as it changes, a lot stays the same. It has been very challenging to see a multitude of friends and colleagues approach me with problems that often seem beyond repair with clients that they have worked with for years. Read more

TechCrunch

TechCrunch

WordPress Sites Now Support Google’s AMP To Make Mobile Pages Load Much Faster

Google has some big plans when it comes to making the web faster on your mobile phone. The company just added AMP-enabled pages in its mobile search results... Read more