Make WordPress Core


Ignore:
Timestamp:
05/11/2023 12:25:51 PM (3 years ago)
Author:
spacedmonkey
Message:

Comments: Always lazily load comment meta.

In [34270] introduced lazy loading of comment meta. However, this was only in the context of WP_Query. Other parts of the codebase, like WP_Comment_Query did not lazily load comment meta. In this change, calls to update_meta_cache are now replaced with wp_lazyload_comment_meta, that instead of priming comment meta caches, just adds them to the queue to be primed it ever called. This results in far less database queries, as there a number of places where comment meta is being primed unnecessarily and never used. Adding everything to the comment meta queue, also means that if comment meta is used, that is all loaded in a single database / cache call.

Follow on from [55671], [55747].

Props spacedmonkey, peterwilsoncc, flixos90, mukesh27.
Fixes #57801.

File:
1 edited

Legend:

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

    r55745 r55749  
    1212     * @covers ::update_comment_meta
    1313     */
    14     public function test_update_comment_meta_cache_should_default_to_true() {
     14    public function test_update_comment_meta_cache_should_default_to_lazy_loading() {
    1515        $p           = self::factory()->post->create( array( 'post_status' => 'publish' ) );
    1616        $comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
     
    2323        clean_comment_cache( $comment_ids );
    2424
    25         $q = new WP_Comment_Query(
     25        $num_queries = get_num_queries();
     26        $q           = new WP_Comment_Query(
    2627            array(
    2728                'post_ID' => $p,
     
    2930        );
    3031
     32        $this->assertSame( 2, get_num_queries() - $num_queries, 'Querying comments is expected to make two queries' );
     33
    3134        $num_queries = get_num_queries();
    3235        foreach ( $comment_ids as $cid ) {
     
    3437        }
    3538
    36         $this->assertSame( $num_queries, get_num_queries() );
    37     }
    38 
    39     /**
    40      * @ticket 16894
    41      *
    42      * @covers ::update_comment_meta
    43      */
    44     public function test_update_comment_meta_cache_true() {
     39        $this->assertSame( 1, get_num_queries() - $num_queries, 'Querying comments is expected to make two queries' );
     40    }
     41
     42    /**
     43     * @ticket 57801
     44     *
     45     * @covers ::wp_lazyload_comment_meta
     46     */
     47    public function test_update_comment_meta_cache_should_default_to_lazy_loading_fields_id() {
    4548        $p           = self::factory()->post->create( array( 'post_status' => 'publish' ) );
    4649        $comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
     
    5356        clean_comment_cache( $comment_ids );
    5457
    55         $q = new WP_Comment_Query(
     58        $num_queries = get_num_queries();
     59        $q           = new WP_Comment_Query(
     60            array(
     61                'post_ID' => $p,
     62                'fields'  => 'ids',
     63            )
     64        );
     65
     66        $this->assertSame( 1, get_num_queries() - $num_queries, 'Querying comments is expected to make two queries' );
     67
     68        $num_queries = get_num_queries();
     69        foreach ( $comment_ids as $cid ) {
     70            get_comment_meta( $cid, 'foo', 'bar' );
     71        }
     72
     73        $this->assertSame( 1, get_num_queries() - $num_queries, 'Comment meta is expected to be lazy loaded' );
     74    }
     75
     76    /**
     77     * @ticket 16894
     78     *
     79     * @covers ::update_comment_meta
     80     */
     81    public function test_update_comment_meta_cache_true() {
     82        $p           = self::factory()->post->create( array( 'post_status' => 'publish' ) );
     83        $comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
     84
     85        foreach ( $comment_ids as $cid ) {
     86            update_comment_meta( $cid, 'foo', 'bar' );
     87        }
     88
     89        // Clear comment cache, just in case.
     90        clean_comment_cache( $comment_ids );
     91
     92        $num_queries = get_num_queries();
     93        $q           = new WP_Comment_Query(
    5694            array(
    5795                'post_ID'                   => $p,
     
    5997            )
    6098        );
     99        $this->assertSame( 2, get_num_queries() - $num_queries, 'Comments should be queries and primed in two database queries' );
    61100
    62101        $num_queries = get_num_queries();
     
    65104        }
    66105
    67         $this->assertSame( $num_queries, get_num_queries() );
     106        $this->assertSame( 1, get_num_queries() - $num_queries, 'Comment meta should be loaded in one database query' );
     107    }
     108
     109    /**
     110     * @ticket 57801
     111     *
     112     * @covers ::update_comment_meta
     113     */
     114    public function test_update_comment_meta_cache_true_multiple() {
     115        $posts           = self::factory()->post->create_many( 3 );
     116        $all_comment_ids = array();
     117        foreach ( $posts as $p ) {
     118            $comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
     119
     120            foreach ( $comment_ids as $cid ) {
     121                update_comment_meta( $cid, 'foo', 'bar' );
     122                $all_comment_ids[] = $cid;
     123            }
     124
     125            $num_queries = get_num_queries();
     126            $q           = new WP_Comment_Query(
     127                array(
     128                    'post_ID'                   => $p,
     129                    'update_comment_meta_cache' => true,
     130                )
     131            );
     132            $this->assertSame( 1, get_num_queries() - $num_queries, 'Comment query should only add one query' );
     133        }
     134
     135        $filter = new MockAction();
     136        add_filter( 'update_comment_metadata_cache', array( $filter, 'filter' ), 10, 2 );
     137        $num_queries = get_num_queries();
     138        get_comment_meta( $comment_ids[0], 'foo', 'bar' );
     139
     140        $this->assertSame( 1, get_num_queries() - $num_queries, 'Comment meta should be loaded in one database query' );
     141        $args              = $filter->get_args();
     142        $first             = reset( $args );
     143        $prime_comment_ids = end( $first );
     144        $this->assertSameSets( $prime_comment_ids, $all_comment_ids, 'All comment meta should be loaded all at once' );
    68145    }
    69146
     
    93170        }
    94171
    95         $this->assertSame( $num_queries + 3, get_num_queries() );
     172        $this->assertSame( 3, get_num_queries() - $num_queries );
    96173    }
    97174
     
    121198                $num_queries = get_num_queries();
    122199                get_comment_meta( $comment_ids[0], 'sauce' );
    123                 $this->assertSame( $num_queries + 1, get_num_queries() );
     200                $this->assertSame( 1, get_num_queries() - $num_queries );
    124201
    125202                // Second and third requests should be in cache.
    126203                get_comment_meta( $comment_ids[1], 'sauce' );
    127204                get_comment_meta( $comment_ids[2], 'sauce' );
    128                 $this->assertSame( $num_queries + 1, get_num_queries() );
     205                $this->assertSame( 1, get_num_queries() - $num_queries );
    129206            }
    130207        }
     
    135212     *
    136213     * @covers ::get_comment_meta
     214     * @covers ::wp_lazyload_comment_meta
    137215     */
    138216    public function test_comment_meta_should_be_lazy_loaded_in_comment_feed_queries() {
     
    183261     *
    184262     * @covers ::get_comment_meta
     263     * @covers ::wp_lazyload_comment_meta
    185264     */
    186265    public function test_comment_meta_should_be_lazy_loaded_in_single_post_comment_feed_queries() {
Note: See TracChangeset for help on using the changeset viewer.