-
Notifications
You must be signed in to change notification settings - Fork 383
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
Issue 959: cache post processor response #1156
Changes from 5 commits
091a4a2
82b6b88
65cfa08
b3ad9a3
e80ac16
9bcc4ac
0b34b1b
2eeef23
682808a
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 |
---|---|---|
|
@@ -19,6 +19,27 @@ class AMP_Theme_Support { | |
*/ | ||
const SCRIPTS_PLACEHOLDER = '<!-- AMP:SCRIPTS_PLACEHOLDER -->'; | ||
|
||
/** | ||
* Response cache group name. | ||
* | ||
* @var string | ||
*/ | ||
const RESPONSE_CACHE_GROUP = 'amp-reponse'; | ||
|
||
/** | ||
* Query var that triggers response cache removal for the given page. | ||
* | ||
* @var string | ||
*/ | ||
const FLUSH_RESPONSE_CACHE_VAR = 'amp_flush_response_cache'; | ||
|
||
/** | ||
* Response cache hash key. | ||
* | ||
* @var string | ||
*/ | ||
public static $response_cache_key; | ||
|
||
/** | ||
* Sanitizer classes. | ||
* | ||
|
@@ -970,8 +991,6 @@ public static function is_output_buffering() { | |
* @return string Processed Response. | ||
*/ | ||
public static function finish_output_buffering( $response ) { | ||
AMP_Response_Headers::send_server_timing( 'amp_output_buffer', -self::$init_start_time, 'AMP Output Buffer' ); | ||
|
||
self::$is_output_buffering = false; | ||
return self::prepare_response( $response ); | ||
} | ||
|
@@ -1036,10 +1055,36 @@ public static function prepare_response( $response, $args = array() ) { | |
'allow_dirty_styles' => self::is_customize_preview_iframe(), // Dirty styles only needed when editing (e.g. for edit shortcodes). | ||
'allow_dirty_scripts' => is_customize_preview(), // Scripts are always needed to inject changeset UUID. | ||
'disable_invalid_removal' => $is_validation_debug_mode, | ||
'enable_response_caching' => ! defined( 'WP_DEBUG' ) || true !== WP_DEBUG || $is_validation_debug_mode, | ||
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. Instead of |
||
), | ||
$args | ||
); | ||
|
||
// Return cache if enabled and found. | ||
if ( true === $args['enable_response_caching'] ) { | ||
self::set_response_cache_key( array( | ||
$args, | ||
$response, | ||
self::$sanitizer_classes, | ||
self::$embed_handlers, | ||
AMP__VERSION, | ||
) ); | ||
|
||
if ( isset( $_REQUEST[ self::FLUSH_RESPONSE_CACHE_VAR ] ) ) { // WPCS: csrf ok. | ||
wp_cache_delete( self::$response_cache_key, self::RESPONSE_CACHE_GROUP ); | ||
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. Why does there need to be a way to manually flush the cache? Is this for development? If so, then there should be a cap check to make sure that only authorized users can delete the cache. Otherwise, I think this cache deletion ability could be removed. 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 agree, cap should be checked to flush cache. Yes it was to ease development as flushing cached may be cumbersome depending on the setup and object caching mechanism used. Let's keep the code base clean though, I removed it in this commit. |
||
} else { | ||
$response_cache = wp_cache_get( self::$response_cache_key, self::RESPONSE_CACHE_GROUP ); | ||
|
||
if ( ! empty( $response_cache ) ) { | ||
AMP_Response_Headers::send_header( 'AMP-Response-Cache', true ); | ||
return $response_cache; | ||
} | ||
} | ||
} | ||
|
||
AMP_Response_Headers::send_header( 'AMP-Response-Cache', false ); | ||
AMP_Response_Headers::send_server_timing( 'amp_output_buffer', -self::$init_start_time, 'AMP Output Buffer' ); | ||
|
||
$dom_parse_start = microtime( true ); | ||
|
||
/* | ||
|
@@ -1140,9 +1185,39 @@ public static function prepare_response( $response, $args = array() ) { | |
|
||
AMP_Response_Headers::send_server_timing( 'amp_dom_serialize', -$dom_serialize_start, 'AMP DOM Serialize' ); | ||
|
||
// Cache response if enabled. | ||
if ( true === $args['enable_response_caching'] ) { | ||
wp_cache_set( self::$response_cache_key, $response, self::RESPONSE_CACHE_GROUP, MONTH_IN_SECONDS ); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Set response cache hash, the data values dictates whether a new hash key should be generated or not. | ||
* | ||
* @since 1.0 | ||
* | ||
* @param array $data Data used to build hash key. A new hash key will be generated upon data value changes | ||
* which will eventually trigger a new cached reponse. | ||
*/ | ||
protected static function set_response_cache_key( $data ) { | ||
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 confused as to why this is needed. What specifically are the closures that are causing the problem? I don't think we should be calling them to obtain their value, as what if the closures require arguments? What are the closures going to be doing? It would be better to just omit any closures. In fact, if you use 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. If an argument callback is used in a way that would modify the response during post processing, the cache would not be regenerated even if the returned value of the callback changes which could cause issues. I don't think any argument callback are used that way at this so should be fine |
||
// Loop through the data and make sure all functions and closure are called to prevent serializing issues. | ||
$maybe_call_user_function = function ( $data ) use ( &$maybe_call_user_function ) { | ||
if ( is_array( $data ) ) { | ||
foreach ( $data as $index => $item ) { | ||
$data[ $index ] = $maybe_call_user_function( $item ); | ||
} | ||
} elseif ( is_callable( $data ) ) { | ||
return call_user_func( $data ); | ||
} | ||
|
||
return $data; | ||
}; | ||
|
||
self::$response_cache_key = md5( maybe_serialize( $maybe_call_user_function( $data ) ) ); | ||
} | ||
|
||
/** | ||
* Adds 'data-amp-layout' to the allowed <img> attributes for wp_kses(). | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this variable is only ever used inside of the
prepare_response
method, I don't think it needs a class variable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was used in
prepare_response()
,set_response_cache_key()
and in the tests but it is no longer necessary sinceset_response_cache_key()
was removed in favour ofwp_json_encode()
in this commit.