Make WordPress Core

Changeset 53941

Timestamp:
08/25/2022 04:21:40 AM (2 years ago)
Author:
peterwilsoncc
Message:

Query: Cache post ID database query within WP_Query.

Add object caching to the first database query in WP_Query, ie the database query for post IDs that match the desired result. Randomly ordered queries remain uncached as they are not intended to return consistent results.

Caching of ID queries is enabled by default, per the post object, term and meta caches.

Props spacedmonkey, aaroncampbell, batmoo, chriscct7, costdev, dd32, drewapicture, johnbillion, mukesh27, nacin, ocean90, peterwilsoncc, ryan, scribu, sergeybiryukov, tillkruss.
Fixes #22176.

Location:
trunk
Files:
1 added
2 edited

Legend:

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

    r53891 r53941  
    18751875
    18761876        if ( ! isset( $q['cache_results'] ) ) {
    1877             if ( wp_using_ext_object_cache() ) {
    1878                 $q['cache_results'] = false;
    1879             } else {
    1880                 $q['cache_results'] = true;
    1881             }
     1877            $q['cache_results'] = true;
    18821878        }
    18831879
     
    30733069        $this->posts = apply_filters_ref_array( 'posts_pre_query', array( null, &$this ) );
    30743070
     3071
     3072
     3073
     3074
     3075
     3076
     3077
     3078
     3079
     3080
     3081
     3082
     3083
     3084
     3085
     3086
     3087
     3088
     3089
     3090
     3091
     3092
     3093
     3094
     3095
     3096
     3097
     3098
     3099
     3100
     3101
     3102
     3103
     3104
     3105
     3106
     3107
     3108
     3109
     3110
     3111
     3112
     3113
     3114
     3115
     3116
     3117
     3118
     3119
     3120
     3121
     3122
     3123
     3124
     3125
     3126
     3127
     3128
     3129
     3130
     3131
     3132
     3133
     3134
     3135
     3136
     3137
     3138
     3139
     3140
     3141
    30753142        if ( 'ids' === $q['fields'] ) {
    30763143            if ( null === $this->posts ) {
     
    30833150            $this->set_found_posts( $q, $limits );
    30843151
     3152
     3153
     3154
     3155
     3156
     3157
     3158
     3159
     3160
     3161
    30853162            return $this->posts;
    30863163        }
     
    30953172
    30963173            /** @var int[] */
    3097             $r = array();
     3174            $post_parents = array();
     3175            $post_ids     = array();
     3176
    30983177            foreach ( $this->posts as $key => $post ) {
    30993178                $this->posts[ $key ]->ID          = (int) $post->ID;
    31003179                $this->posts[ $key ]->post_parent = (int) $post->post_parent;
    31013180
    3102                 $r[ (int) $post->ID ] = (int) $post->post_parent;
    3103             }
    3104 
    3105             return $r;
     3181                $post_parents[ (int) $post->ID ] = (int) $post->post_parent;
     3182                $post_ids[]                      = (int) $post->ID;
     3183            }
     3184
     3185            if ( $q['cache_results'] && $id_query_is_cacheable ) {
     3186                $cache_value = array(
     3187                    'posts'         => $post_ids,
     3188                    'found_posts'   => $this->found_posts,
     3189                    'max_num_pages' => $this->max_num_pages,
     3190                );
     3191
     3192                wp_cache_set( $cache_key, $cache_value, 'posts' );
     3193            }
     3194
     3195            return $post_parents;
    31063196        }
    31073197
     
    31453235                $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
    31463236
    3147                 $ids = $wpdb->get_col( $this->request );
    3148 
    3149                 if ( $ids ) {
    3150                     $this->posts = $ids;
     3237                $ids = $wpdb->get_col( $this->request );
     3238
     3239                if ( $ids ) {
     3240                    $this->posts = $ids;
    31513241                    $this->set_found_posts( $q, $limits );
    3152                     _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
     3242                    _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
    31533243                } else {
    31543244                    $this->posts = array();
     
    31663256        }
    31673257
     3258
     3259
     3260
     3261
     3262
     3263
     3264
     3265
     3266
     3267
     3268
     3269
    31683270        if ( ! empty( $this->posts ) && $q['update_menu_item_cache'] ) {
    31693271            update_menu_item_cache( $this->posts );
     
    32023304            $comments_request = "SELECT {$wpdb->comments}.comment_ID FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits";
    32033305
    3204             $key          = md5( $comments_request );
    3205             $last_changed = wp_cache_get_last_changed( 'comment' );
    3206 
    3207             $cache_key   = "comment_feed:$key:$last_changed";
    3208             $comment_ids = wp_cache_get( $cache_key, 'comment' );
     3306            $key          = md5( $comments_request );
     3307            $last_changed = wp_cache_get_last_changed( 'comment' );
     3308
     3309            $clast_changed";
     3310            $comment_ids cache_key, 'comment' );
    32093311            if ( false === $comment_ids ) {
    32103312                $comment_ids = $wpdb->get_col( $comments_request );
    3211                 wp_cache_add( $cache_key, $comment_ids, 'comment' );
     3313                wp_cache_add( $cache_key, $comment_ids, 'comment' );
    32123314            }
    32133315            _prime_comment_caches( $comment_ids, false );
  • trunk/tests/phpunit/tests/query/commentFeed.php

    r53066 r53941  
    4040            'ignore_sticky_posts'    => false,
    4141            'no_found_rows'          => true,
     42
    4243        );
    4344        $q1->query( $args );
     
    99100            'update_post_term_cache' => false,
    100101            'ignore_sticky_posts'    => false,
     102
    101103        );
    102104
Note: See TracChangeset for help on using the changeset viewer.