-
Notifications
You must be signed in to change notification settings - Fork 382
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
Improve post processor response cache #1325
Changes from 3 commits
0a5e3f2
fe8bf17
1d1f399
7e6f874
eab2d95
d7afb0d
73e72b9
7316ee0
773bfee
75611f5
bb39b89
2c760bd
efd9d93
f37e7ff
b0dc43d
8e553e9
1e6a8af
b05a38f
0274715
ecb1587
752de78
e575cde
f6716ec
bf3dd20
cdd0506
65ad9fa
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 |
---|---|---|
|
@@ -26,6 +26,20 @@ class AMP_Theme_Support { | |
*/ | ||
const RESPONSE_CACHE_GROUP = 'amp-response'; | ||
|
||
/** | ||
* Post-processor cache group name. | ||
* | ||
* @var string | ||
*/ | ||
const POST_PROCESSOR_CACHE_EFFECTIVENESS = 'post_processor_cache_effectiveness'; | ||
|
||
/** | ||
* Cache miss threshold for determining when to disable post-processor cache. | ||
* | ||
* @var int | ||
*/ | ||
const CACHE_MISS_THRESHOLD = 3; | ||
|
||
/** | ||
* Sanitizer classes. | ||
* | ||
|
@@ -98,6 +112,20 @@ class AMP_Theme_Support { | |
*/ | ||
protected static $support_added_via_option = false; | ||
|
||
/** | ||
* Response cache key. | ||
* | ||
* @var string | ||
*/ | ||
public static $response_cache_key; | ||
|
||
/** | ||
* Post-processor cache key. | ||
* | ||
* @var string | ||
*/ | ||
public static $post_processor_cache_key; | ||
|
||
/** | ||
* Initialize. | ||
* | ||
|
@@ -1760,19 +1788,36 @@ public static function prepare_response( $response, $args = array() ) { | |
$current_url = amp_get_current_url(); | ||
$ampless_url = amp_remove_endpoint( $current_url ); | ||
|
||
// When response caching is enabled, determine if it should be turned off for cache misses. | ||
$caches_for_url = null; | ||
self::$post_processor_cache_key = null; | ||
if ( true === $args['enable_response_caching'] ) { | ||
self::$post_processor_cache_key = md5( $current_url ); | ||
$caches_for_url = wp_cache_get( self::$post_processor_cache_key, self::POST_PROCESSOR_CACHE_EFFECTIVENESS ); | ||
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. Sone of the subsequent checks could perhaps be simplified by checking if this is an array here, and if not, just set it to be an empty array. |
||
|
||
$args['enable_response_caching'] = ( | ||
empty( $caches_for_url ) | ||
|| | ||
! is_array( $caches_for_url ) | ||
|| | ||
count( $caches_for_url ) < self::CACHE_MISS_THRESHOLD | ||
); | ||
} | ||
|
||
// Return cache if enabled and found. | ||
$cache_response = null; | ||
$cache_response = null; | ||
self::$response_cache_key = ''; | ||
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( | ||
self::$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 ); | ||
$response_cache = wp_cache_get( self::$response_cache_key, self::RESPONSE_CACHE_GROUP ); | ||
|
||
// Make sure that all of the validation errors should be sanitized in the same way; if not, then the cached body should be discarded. | ||
$blocking_error_count = 0; | ||
|
@@ -1802,9 +1847,21 @@ public static function prepare_response( $response, $args = array() ) { | |
return $response_cache['body']; | ||
} | ||
|
||
$cache_response = function( $body, $validation_results ) use ( $response_cache_key ) { | ||
$cache_response = function( $body, $validation_results ) use ( $caches_for_url ) { | ||
if ( empty( $caches_for_url ) ) { | ||
$caches_for_url = array( AMP_Theme_Support::$response_cache_key ); | ||
} else { | ||
$caches_for_url[] = AMP_Theme_Support::$response_cache_key; | ||
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. Per above, this could be the sole line instead of the if/else. |
||
} | ||
wp_cache_set( | ||
AMP_Theme_Support::$post_processor_cache_key, | ||
$caches_for_url, | ||
AMP_Theme_Support::POST_PROCESSOR_CACHE_EFFECTIVENESS, | ||
MONTH_IN_SECONDS | ||
); | ||
|
||
return wp_cache_set( | ||
$response_cache_key, | ||
AMP_Theme_Support::$response_cache_key, | ||
compact( 'body', 'validation_results' ), | ||
AMP_Theme_Support::RESPONSE_CACHE_GROUP, | ||
MONTH_IN_SECONDS | ||
|
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.
Is this necessary? Aren't references limited to a single method? Since this is a static class it would be better of we limited the state that is persisted in it's class variables IMO.
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.
@westonruter I added this public property for the following purposes:
use
.It serves no other external purpose as part of an API.
Let's think about it.
From a testing point of view, we can infer that we disabled the cache by the state of the post-processor cache. While that's not a direct test on the response cache, both are within the closure. Therefore, it's a minimal risk to test by inference.
Hmm, I like that approach better actually. We get the benefit only exposing what is needed externally while avoided the extra work of resetting persistent state.
I'll make that change.