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

Cache error index counts and URL count in "At a Glance" widget #5343

Merged
merged 11 commits into from
Sep 10, 2020

Conversation

johnwatkins0
Copy link
Contributor

@johnwatkins0 johnwatkins0 commented Sep 6, 2020

Summary

Fixes #5271

This PR adds transient-based caching to the get_validation_error_count function. The transient data is an associative array with the JSON-encoded get_validation_error_count used as the key. The transient data is deleted when terms are created, deleted, or updated.

Additionally, this PR caches the validated URL count shown in the "At a Glance" admin dashboard widget using the transient used to show the count in the sidebar.

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@google-cla google-cla bot added the cla: yes Signed the Google CLA label Sep 6, 2020
@johnwatkins0 johnwatkins0 marked this pull request as ready for review September 7, 2020 01:49
@github-actions
Copy link
Contributor

github-actions bot commented Sep 7, 2020

Plugin builds for 4fe39ad are ready 🛎️!

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The transient will also need to be deleted in \AMP_Validated_URL_Post_Type::handle_validation_error_status_update().

Also when an amp_validated_url post is deleted.

Basically I think wherever we have a delete_transient( AMP_Validated_URL_Post_Type::NEW_VALIDATION_ERROR_URLS_COUNT_TRANSIENT ) we'll need to also add deletion of this other transient.

@westonruter westonruter added the WS:Core Work stream for Plugin core label Sep 8, 2020
@google-cla
Copy link

google-cla bot commented Sep 9, 2020

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added cla: no Has not signed the Google CLA and removed cla: yes Signed the Google CLA labels Sep 9, 2020
@pierlon
Copy link
Contributor

pierlon commented Sep 9, 2020

@googlebot I consent.

@google-cla google-cla bot added cla: yes Signed the Google CLA and removed cla: no Has not signed the Google CLA labels Sep 9, 2020
@westonruter westonruter modified the milestones: v2.1, v2.0.2 Sep 9, 2020
@johnwatkins0
Copy link
Contributor Author

Added transient deletion in more places, and it looks like Pierre helped with the unrelated e2e test failure.

