Skip to content
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

Merged
merged 9 commits into from
May 23, 2018
39 changes: 37 additions & 2 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class AMP_Theme_Support {
*/
const SCRIPTS_PLACEHOLDER = '<!-- AMP:SCRIPTS_PLACEHOLDER -->';

/**
* Response cache group name.
*
* @var string
*/
const RESPONSE_CACHE_GROUP = 'amp-reponse';

/**
* Sanitizer classes.
*
Expand Down Expand Up @@ -970,8 +977,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 );
}
Expand Down Expand Up @@ -1036,10 +1041,35 @@ 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 )
&&
! AMP_Validation_Utils::should_validate_response()
),
),
$args
);

// Return cache if enabled and found.
if ( true === $args['enable_response_caching'] ) {
// Set response cache hash, the data values dictates whether a new hash key should be generated or not.
$response_cache_key = md5( wp_json_encode( array(
$args,
$response,
self::$sanitizer_classes,
self::$embed_handlers,
AMP__VERSION,
) ) );

$response_cache = wp_cache_get( $response_cache_key, self::RESPONSE_CACHE_GROUP );

if ( ! empty( $response_cache ) ) {
return $response_cache;
}
}

AMP_Response_Headers::send_server_timing( 'amp_output_buffer', -self::$init_start_time, 'AMP Output Buffer' );

$dom_parse_start = microtime( true );

/*
Expand Down Expand Up @@ -1140,6 +1170,11 @@ 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( $response_cache_key, $response, self::RESPONSE_CACHE_GROUP, MONTH_IN_SECONDS );
}

return $response;
}

Expand Down
38 changes: 38 additions & 0 deletions tests/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,44 @@ public function test_prepare_response() {
$this->assertInstanceOf( 'DOMElement', $removed_nodes['script'] );
$this->assertInstanceOf( 'DOMAttr', $removed_nodes['onclick'] );
$this->assertInstanceOf( 'DOMAttr', $removed_nodes['handle'] );

$call_response = function() use ( $original_html ) {
return AMP_Theme_Support::prepare_response( $original_html, array(
'enable_response_caching' => true,
) );
};

// Test that first response isn't cached.
$first_response = $call_response();
$this->assertGreaterThan( 0, count( array_filter(
AMP_Response_Headers::$headers_sent,
function( $header ) {
return 'Server-Timing' === $header['name'];
}
) ) );

// Test that response cache is return upon second call.
AMP_Response_Headers::$headers_sent = array();
$this->assertEquals( $first_response, $call_response() );
$this->assertSame( 0, count( array_filter(
AMP_Response_Headers::$headers_sent,
function( $header ) {
return 'Server-Timing' === $header['name'];
}
) ) );

// Test new cache upon argument change.
AMP_Response_Headers::$headers_sent = array();
AMP_Theme_Support::prepare_response( $original_html, array(
'enable_response_caching' => true,
'test_reset_by_arg' => true,
) );
$this->assertGreaterThan( 0, count( array_filter(
AMP_Response_Headers::$headers_sent,
function( $header ) {
return 'Server-Timing' === $header['name'];
}
) ) );
}

/**
Expand Down