Make WordPress Core

Changeset 53065


Ignore:
Timestamp:
04/05/2022 01:53:59 AM (3 years ago)
Author:
peterwilsoncc
Message:

Query: Cache comments feeds in WP_Query.

Cache queries to the comments table in WP_Query for various comments feeds. Only comment IDs are stored for each feeds cache to avoid doubling up caching with each individual comment's cache.

Props spacedmonkey, boonebgorges, pbearne.
Fixes #36904.

File:
1 edited

Legend:

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

    r52990 r53065  
    27212721            $climits  = ( ! empty( $climits ) ) ? $climits : '';
    27222722
    2723             $comments = (array) $wpdb->get_results( "SELECT $distinct {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits" );
     2723            $comments_request = "SELECT $distinct {$wpdb->comments}.comment_ID FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits";
     2724
     2725            $key          = md5( $comments_request );
     2726            $last_changed = wp_cache_get_last_changed( 'comment' ) . ':' . wp_cache_get_last_changed( 'posts' );
     2727
     2728            $cache_key   = "comment_feed:$key:$last_changed";
     2729            $comment_ids = wp_cache_get( $cache_key, 'comment' );
     2730            if ( false === $comment_ids ) {
     2731                $comment_ids = $wpdb->get_col( $comments_request );
     2732                wp_cache_add( $cache_key, $comment_ids, 'comment' );
     2733            }
     2734            _prime_comment_caches( $comment_ids, false );
     2735
    27242736            // Convert to WP_Comment.
    27252737            /** @var WP_Comment[] */
    2726             $this->comments      = array_map( 'get_comment', $comments );
     2738            $this->comments      = array_map( 'get_comment', $comment_ids );
    27272739            $this->comment_count = count( $this->comments );
    27282740
     
    31653177            $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option( 'posts_per_rss' ), &$this ) );
    31663178
    3167             $comments_request = "SELECT {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits";
    3168             $comments         = $wpdb->get_results( $comments_request );
     3179            $comments_request = "SELECT {$wpdb->comments}.comment_ID FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits";
     3180
     3181            $key          = md5( $comments_request );
     3182            $last_changed = wp_cache_get_last_changed( 'comment' );
     3183
     3184            $cache_key   = "comment_feed:$key:$last_changed";
     3185            $comment_ids = wp_cache_get( $cache_key, 'comment' );
     3186            if ( false === $comment_ids ) {
     3187                $comment_ids = $wpdb->get_col( $comments_request );
     3188                wp_cache_add( $cache_key, $comment_ids, 'comment' );
     3189            }
     3190            _prime_comment_caches( $comment_ids, false );
     3191
    31693192            // Convert to WP_Comment.
    31703193            /** @var WP_Comment[] */
    3171             $this->comments      = array_map( 'get_comment', $comments );
     3194            $this->comments      = array_map( 'get_comment', $comment_ids );
    31723195            $this->comment_count = count( $this->comments );
    31733196        }
Note: See TracChangeset for help on using the changeset viewer.