Taxonomy: Increase cache hits in `WP_Term_Query`.

Increase the number of cache hits in `WP_Term_Query` by normalizing data included in the cache key.

Arguments that do not affect the SQL query, eg `update_term_meta_cache`, are removed from cache key generation. Arguments that are accepted in multiple formats, eg a string and an array, are normalized for both the cache key and the SQL query.

Props spacedmonkey.
Fixes #55352.


Built from https://develop.svn.wordpress.org/trunk@52970


git-svn-id: http://core.svn.wordpress.org/trunk@52559 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2022-03-21 03:55:03 +00:00
parent dcf9c33bc6
commit 284308048e
2 changed files with 21 additions and 29 deletions

View File

@ -526,11 +526,9 @@ class WP_Term_Query {
$this->sql_clauses['where']['exclusions'] = preg_replace( '/^\s*AND\s*/', '', $exclusions );
}
if (
( ! empty( $args['name'] ) ) ||
( is_string( $args['name'] ) && 0 !== strlen( $args['name'] ) )
) {
$names = (array) $args['name'];
$args['name'] = is_string( $args['name'] ) && 0 === strlen( $args['name'] ) ? array() : (array) $args['name'];
if ( ! empty( $args['name'] ) ) {
$names = $args['name'];
foreach ( $names as &$_name ) {
// `sanitize_term_field()` returns slashed data.
$_name = stripslashes( sanitize_term_field( 'name', $_name, 0, reset( $taxonomies ), 'db' ) );
@ -539,26 +537,18 @@ class WP_Term_Query {
$this->sql_clauses['where']['name'] = "t.name IN ('" . implode( "', '", array_map( 'esc_sql', $names ) ) . "')";
}
if (
( ! empty( $args['slug'] ) ) ||
( is_string( $args['slug'] ) && 0 !== strlen( $args['slug'] ) )
) {
if ( is_array( $args['slug'] ) ) {
$slug = array_map( 'sanitize_title', $args['slug'] );
$this->sql_clauses['where']['slug'] = "t.slug IN ('" . implode( "', '", $slug ) . "')";
} else {
$slug = sanitize_title( $args['slug'] );
$this->sql_clauses['where']['slug'] = "t.slug = '$slug'";
}
$args['slug'] = is_string( $args['slug'] ) && 0 === strlen( $args['slug'] ) ? array() : array_map( 'sanitize_title', (array) $args['slug'] );
if ( ! empty( $args['slug'] ) ) {
$slug = implode( "', '", $args['slug'] );
$this->sql_clauses['where']['slug'] = "t.slug IN ('" . $slug . "')";
}
$args['term_taxonomy_id'] = is_string( $args['term_taxonomy_id'] ) && 0 === strlen( $args['term_taxonomy_id'] ) ? array() : array_map( 'intval', (array) $args['term_taxonomy_id'] );
if ( ! empty( $args['term_taxonomy_id'] ) ) {
if ( is_array( $args['term_taxonomy_id'] ) ) {
$tt_ids = implode( ',', array_map( 'intval', $args['term_taxonomy_id'] ) );
$tt_ids = implode( ',', $args['term_taxonomy_id'] );
$this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id IN ({$tt_ids})";
} else {
$this->sql_clauses['where']['term_taxonomy_id'] = $wpdb->prepare( 'tt.term_taxonomy_id = %d', $args['term_taxonomy_id'] );
}
}
if ( ! empty( $args['name__like'] ) ) {
@ -569,13 +559,10 @@ class WP_Term_Query {
$this->sql_clauses['where']['description__like'] = $wpdb->prepare( 'tt.description LIKE %s', '%' . $wpdb->esc_like( $args['description__like'] ) . '%' );
}
$args['object_ids'] = is_string( $args['object_ids'] ) && 0 === strlen( $args['object_ids'] ) ? array() : array_map( 'intval', (array) $args['object_ids'] );
if ( ! empty( $args['object_ids'] ) ) {
$object_ids = $args['object_ids'];
if ( ! is_array( $object_ids ) ) {
$object_ids = array( $object_ids );
}
$object_ids = implode( ', ', $args['object_ids'] );
$object_ids = implode( ', ', array_map( 'intval', $object_ids ) );
$this->sql_clauses['where']['object_ids'] = "tr.object_id IN ($object_ids)";
}
@ -728,7 +715,12 @@ class WP_Term_Query {
}
// $args can be anything. Only use the args defined in defaults to compute the key.
$key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) ) ) . serialize( $taxonomies ) . $this->request );
$cache_args = wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) );
unset( $cache_args['pad_counts'], $cache_args['update_term_meta_cache'] );
if ( 'count' !== $_fields && 'all_with_object_id' !== $_fields ) {
$cache_args['fields'] = 'all';
}
$key = md5( serialize( $cache_args ) . serialize( $taxonomies ) . $this->request );
$last_changed = wp_cache_get_last_changed( 'terms' );
$cache_key = "get_terms:$key:$last_changed";
$cache = wp_cache_get( $cache_key, 'terms' );

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.0-alpha-52969';
$wp_version = '6.0-alpha-52970';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.