Skip to content


Subversion checkout URL

You can clone with
Download ZIP


[HttpKernel] HttpCache not handling ttl based invalidation properly #6746

lsmith77 opened this Issue · 7 comments

2 participants


I am debugging a Symfony 2.1 app using the HttpCache and it seems to me that in every request the age remains at 0. imho we need to set a date in the response inside HttpCache::store() before we call write() to ensure that on a subsequent request we can properly compute the age of the response.


it works with this quick hack:

diff --git a/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php b/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
index ec66dcd..f6169b8 100644
--- a/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
+++ b/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
@@ -553,6 +553,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
     protected function store(Request $request, Response $response)
         try {
+            $response->setDate(new \DateTime(null, new \DateTimeZone('UTC')));
             $this->store->write($request, $response);

             $this->record($request, 'store');

but i am not sure if this is the right approach. for one for the above code path it will then do the same again inside lookup(). furthermore i wonder if it wouldnt be better to convert a maxage check into an expire check to reduce the amount of computation that needs to be done on a lookup. but that is a question independent on this issue.


ok closed #6758 since i don't think its fixing the root cause. the issue seems to be somewhere else, since the following also fixes the issue:

diff --git a/src/Symfony/Component/HttpFoundation/Response.php b/src/Symfony/Component/HttpFoundation/Response.php
index 7428b9a..abf1761 100644
--- a/src/Symfony/Component/HttpFoundation/Response.php
+++ b/src/Symfony/Component/HttpFoundation/Response.php
@@ -585,7 +585,13 @@ class Response
     public function getDate()
-        return $this->headers->getDate('Date', new \DateTime());
+        $date = $this->headers->getDate('Date');
+        if (!$date) {
+            $date = new \DateTime(null, new \DateTimeZone('UTC'));
+            $this->setDate($date);
+        }
+        return $date;


i should stress that from my analysis TTL based invalidation never worked in Symfony2. then again people claim to be using HttpCache, so i dont know if they just dont use TTL based invalidation or if there is some other factor here. as this is an urgent issue i created this temporary fix for my project:

diff --git a/app/AppKernel.php b/app/AppKernel.php
index f072268..f22047f 100644
--- a/app/AppKernel.php
+++ b/app/AppKernel.php
@@ -1,6 +1,8 @@

 use Symfony\Component\HttpKernel\Kernel;
+use Symfony\Component\HttpKernel\HttpKernelInterface;
+use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\Config\Loader\LoaderInterface;

 class AppKernel extends Kernel
@@ -63,6 +65,14 @@ class AppKernel extends Kernel
         return $bundles;

+    public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
+    {
+        $response = parent::handle($request, $type, $catch);
+        $response->setDate($response->getDate());
+        return $response;
+    }
     public function registerContainerConfiguration(LoaderInterface $loader)

I think the issue might be caused by some timezone issue?
however i then tried to set the timezone in Response::getDate() but this doesnt fix things either.


Have you tried to print the response in the handle method (echo $response) to see if the Date header is there? We have a bunch of unit tests for the Age header, so there is something special going on here.


on the first request i see:

HTTP/1.0 200 OK Age: 0 Cache-Control: max-age=300, public, s-maxage=300 Content-Length: 20925 Content-Type: text/html; charset=UTF-8 Date: Mon, 21 Jan 2013 15:58:04 GMT X-Content-Digest: enfa32f78d314a08b68a9cbb4ca6fb09aab76ca732 X-Symfony-Cache: GET /news: miss, store 

in the next request i see:

HTTP/1.0 200 OK Age: 0 Cache-Control: max-age=300, public, s-maxage=300 Content-Length: 20925 Content-Type: text/html; charset=UTF-8 Date: Mon, 21 Jan 2013 15:59:28 GMT X-Content-Digest: enfa32f78d314a08b68a9cbb4ca6fb09aab76ca732 X-Symfony-Cache: GET /news: fresh 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.