Make WordPress Core


Ignore:
Timestamp:
03/21/2022 03:53:33 AM (21 months ago)
Author:
peterwilsoncc
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-term-query.php

    r52836 r52970  
    527527        }
    528528
    529         if (
    530             ( ! empty( $args['name'] ) ) ||
    531             ( is_string( $args['name'] ) && 0 !== strlen( $args['name'] ) )
    532         ) {
    533             $names = (array) $args['name'];
     529        $args['name'] = is_string( $args['name'] ) && 0 === strlen( $args['name'] ) ? array() : (array) $args['name'];
     530        if ( ! empty( $args['name'] ) ) {
     531            $names = $args['name'];
    534532            foreach ( $names as &$_name ) {
    535533                // `sanitize_term_field()` returns slashed data.
     
    540538        }
    541539
    542         if (
    543             ( ! empty( $args['slug'] ) ) ||
    544             ( is_string( $args['slug'] ) && 0 !== strlen( $args['slug'] ) )
    545         ) {
    546             if ( is_array( $args['slug'] ) ) {
    547                 $slug                               = array_map( 'sanitize_title', $args['slug'] );
    548                 $this->sql_clauses['where']['slug'] = "t.slug IN ('" . implode( "', '", $slug ) . "')";
    549             } else {
    550                 $slug                               = sanitize_title( $args['slug'] );
    551                 $this->sql_clauses['where']['slug'] = "t.slug = '$slug'";
    552             }
    553         }
    554 
     540        $args['slug'] = is_string( $args['slug'] ) && 0 === strlen( $args['slug'] ) ? array() : array_map( 'sanitize_title', (array) $args['slug'] );
     541        if ( ! empty( $args['slug'] ) ) {
     542            $slug = implode( "', '", $args['slug'] );
     543
     544            $this->sql_clauses['where']['slug'] = "t.slug IN ('" . $slug . "')";
     545        }
     546
     547        $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'] );
    555548        if ( ! empty( $args['term_taxonomy_id'] ) ) {
    556             if ( is_array( $args['term_taxonomy_id'] ) ) {
    557                 $tt_ids = implode( ',', array_map( 'intval', $args['term_taxonomy_id'] ) );
    558                 $this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id IN ({$tt_ids})";
    559             } else {
    560                 $this->sql_clauses['where']['term_taxonomy_id'] = $wpdb->prepare( 'tt.term_taxonomy_id = %d', $args['term_taxonomy_id'] );
    561             }
     549            $tt_ids = implode( ',', $args['term_taxonomy_id'] );
     550
     551            $this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id IN ({$tt_ids})";
    562552        }
    563553
     
    570560        }
    571561
     562        $args['object_ids'] = is_string( $args['object_ids'] ) && 0 === strlen( $args['object_ids'] ) ? array() : array_map( 'intval', (array) $args['object_ids'] );
    572563        if ( ! empty( $args['object_ids'] ) ) {
    573             $object_ids = $args['object_ids'];
    574             if ( ! is_array( $object_ids ) ) {
    575                 $object_ids = array( $object_ids );
    576             }
    577 
    578             $object_ids                               = implode( ', ', array_map( 'intval', $object_ids ) );
     564            $object_ids = implode( ', ', $args['object_ids'] );
     565
    579566            $this->sql_clauses['where']['object_ids'] = "tr.object_id IN ($object_ids)";
    580567        }
     
    729716
    730717        // $args can be anything. Only use the args defined in defaults to compute the key.
    731         $key          = md5( serialize( wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) ) ) . serialize( $taxonomies ) . $this->request );
     718        $cache_args = wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) );
     719        unset( $cache_args['pad_counts'], $cache_args['update_term_meta_cache'] );
     720        if ( 'count' !== $_fields && 'all_with_object_id' !== $_fields ) {
     721            $cache_args['fields'] = 'all';
     722        }
     723        $key          = md5( serialize( $cache_args ) . serialize( $taxonomies ) . $this->request );
    732724        $last_changed = wp_cache_get_last_changed( 'terms' );
    733725        $cache_key    = "get_terms:$key:$last_changed";
Note: See TracChangeset for help on using the changeset viewer.