Make WordPress Core

Changeset 38784


Ignore:
Timestamp:
10/12/2016 03:29:03 PM (10 years ago)
Author:
boonebgorges
Message:

Taxonomy: Cache results of term count queries.

Fixes #38295.

Location:
trunk
Files:
2 edited

Legend:

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

    r38768 r38784  
    691691
    692692                if ( 'count' == $_fields ) {
    693                         return $wpdb->get_var( $this->request );
     693                        $count = $wpdb->get_var( $this->request );
     694                        wp_cache_set( $cache_key, $count, 'terms' );
     695                        return $count;
    694696                }
    695697
  • trunk/tests/phpunit/tests/term/query.php

    r38667 r38784  
    323323                $this->assertEqualSets( array( $terms[1] ), $found );
    324324        }
     325
     326        /**
     327         * @ticket 38295
     328         * @group cache
     329         */
     330        public function test_count_query_should_be_cached() {
     331                global $wpdb;
     332
     333                register_taxonomy( 'wptests_tax_1', 'post' );
     334
     335                $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax_1' ) );
     336
     337                $query = new WP_Term_Query( array(
     338                        'taxonomy' => 'wptests_tax_1',
     339                        'fields' => 'count',
     340                        'hide_empty' => false,
     341                ) );
     342                $count = $query->get_terms();
     343                $this->assertEquals( 2, $count );
     344
     345                $num_queries = $wpdb->num_queries;
     346
     347                $query = new WP_Term_Query( array(
     348                        'taxonomy' => 'wptests_tax_1',
     349                        'fields' => 'count',
     350                        'hide_empty' => false,
     351                ) );
     352                $count = $query->get_terms();
     353                $this->assertEquals( 2, $count );
     354                $this->assertSame( $num_queries, $wpdb->num_queries );
     355        }
     356
     357        /**
     358         * @ticket 38295
     359         * @group cache
     360         */
     361        public function test_count_query_cache_should_be_invalidated_with_incrementor_bump() {
     362                register_taxonomy( 'wptests_tax_1', 'post' );
     363
     364                $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax_1' ) );
     365
     366                $query = new WP_Term_Query( array(
     367                        'taxonomy' => 'wptests_tax_1',
     368                        'fields' => 'count',
     369                        'hide_empty' => false,
     370                ) );
     371                $count = $query->get_terms();
     372                $this->assertEquals( 2, $count );
     373
     374                wp_delete_term( $terms[0], 'wptests_tax_1' );
     375
     376                $query = new WP_Term_Query( array(
     377                        'taxonomy' => 'wptests_tax_1',
     378                        'fields' => 'count',
     379                        'hide_empty' => false,
     380                ) );
     381                $count = $query->get_terms();
     382                $this->assertEquals( 1, $count );
     383        }
    325384}
Note: See TracChangeset for help on using the changeset viewer.