-
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 8 commits
a140b8a
bf3dfe4
a205f96
40c6544
29c7ecb
6c8fd10
4981d3f
dafd919
a1c7e51
bb1a106
44f1b25
1738a00
b94db2f
1cde10a
52bc96e
be07ab9
9ae5731
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,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 |
|---|---|---|
|
|
@@ -19,31 +19,30 @@ class RdbCacheStrategy extends GreedyCacheStrategy { | |
| public const CACHE_TTL_REQUEST_HEADER = GreedyCacheStrategy::HEADER_TTL; | ||
| public const WP_OBJECT_CACHE_GROUP = 'remote-data-blocks'; | ||
|
|
||
| private const CACHE_INVALIDATING_REQUEST_HEADERS = [ 'Authorization', 'Cache-Control' ]; | ||
| private const ERROR_CACHE_TTL_IN_SECONDS = 30; // 30 seconds for error responses | ||
| private const FALLBACK_CACHE_TTL_IN_SECONDS = 300; // 5 minutes for success responses | ||
|
|
||
| public function __construct( ?CacheStorageInterface $storage = null ) { | ||
| // Filter this if customization is needed. | ||
| $vary_headers = new KeyValueHttpHeader( self::CACHE_INVALIDATING_REQUEST_HEADERS ); | ||
|
|
||
| parent::__construct( | ||
| $storage ?? new WordPressObjectCacheStorage( self::WP_OBJECT_CACHE_GROUP ), | ||
| self::FALLBACK_CACHE_TTL_IN_SECONDS, | ||
| $vary_headers | ||
|
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. Cache poisoning is still possible in the future. The vary headers passed to the constructor (
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. |
||
| self::FALLBACK_CACHE_TTL_IN_SECONDS | ||
| ); | ||
| } | ||
|
|
||
| public static function get_object_cache_key_from_request( RequestInterface $request ): string { | ||
| $request_body = (string) $request->getBody(); | ||
| $request_headers = $request->getHeaders(); | ||
| $request_method = $request->getMethod(); | ||
| $request_uri = (string) $request->getUri(); | ||
|
|
||
| $cache_key_request_headers = CacheKeyRequestHeaders::merge( | ||
| CacheKeyRequestHeaders::DEFAULT_HEADERS, | ||
| $request->getHeader( RdbCacheMiddleware::CACHE_KEY_REQUEST_HEADERS_HEADER ) | ||
| ); | ||
|
|
||
| $cache_headers = []; | ||
| foreach ( self::CACHE_INVALIDATING_REQUEST_HEADERS as $header ) { | ||
| if ( isset( $request_headers[ $header ] ) ) { | ||
| $cache_headers[ $header ] = $request_headers[ $header ]; | ||
| foreach ( $cache_key_request_headers as $header ) { | ||
| if ( $request->hasHeader( $header ) ) { | ||
| $cache_headers[ $header ] = $request->getHeader( $header ); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -81,11 +80,15 @@ protected function getCacheObject( RequestInterface $request, ResponseInterface | |
| $jitter = intval( ceil( min( $ttl * 0.1, 20 ) ) ); | ||
| $ttl = intval( $ttl ) + wp_rand( 0, $jitter ); | ||
|
|
||
| // NOTE: We skip the vary headers '*' check from the parent method | ||
| // since our defined vary headers cannot accept a '*' value. | ||
| // Cache-key request headers are resolved per request in getCacheKey(), so | ||
| // the parent's static vary-header check does not apply here. | ||
|
|
||
| $response = $response->withoutHeader( 'Etag' )->withoutHeader( 'Last-Modified' ); | ||
|
|
||
| return new CacheEntry( $request->withoutHeader( static::HEADER_TTL ), $response, new DateTime( sprintf( '%+d seconds', $ttl ) ) ); | ||
| $cache_request = $request | ||
| ->withoutHeader( static::HEADER_TTL ) | ||
| ->withoutHeader( RdbCacheMiddleware::CACHE_KEY_REQUEST_HEADERS_HEADER ); | ||
|
|
||
| return new CacheEntry( $cache_request, $response, new DateTime( sprintf( '%+d seconds', $ttl ) ) ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.