-
Notifications
You must be signed in to change notification settings - Fork 14
Configure request headers used in cache keys #800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from 5 commits
a140b8a
bf3dfe4
a205f96
40c6544
29c7ecb
6c8fd10
4981d3f
dafd919
a1c7e51
bb1a106
44f1b25
1738a00
b94db2f
1cde10a
52bc96e
be07ab9
9ae5731
78741db
6f5bfe3
e9b5dc5
fea0af2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace RemoteDataBlocks\Config; | ||
|
|
||
| interface CacheKeyRequestHeadersInterface { | ||
| /** | ||
| * Get the request header names whose values should be included in cache keys. | ||
| * | ||
| * @return array<string> Request header names included in cache keys. | ||
| */ | ||
| public function get_cache_key_request_headers(): array; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace RemoteDataBlocks\HttpClient; | ||
|
|
||
| final class CacheKeyRequestHeaders { | ||
| public const DEFAULT_HEADERS = [ 'Authorization', 'Cache-Control' ]; | ||
|
maxschmeling marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Merge request header name lists without case-insensitive duplicates. | ||
| * | ||
| * @param array<string> ...$header_lists Request header name lists. | ||
| * @return array<string> Merged request header names. | ||
| */ | ||
| public static function merge( array ...$header_lists ): array { | ||
| $merged_headers = []; | ||
| $seen_headers = []; | ||
|
|
||
| foreach ( $header_lists as $header_list ) { | ||
| foreach ( $header_list as $header ) { | ||
| $normalized_header = strtolower( $header ); | ||
| if ( isset( $seen_headers[ $normalized_header ] ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| $seen_headers[ $normalized_header ] = true; | ||
| $merged_headers[] = $header; | ||
| } | ||
| } | ||
|
|
||
| return $merged_headers; | ||
|
maxschmeling marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,31 @@ | |
|
|
||
| namespace RemoteDataBlocks\HttpClient; | ||
|
|
||
| use Psr\Http\Message\RequestInterface; | ||
|
|
||
| class RdbCacheMiddleware extends \Kevinrob\GuzzleCache\CacheMiddleware { | ||
| public const CACHE_KEY_REQUEST_HEADERS_HEADER = 'X-Remote-Data-Blocks-Cache-Key-Headers'; | ||
|
chriszarate marked this conversation as resolved.
Outdated
|
||
| public const CACHE_KEY_REQUEST_HEADERS_OPTION = 'remote_data_blocks_cache_key_request_headers'; | ||
|
|
||
| public function __invoke( callable $handler ): callable { | ||
|
maxschmeling marked this conversation as resolved.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand what is accomplished by this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Left over duplicate. I've removed it.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be clear, I don't think this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't. I removed it but didn't get that in the commit I guess. Pushed in 78741db
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This removal actually does cause a test failure.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also realized this means the TTL value isn't being stripped either. So I've added a test and fix for that as well.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The same is probably true of
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure it is a bad side effect. It's just a request header. But we should pick a policy and be consistent. I'd vote to leave it personally.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I already removed it in the commit I commented above. I definitely don't think we should be adding headers to requests for internal functions and letting them flow through to the endpoint. Unless you have strong opinions about it, I'm going to go with the approach of stripping them. |
||
| $handler_without_cache_metadata = function ( RequestInterface $request, array $options ) use ( $handler ) { | ||
| return $handler( $request->withoutHeader( self::CACHE_KEY_REQUEST_HEADERS_HEADER ), $options ); | ||
| }; | ||
| $cache_handler = parent::__invoke( $handler_without_cache_metadata ); | ||
|
|
||
| return function ( RequestInterface $request, array $options ) use ( $cache_handler ) { | ||
| $cache_key_request_headers = $options[ self::CACHE_KEY_REQUEST_HEADERS_OPTION ] ?? []; | ||
| unset( $options[ self::CACHE_KEY_REQUEST_HEADERS_OPTION ] ); | ||
|
|
||
| if ( is_array( $cache_key_request_headers ) ) { | ||
| $cache_key_request_headers = array_values( array_filter( $cache_key_request_headers, 'is_string' ) ); | ||
| $request = $request->withHeader( self::CACHE_KEY_REQUEST_HEADERS_HEADER, $cache_key_request_headers ); | ||
| } | ||
|
|
||
| return $cache_handler( $request, $options ); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @var array<string, true> | ||
| */ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.