Make WordPress Core

Changeset 40921


Ignore:
Timestamp:
06/22/2017 03:18:18 AM (8 years ago)
Author:
boonebgorges
Message:

Cache results in get_objects_in_term().

This helps to reduce database queries when generating nav menus.

Props spacedmonkey.
Fixes #37094.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r40920 r40921  
    651651    $term_ids = "'" . implode( "', '", $term_ids ) . "'";
    652652
    653     $object_ids = $wpdb->get_col("SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order");
     653    $sql = "SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order";
     654
     655    $last_changed = wp_cache_get_last_changed( 'terms' );
     656    $cache_key = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed";
     657    $cache = wp_cache_get( $cache_key, 'terms' );
     658    if ( false === $cache ) {
     659        $object_ids = $wpdb->get_col( $sql );
     660        wp_cache_set( $cache_key, $object_ids, 'terms' );
     661    } else {
     662        $object_ids = (array) $cache;
     663    }
    654664
    655665    if ( ! $object_ids ){
  • trunk/tests/phpunit/tests/taxonomy.php

    r40049 r40921  
    293293
    294294    /**
     295     * @ticket 37094
     296     */
     297    public function test_term_assignment_should_invalidate_get_objects_in_term_cache() {
     298        register_taxonomy( 'wptests_tax', 'post' );
     299
     300        $posts = self::factory()->post->create_many( 2 );
     301        $term_id = self::factory()->term->create( array(
     302            'taxonomy' => 'wptests_tax',
     303        ) );
     304
     305        wp_set_object_terms( $posts[1], $term_id, 'wptests_tax' );
     306
     307        // Prime cache.
     308        $before = get_objects_in_term( $term_id, 'wptests_tax' );
     309        $this->assertEqualSets( array( $posts[1] ), $before );
     310
     311        wp_set_object_terms( $posts[1], array(), 'wptests_tax' );
     312
     313        $after = get_objects_in_term( $term_id, 'wptests_tax' );
     314        $this->assertSame( array(), $after );
     315    }
     316
     317    /**
     318     * @ticket 37094
     319     */
     320    public function test_term_deletion_should_invalidate_get_objects_in_term_cache() {
     321        register_taxonomy( 'wptests_tax', 'post' );
     322
     323        $posts = self::factory()->post->create_many( 2 );
     324        $term_id = self::factory()->term->create( array(
     325            'taxonomy' => 'wptests_tax',
     326        ) );
     327
     328        wp_set_object_terms( $posts[1], $term_id, 'wptests_tax' );
     329
     330        // Prime cache.
     331        $before = get_objects_in_term( $term_id, 'wptests_tax' );
     332        $this->assertEqualSets( array( $posts[1] ), $before );
     333
     334        wp_delete_term( $term_id, 'wptests_tax' );
     335
     336        $after = get_objects_in_term( $term_id, 'wptests_tax' );
     337        $this->assertSame( array(), $after );
     338    }
     339
     340    /**
     341     * @ticket 37094
     342     */
     343    public function test_post_deletion_should_invalidate_get_objects_in_term_cache() {
     344        register_taxonomy( 'wptests_tax', 'post' );
     345
     346        $posts = self::factory()->post->create_many( 2 );
     347        $term_id = self::factory()->term->create( array(
     348            'taxonomy' => 'wptests_tax',
     349        ) );
     350
     351        wp_set_object_terms( $posts[1], $term_id, 'wptests_tax' );
     352
     353        // Prime cache.
     354        $before = get_objects_in_term( $term_id, 'wptests_tax' );
     355        $this->assertEqualSets( array( $posts[1] ), $before );
     356
     357        wp_delete_post( $posts[1], true );
     358
     359        $after = get_objects_in_term( $term_id, 'wptests_tax' );
     360        $this->assertSame( array(), $after );
     361    }
     362
     363    /**
    295364     * @ticket 25706
    296365     */
Note: See TracChangeset for help on using the changeset viewer.