@@ -219,6 +219,7 @@ static function () {
$handle_delete = static function ( $post_id ) {
if ( static::POST_TYPE_SLUG === get_post_type( $post_id ) ) {
delete_transient( static::NEW_VALIDATION_ERROR_URLS_COUNT_TRANSIENT );
delete_transient( AMP_Validation_Error_Taxonomy::TRANSIENT_KEY_ERROR_INDEX_COUNTS );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saw that the value stored in the TRANSIENT_KEY_ERROR_INDEX_COUNTS is only amp_validation_error taxonomy terms irrespective of amp_validated_url posts (hide_empty is not set to true when calling wp_count_terms()). But someone could pass 'hide_empty' => true when calling \AMP_Validation_Error_Taxonomy::get_validation_error_count(). So I suppose this is safest to include.

@@ -1970,6 +1972,7 @@ static function( $result ) {
}

delete_transient( static::NEW_VALIDATION_ERROR_URLS_COUNT_TRANSIENT );
delete_transient( AMP_Validation_Error_Taxonomy::TRANSIENT_KEY_ERROR_INDEX_COUNTS );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize this may not be necessary because recheck_post() will call AMP_Validated_URL_Post_Type::store_validation_errors() which will then clear both of these transients.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it won't be needed with clearing transient at the edit_amp_validation_error action.

Suggested change
delete_transient( AMP_Validation_Error_Taxonomy::TRANSIENT_KEY_ERROR_INDEX_COUNTS );

Comment on lines 2928 to 2946
$count = get_transient( static::NEW_VALIDATION_ERROR_URLS_COUNT_TRANSIENT );

$query = new WP_Query(
[
'post_type' => self::POST_TYPE_SLUG,
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_STATUS_QUERY_VAR => [
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_REJECTED_STATUS,
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_ACCEPTED_STATUS,
],
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
]
);
if ( false === $count ) {

$query = new WP_Query(
[
'post_type' => self::POST_TYPE_SLUG,
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_STATUS_QUERY_VAR => [
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_REJECTED_STATUS,
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_ACCEPTED_STATUS,
],
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
]
);

$count = $query->found_posts;
set_transient( static::NEW_VALIDATION_ERROR_URLS_COUNT_TRANSIENT, $count, DAY_IN_SECONDS );
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out the logic here is duplicated with the logic in \AMP_Validated_URL_Post_Type::get_validation_error_urls_count(). What we are missing is setting the transient inside this method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b98b921

@@ -957,6 +958,7 @@ public static function store_validation_errors( $validation_errors, $url, $args
}

delete_transient( static::NEW_VALIDATION_ERROR_URLS_COUNT_TRANSIENT );
delete_transient( AMP_Validation_Error_Taxonomy::TRANSIENT_KEY_ERROR_INDEX_COUNTS );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct as terms may be created/updated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, it is not needed if we delete the transient at the edit_amp_validation_error action.

Suggested change
delete_transient( AMP_Validation_Error_Taxonomy::TRANSIENT_KEY_ERROR_INDEX_COUNTS );

Comment on lines 344 to 346

delete_transient( self::TRANSIENT_KEY_ERROR_INDEX_COUNTS );

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of deleting the transient here, we can catch more cases by adding a delete_amp_validation_error action above.

Suggested change
delete_transient( self::TRANSIENT_KEY_ERROR_INDEX_COUNTS );

@@ -279,6 +286,8 @@ public static function register() {
if ( is_admin() ) {
self::add_admin_hooks();
}

add_action( 'created_' . self::TAXONOMY_SLUG, [ __CLASS__, 'clear_cached_counts' ] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
add_action( 'created_' . self::TAXONOMY_SLUG, [ __CLASS__, 'clear_cached_counts' ] );
add_action( 'created_' . self::TAXONOMY_SLUG, [ __CLASS__, 'clear_cached_counts' ] );
add_action( 'delete_' . self::TAXONOMY_SLUG, [ __CLASS__, 'clear_cached_counts' ] );
add_action( 'edit_' . self::TAXONOMY_SLUG, [ __CLASS__, 'clear_cached_counts' ] );

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 0675a76.

@pierlon @johnwatkins0 Thoughts?

@@ -2846,6 +2874,7 @@ public static function handle_validation_error_update( $redirect_to, $action, $t

if ( $updated_count ) {
delete_transient( AMP_Validated_URL_Post_Type::NEW_VALIDATION_ERROR_URLS_COUNT_TRANSIENT );
delete_transient( self::TRANSIENT_KEY_ERROR_INDEX_COUNTS );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed with adding a delete_amp_validation_error action callback above.

@westonruter
Copy link
Member

After the changes in this PR, all queries pull from transients:

Before After
image image

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But looking for confirmation prior to merging.

@johnwatkins0
Copy link
Contributor Author

@westonruter I see the same.
Screen Shot 2020-09-09 at 5 10 02 PM

@westonruter
Copy link
Member

So you endorse my additional changes?

@johnwatkins0
Copy link
Contributor Author

✅ Endorsed. I think we're still deleting the transient more than technically needed, but that's better than less than needed.

@pierlon
Copy link
Contributor

pierlon commented Sep 9, 2020

Oh wait! Making a quick review.

Comment on lines 291 to 292
add_action( 'edit_' . self::TAXONOMY_SLUG, [ __CLASS__, 'clear_cached_counts' ] );
add_action( 'delete_' . self::TAXONOMY_SLUG, [ __CLASS__, 'clear_cached_counts' ] );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these instead be edited_ and deleted_? The edit_ and delete_ actions are run before the term is filtered, while the past tense counterpart is run after the term cache has been cleaned.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that created_ is used above.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm seeing that edit_{$taxonomy} also happens after the edit has been made. It's docblock says:

Fires after a term in a specific taxonomy has been updated, but before the term cache has been cleaned.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see an action for deleted_{$taxonomy}.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes your right. Just was wondering why the use of created_{$taxonomy} and not create_{$taxonomy}.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we could use created_{$taxonomy} and edited_{$taxonomy} instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that saved_{$taxonomy} is called when both inserting and editing, so that's an alternative.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait that was introduced in WP 5.5, nevermind.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See 4fe39ad, where edited_{$taxonomy} is instead used.

$new_validation_error_urls = (int) $new_validation_error_urls;
}

$new_validation_error_urls = static::get_validation_error_urls_count();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this instead be self:: (although the class is not marked as final)?

Suggested change
$new_validation_error_urls = static::get_validation_error_urls_count();
$new_validation_error_urls = self::get_validation_error_urls_count();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering after seeing the alternating uses of static and self within the class.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's in part because we used to support PHP 5.2 in which static:: didn't exist.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah OK, that can be attended to at a later time.

@westonruter westonruter merged commit a2c0d44 into develop Sep 10, 2020
@westonruter westonruter deleted the feature/5271-cache-error-index-counts branch September 10, 2020 00:00
westonruter added a commit that referenced this pull request Sep 10, 2020
Co-authored-by: John Watkins <johnwatkins0@gmail.com>
Co-authored-by: Pierre Gordon <pierregordon@protonmail.com>
@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Sep 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelogged Whether the issue/PR has been added to release notes. cla: yes Signed the Google CLA WS:Core Work stream for Plugin core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cache "Error Index" count
3 participants