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

WP_Term_Query cache problem #2756

Closed
wants to merge 8 commits into from

Conversation

spacedmonkey
Copy link
Member

Trac ticket: https://core.trac.wordpress.org/ticket/55837


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@spacedmonkey spacedmonkey self-assigned this May 27, 2022
Copy link
Contributor

@costdev costdev left a comment

Choose a reason for hiding this comment

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

Thanks for the PR Jonny!

This review suggests some changes in the tests.

tests/phpunit/tests/term/getTerms.php Outdated Show resolved Hide resolved
tests/phpunit/tests/term/getTerms.php Outdated Show resolved Hide resolved
tests/phpunit/tests/term/getTerms.php Show resolved Hide resolved
tests/phpunit/tests/term/getTerms.php Show resolved Hide resolved
tests/phpunit/tests/term/getTerms.php Show resolved Hide resolved
Co-authored-by: Colin Stewart <79332690+costdev@users.noreply.github.com>
@@ -770,7 +770,7 @@ public function get_terms() {
// $args can be anything. Only use the args defined in defaults to compute the key.
$cache_args = wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) );

unset( $cache_args['pad_counts'], $cache_args['update_term_meta_cache'] );
unset( $cache_args['update_term_meta_cache'] );
Copy link
Member Author

Choose a reason for hiding this comment

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

Padding counts changes the response, so we have to have a different cache.

@@ -770,7 +770,7 @@ public function get_terms() {
// $args can be anything. Only use the args defined in defaults to compute the key.
$cache_args = wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) );

unset( $cache_args['pad_counts'], $cache_args['update_term_meta_cache'] );
unset( $cache_args['update_term_meta_cache'] );
Copy link
Member Author

Choose a reason for hiding this comment

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

Padding counts changes the response, so we have to have a different cache.

Comment on lines -852 to -871
/*
* When querying for terms connected to objects, we may get
* duplicate results. The duplicates should be preserved if
* `$fields` is 'all_with_object_id', but should otherwise be
* removed.
*/
if ( ! empty( $args['object_ids'] ) && 'all_with_object_id' !== $_fields ) {
$_tt_ids = array();
$_terms = array();
foreach ( $terms as $term ) {
if ( isset( $_tt_ids[ $term->term_id ] ) ) {
continue;
}

$_tt_ids[ $term->term_id ] = 1;
$_terms[] = $term;
}

$terms = $_terms;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This code no longer does anything, so should be removed.

Comment on lines -852 to -871
/*
* When querying for terms connected to objects, we may get
* duplicate results. The duplicates should be preserved if
* `$fields` is 'all_with_object_id', but should otherwise be
* removed.
*/
if ( ! empty( $args['object_ids'] ) && 'all_with_object_id' !== $_fields ) {
$_tt_ids = array();
$_terms = array();
foreach ( $terms as $term ) {
if ( isset( $_tt_ids[ $term->term_id ] ) ) {
continue;
}

$_tt_ids[ $term->term_id ] = 1;
$_terms[] = $term;
}

$terms = $_terms;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This code no longer does anything, so should be removed.

@spacedmonkey
Copy link
Member Author

@peterwilsoncc

The biggest change here, is that before there were two variables, $term_objects and $terms. These variables basically did the same thing but depending on what params were passed, would result in different values. This PR, ensure that if pad_counts or child_of are passed, that the correct value is returned. It also makes the code a lot easier to read, by removing two variables. It also improves cache value, by only caching an array of ids, if other properties are not needed.

Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

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

@spacedmonkey Thanks for the explanation, it really helped.

Just one note below about triggering a cache flush on update. The current revision ID on SVN is 53479 so you can use that as the DB version for now but it is usually updated on commit to the current SVN revision.

@@ -770,7 +770,7 @@ public function get_terms() {
// $args can be anything. Only use the args defined in defaults to compute the key.
$cache_args = wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) );

unset( $cache_args['pad_counts'], $cache_args['update_term_meta_cache'] );
unset( $cache_args['update_term_meta_cache'] );
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this changes the cache key & format of the cached data. To be safe the global cache should be flushed during update.

Changing the database version will cause that to happen, no update function is required.

*/
$wp_db_version = 51917;

upgrade_all();
if ( is_multisite() && is_main_site() ) {
upgrade_network();
}
wp_cache_flush();

The chances of a collision a really slim.

Copy link
Member Author

Choose a reason for hiding this comment

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

Unsetting this, will change the cache key. Meaning caches will be invalidated. Meaning we should not have to flush the cache.

@spacedmonkey
Copy link
Member Author

Committed 6d89ea8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants