Cache API: Improve cache key generation in WP_Comment_Query.

Discard the parameters `update_comment_meta_cache` and `update_comment_post_cache` when generating the cache key as neither have an effect on the database query generated.

Props uday17035, spacedmonkey.
Fixes #55460.



git-svn-id: https://develop.svn.wordpress.org/trunk@53169 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson 2022-04-13 02:55:34 +00:00
parent 2e4661c6c9
commit 487347fa54
2 changed files with 29 additions and 2 deletions

View File

@ -444,10 +444,10 @@ class WP_Comment_Query {
/*
* Only use the args defined in the query_var_defaults to compute the key,
* but ignore 'fields', which does not affect query results.
* but ignore 'fields', 'update_comment_meta_cache', 'update_comment_post_cache' which does not affect query results.
*/
$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
unset( $_args['fields'] );
unset( $_args['fields'], $_args['update_comment_meta_cache'], $_args['update_comment_post_cache'] );
$key = md5( serialize( $_args ) );
$last_changed = wp_cache_get_last_changed( 'comment' );

View File

@ -4941,4 +4941,31 @@ class Tests_Comment_Query extends WP_UnitTestCase {
return array( get_comment( $c ) );
}
/**
* @ticket 55460
*/
public function test_comment_cache_key_should_ignore_unset_params() {
$p = self::factory()->post->create();
$c = self::factory()->comment->create( array( 'comment_post_ID' => $p ) );
$_args = array(
'post_id' => $p,
'fields' => 'ids',
'update_comment_meta_cache' => true,
'update_comment_post_cache' => false,
);
$q1 = new WP_Comment_Query();
$q1->query( $_args );
$num_queries_all_args = get_num_queries();
// Ignore 'fields', 'update_comment_meta_cache', 'update_comment_post_cache'.
unset( $_args['fields'], $_args['update_comment_meta_cache'], $_args['update_comment_post_cache'] );
$q2 = new WP_Comment_Query();
$q2->query( $_args );
$this->assertSame( $num_queries_all_args, get_num_queries() );
}
}