From ef42804ccec4fd7e22f66a392d0d748a34dd7e3f Mon Sep 17 00:00:00 2001 From: Franck Date: Fri, 28 Mar 2025 17:19:21 +0100 Subject: [PATCH 1/2] [WIP] Add cache status hit headers if fresh cache --- src/Symfony/Component/HttpFoundation/HeaderBag.php | 2 ++ src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php | 3 +++ .../Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php | 3 +++ 3 files changed, 8 insertions(+) diff --git a/src/Symfony/Component/HttpFoundation/HeaderBag.php b/src/Symfony/Component/HttpFoundation/HeaderBag.php index c2ede560b276f..f0af471d5c348 100644 --- a/src/Symfony/Component/HttpFoundation/HeaderBag.php +++ b/src/Symfony/Component/HttpFoundation/HeaderBag.php @@ -29,6 +29,8 @@ class HeaderBag implements \IteratorAggregate, \Countable, \Stringable protected array $headers = []; protected array $cacheControl = []; + protected array $cacheStatus = []; + public function __construct(array $headers = []) { foreach ($headers as $key => $values) { diff --git a/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php b/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php index 3ef1b8dcb821f..f9aec92119b47 100644 --- a/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php +++ b/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php @@ -335,6 +335,8 @@ protected function lookup(Request $request, bool $catch = false): Response return $this->fetch($request, $catch); } + $entry->headers->set('Cache-Status', 'SymfonyCache, hit; ttl='.$entry->getTtl()); + if (!$this->isFreshEnough($request, $entry)) { $this->record($request, 'stale'); @@ -345,6 +347,7 @@ protected function lookup(Request $request, bool $catch = false): Response return $this->validate($request, $entry, $catch); } + $this->record($request, 'hit'); $this->record($request, 'fresh'); $entry->headers->set('Age', $entry->getAge()); diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php index e82e8fd81b481..bcf08f1be8a93 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php @@ -127,6 +127,7 @@ public function testInvalidatesOnPostPutDeleteRequests() $this->assertHttpKernelIsCalled(); $this->assertResponseOk(); + $this->assertTraceContains('invalidate'); $this->assertTraceContains('pass'); } @@ -635,6 +636,8 @@ public function testHitsCachedResponseWithMaxAgeDirective() $this->assertLessThan(2, strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date'))); $this->assertGreaterThan(0, $this->response->headers->get('Age')); $this->assertNotNull($this->response->headers->get('X-Content-Digest')); + $this->assertNotNull($this->response->headers->get('Cache-Status')); + $this->assertTraceContains('hit'); $this->assertTraceContains('fresh'); $this->assertTraceNotContains('store'); $this->assertEquals('Hello World', $this->response->getContent()); From 10834e891d092c3b4711baa71679e403eab30bdf Mon Sep 17 00:00:00 2001 From: Franck Date: Tue, 24 Jun 2025 09:16:38 +0200 Subject: [PATCH 2/2] fix: remove blank lines --- src/Symfony/Component/HttpFoundation/HeaderBag.php | 1 - .../Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/HeaderBag.php b/src/Symfony/Component/HttpFoundation/HeaderBag.php index f0af471d5c348..23cabc2e67ea8 100644 --- a/src/Symfony/Component/HttpFoundation/HeaderBag.php +++ b/src/Symfony/Component/HttpFoundation/HeaderBag.php @@ -28,7 +28,6 @@ class HeaderBag implements \IteratorAggregate, \Countable, \Stringable */ protected array $headers = []; protected array $cacheControl = []; - protected array $cacheStatus = []; public function __construct(array $headers = []) diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php index da770930d3da0..a6c2b73035af6 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php @@ -128,7 +128,6 @@ public function testInvalidatesOnPostPutDeleteRequests() $this->assertHttpKernelIsCalled(); $this->assertResponseOk(); - $this->assertTraceContains('invalidate'); $this->assertTraceContains('pass'); }