Skip to content

Commit 65375ef

Browse files
authored
Reduce the payload of the cached HTTP client response (#805)
1 parent 6684fa4 commit 65375ef

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## v1.12.1
9+
10+
### Changed
11+
12+
- Remove some unnecessary data from the serialized HTTP response to reduce
13+
the size when caching.
14+
815
## v1.12.0
916

1017
### Added

src/mantle/http-client/class-cache-flexible-middleware.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public function __invoke( Pending_Request $request, Closure $next ): Response {
6060
defer( fn () => $this->store_response( $fresh_request->send() ) );
6161
}
6262

63+
$response->cached = true;
64+
6365
return $response;
6466
}
6567
}

src/mantle/http-client/class-cache-middleware.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public function __invoke( Pending_Request $request, Closure $next ): Response {
5050
$cache = wp_cache_get( $cache_key, self::CACHE_GROUP );
5151

5252
if ( $cache && $cache instanceof Response ) {
53+
$cache->cached = true;
54+
5355
return $cache;
5456
}
5557

src/mantle/http-client/class-response.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* code: int,
4949
* message: string,
5050
* },
51+
* http_response?: \WP_HTTP_Requests_Response,
5152
* }
5253
*/
5354
class Response implements ArrayAccess {
@@ -73,6 +74,16 @@ class Response implements ArrayAccess {
7374
*/
7475
protected array $response;
7576

77+
/**
78+
* The request URL.
79+
*/
80+
protected readonly ?string $url;
81+
82+
/**
83+
* Determine if the response was created from the cache.
84+
*/
85+
public bool $cached = false;
86+
7687
/**
7788
* Constructor.
7889
*
@@ -88,6 +99,13 @@ public function __construct( array $response ) {
8899
$response['headers'] = array_change_key_case( (array) ( $response['headers'] ?? [] ) );
89100

90101
$this->response = $response;
102+
103+
// @phpstan-ignore instanceof.alwaysTrue
104+
if ( isset( $response['http_response'] ) && $response['http_response'] instanceof \WP_HTTP_Requests_Response ) {
105+
$this->url = $response['http_response']->get_response_object()->url;
106+
} else {
107+
$this->url = null;
108+
}
91109
}
92110

93111
/**
@@ -463,4 +481,25 @@ public function offsetSet( mixed $offset, mixed $value ): void {
463481
public function offsetUnset( mixed $offset ): void {
464482
throw new LogicException( 'Response values are read-only.' );
465483
}
484+
485+
/**
486+
* Prepare the object for serialization.
487+
*
488+
* @return array<string, mixed>
489+
*/
490+
public function __serialize(): array {
491+
// Purge some data from the response for lighter serialization.
492+
unset( $this->response['http_response'] );
493+
494+
foreach ( [ 'cookies', 'filename', 'headers' ] as $key ) {
495+
if ( empty( $this->response[ $key ] ) ) {
496+
unset( $this->response[ $key ] );
497+
}
498+
}
499+
500+
return [
501+
'url' => $this->url,
502+
'response' => $this->response,
503+
];
504+
}
466505
}

tests/HttpClient/CachedHttpClientTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,17 @@ public function test_can_create_cached_client() {
3737
}
3838

3939
public function test_it_can_make_http_request() {
40+
// $this->allow_stray_requests();
4041
$this->fake_request( mock_http_response()->with_json( [ 'example' => 'value' ] ) );
4142

42-
$this->client->get( 'https://example.com' );
43-
$this->client->get( 'https://example.com' );
43+
$response = $this->client->get( 'https://example.com' );
44+
45+
$this->assertEquals( 'value', $response->json( 'example' ) );
46+
47+
$response = $this->client->get( 'https://example.com' );
48+
49+
$this->assertEquals( 'value', $response->json( 'example' ) );
50+
$this->assertTrue( $response->cached );
4451

4552
$this->assertRequestCount( 1 );
4653
}

0 commit comments

Comments
 (0)