From 75215971675fe566734ec7a983dcdcf3e5ac5339 Mon Sep 17 00:00:00 2001 From: Sergey K Date: Sat, 27 Dec 2025 01:15:53 +0500 Subject: [PATCH 1/3] feat: Added support for HTTP status in ResponseCache --- system/Cache/ResponseCache.php | 11 ++++++++++- tests/system/Cache/ResponseCacheTest.php | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/system/Cache/ResponseCache.php b/system/Cache/ResponseCache.php index 2c92dc5058be..b7afa3ec28a7 100644 --- a/system/Cache/ResponseCache.php +++ b/system/Cache/ResponseCache.php @@ -102,7 +102,12 @@ public function make(CLIRequest|IncomingRequest $request, ResponseInterface $res return $this->cache->save( $this->generateCacheKey($request), - serialize(['headers' => $headers, 'output' => $response->getBody()]), + serialize([ + 'headers' => $headers, + 'output' => $response->getBody(), + 'statuscode' => $response->getStatusCode(), + 'reasonphrase' => $response->getReasonPhrase(), + ]), $this->ttl, ); } @@ -140,6 +145,10 @@ public function get(CLIRequest|IncomingRequest $request, ResponseInterface $resp $response->setBody($output); + if (isset($cachedResponse['statuscode'])) { + $response->setStatusCode($cachedResponse['statuscode'], $cachedResponse['reasonphrase'] ?? ''); + } + return $response; } diff --git a/tests/system/Cache/ResponseCacheTest.php b/tests/system/Cache/ResponseCacheTest.php index b3f0ec832905..198caefbc015 100644 --- a/tests/system/Cache/ResponseCacheTest.php +++ b/tests/system/Cache/ResponseCacheTest.php @@ -107,6 +107,23 @@ public function testCachePageIncomingRequest(): void $this->assertNotInstanceOf(ResponseInterface::class, $cachedResponse); } + public function testCachePageIncomingRequestWithStatus(): void + { + $pageCache = $this->createResponseCache(); + + $response = new Response(new App()); + $response->setStatusCode(432, 'Foo Bar'); + $response->setBody('The response body.'); + + $this->assertTrue($pageCache->make($this->createIncomingRequest('foo/bar'), $response)); + + // Check cached response status + $cachedResponse = $pageCache->get($this->createIncomingRequest('foo/bar'), new Response(new App())); + $this->assertInstanceOf(ResponseInterface::class, $cachedResponse); + $this->assertSame(432, $cachedResponse->getStatusCode()); + $this->assertSame('Foo Bar', $cachedResponse->getReasonPhrase()); + } + public function testCachePageIncomingRequestWithCacheQueryString(): void { $cache = new Cache(); From 97fe5aa9241acc9c54f8260426f07e2f5002f236 Mon Sep 17 00:00:00 2001 From: Sergey Kuznetsov Date: Sat, 27 Dec 2025 02:45:13 +0500 Subject: [PATCH 2/3] Suggestions --- system/Cache/ResponseCache.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/system/Cache/ResponseCache.php b/system/Cache/ResponseCache.php index b7afa3ec28a7..7f0ea9e1da94 100644 --- a/system/Cache/ResponseCache.php +++ b/system/Cache/ResponseCache.php @@ -103,10 +103,10 @@ public function make(CLIRequest|IncomingRequest $request, ResponseInterface $res return $this->cache->save( $this->generateCacheKey($request), serialize([ - 'headers' => $headers, - 'output' => $response->getBody(), - 'statuscode' => $response->getStatusCode(), - 'reasonphrase' => $response->getReasonPhrase(), + 'headers' => $headers, + 'output' => $response->getBody(), + 'status' => $response->getStatusCode(), + 'reason' => $response->getReasonPhrase(), ]), $this->ttl, ); @@ -132,6 +132,8 @@ public function get(CLIRequest|IncomingRequest $request, ResponseInterface $resp $headers = $cachedResponse['headers']; $output = $cachedResponse['output']; + $status = $cachedResponse['status'] ?? 200; + $reason = $cachedResponse['reason'] ?? ''; // Clear all default headers foreach (array_keys($response->headers()) as $key) { @@ -145,9 +147,7 @@ public function get(CLIRequest|IncomingRequest $request, ResponseInterface $resp $response->setBody($output); - if (isset($cachedResponse['statuscode'])) { - $response->setStatusCode($cachedResponse['statuscode'], $cachedResponse['reasonphrase'] ?? ''); - } + $response->setStatusCode($status, $reason); return $response; } From a9ca1733e29cfd953a361f5eeab8c11badabcc8b Mon Sep 17 00:00:00 2001 From: Sergey Kuznetsov Date: Sat, 27 Dec 2025 03:25:32 +0500 Subject: [PATCH 3/3] Changelog --- user_guide_src/source/changelogs/v4.7.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/user_guide_src/source/changelogs/v4.7.0.rst b/user_guide_src/source/changelogs/v4.7.0.rst index 5cfbbf6815f5..bb33537fe556 100644 --- a/user_guide_src/source/changelogs/v4.7.0.rst +++ b/user_guide_src/source/changelogs/v4.7.0.rst @@ -162,6 +162,7 @@ Libraries - **CLI:** Added ``SignalTrait`` to provide unified handling of operating system signals in CLI commands. - **Cache:** Added ``async`` and ``persistent`` config item to Predis handler. - **Cache:** Added ``persistent`` config item to Redis handler. +- **Cache:** Added support for HTTP status in ``ResponseCache``. - **CURLRequest:** Added ``shareConnection`` config item to change default share connection. - **CURLRequest:** Added ``dns_cache_timeout`` option to change default DNS cache timeout. - **CURLRequest:** Added ``fresh_connect`` options to enable/disable request fresh connection.