Make WordPress Core


Ignore:
Timestamp:
06/02/2016 06:27:43 PM (10 years ago)
Author:
boonebgorges
Message:

Comments: Improve caching for hierarchical queries.

Hierarchical comment queries work by first fetching the IDs of top-level
comments, and then filling the descendant tree one level at a time based on the
top-level results. When top-level comment IDs are found in the cache,
WP_Comment_Query does not generate the SQL used to fetch these comments. In
this case, the fill_descendants() query does not have enough information
to fill children. As a result, descendant comments were failing to be filled
in cases where the top-level comments were found in the cache.

This was a minor bug previously, because comment caches were not maintained
between pageloads. Since comment caches are now persistent [37613], the problem
becomes evident anywhere that a persistent object cache is in use.

The solution is to cache parent-child relationships, so that when top-level
comments are found in the cache, descendant comments should be found there as
well.

Fixes #36487.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment/query.php

    r37608 r37625  
    24312431                return $clauses;
    24322432        }
     2433
     2434        /**
     2435         * @ticket 36487
     2436         */
     2437        public function test_cache_should_be_hit_when_querying_descendants() {
     2438                global $wpdb;
     2439
     2440                $p = self::factory()->post->create();
     2441                $comment_1 = self::factory()->comment->create( array(
     2442                        'comment_post_ID' => $p,
     2443                        'comment_approved' => '1',
     2444                ) );
     2445                $comment_2 = self::factory()->comment->create( array(
     2446                        'comment_post_ID' => $p,
     2447                        'comment_approved' => '1',
     2448                        'comment_parent' => $comment_1,
     2449                ) );
     2450                $comment_3 = self::factory()->comment->create( array(
     2451                        'comment_post_ID' => $p,
     2452                        'comment_approved' => '1',
     2453                        'comment_parent' => $comment_1,
     2454                ) );
     2455                $comment_4 = self::factory()->comment->create( array(
     2456                        'comment_post_ID' => $p,
     2457                        'comment_approved' => '1',
     2458                        'comment_parent' => $comment_2,
     2459                ) );
     2460
     2461                $q1 = new WP_Comment_Query( array(
     2462                        'post_id' => $p,
     2463                        'hierarchical' => true,
     2464                ) );
     2465                $q1_ids = wp_list_pluck( $q1->comments, 'comment_ID' );
     2466
     2467                $num_queries = $wpdb->num_queries;
     2468                $q2 = new WP_Comment_Query( array(
     2469                        'post_id' => $p,
     2470                        'hierarchical' => true,
     2471                ) );
     2472                $q2_ids = wp_list_pluck( $q2->comments, 'comment_ID' );
     2473
     2474                $this->assertEqualSets( $q1_ids, $q2_ids );
     2475                $this->assertSame( $num_queries, $wpdb->num_queries );
     2476        }
     2477
    24332478        /**
    24342479         * @ticket 27571
Note: See TracChangeset for help on using the changeset viewer.