Changeset 53941
- Timestamp:
- 08/25/2022 04:21:40 AM (2 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-query.php
r53891 r53941 1875 1875 1876 1876 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; 1882 1878 } 1883 1879 … … 3073 3069 $this->posts = apply_filters_ref_array( 'posts_pre_query', array( null, &$this ) ); 3074 3070 3071 /* 3072 * Ensure the ID database query is able to be cached. 3073 * 3074 * Random queries are expected to have unpredictable results and 3075 * cannot be cached. Note the space before `RAND` in the string 3076 * search, that to ensure against a collision with another 3077 * function. 3078 */ 3079 $id_query_is_cacheable = ! str_contains( strtoupper( $orderby ), ' RAND(' ); 3080 if ( $q['cache_results'] && $id_query_is_cacheable ) { 3081 $cache_args = $q; 3082 3083 unset( 3084 $cache_args['suppress_filters'], 3085 $cache_args['cache_results'], 3086 $cache_args['fields'], 3087 $cache_args['update_post_meta_cache'], 3088 $cache_args['update_post_term_cache'], 3089 $cache_args['lazy_load_term_meta'], 3090 $cache_args['update_menu_item_cache'] 3091 ); 3092 3093 $new_request = str_replace( $fields, "{$wpdb->posts}.*", $this->request ); 3094 $key = md5( serialize( $cache_args ) . $new_request ); 3095 3096 $last_changed = wp_cache_get_last_changed( 'posts' ); 3097 if ( ! empty( $this->tax_query->queried_terms ) ) { 3098 $last_changed .= wp_cache_get_last_changed( 'terms' ); 3099 } 3100 3101 $cache_key = "wp_query:$key:$last_changed"; 3102 3103 if ( null === $this->posts ) { 3104 $cached_results = wp_cache_get( $cache_key, 'posts' ); 3105 3106 if ( $cached_results ) { 3107 if ( 'ids' === $q['fields'] ) { 3108 /** @var int[] */ 3109 $this->posts = array_map( 'intval', $cached_results['posts'] ); 3110 } else { 3111 _prime_post_caches( $cached_results['posts'], $q['update_post_term_cache'], $q['update_post_meta_cache'] ); 3112 /** @var WP_Post[] */ 3113 $this->posts = array_map( 'get_post', $cached_results['posts'] ); 3114 } 3115 3116 $this->post_count = count( $this->posts ); 3117 $this->found_posts = $cached_results['found_posts']; 3118 $this->max_num_pages = $cached_results['max_num_pages']; 3119 3120 if ( 'ids' === $q['fields'] ) { 3121 return $this->posts; 3122 } elseif ( 'id=>parent' === $q['fields'] ) { 3123 /** @var int[] */ 3124 $post_parents = array(); 3125 3126 foreach ( $this->posts as $key => $post ) { 3127 $obj = new stdClass(); 3128 $obj->ID = (int) $post->ID; 3129 $obj->post_parent = (int) $post->post_parent; 3130 3131 $this->posts[ $key ] = $obj; 3132 3133 $post_parents[ $obj->ID ] = $obj->post_parent; 3134 } 3135 3136 return $post_parents; 3137 } 3138 } 3139 } 3140 } 3141 3075 3142 if ( 'ids' === $q['fields'] ) { 3076 3143 if ( null === $this->posts ) { … … 3083 3150 $this->set_found_posts( $q, $limits ); 3084 3151 3152 if ( $q['cache_results'] && $id_query_is_cacheable ) { 3153 $cache_value = array( 3154 'posts' => $this->posts, 3155 'found_posts' => $this->found_posts, 3156 'max_num_pages' => $this->max_num_pages, 3157 ); 3158 3159 wp_cache_set( $cache_key, $cache_value, 'posts' ); 3160 } 3161 3085 3162 return $this->posts; 3086 3163 } … … 3095 3172 3096 3173 /** @var int[] */ 3097 $r = array(); 3174 $post_parents = array(); 3175 $post_ids = array(); 3176 3098 3177 foreach ( $this->posts as $key => $post ) { 3099 3178 $this->posts[ $key ]->ID = (int) $post->ID; 3100 3179 $this->posts[ $key ]->post_parent = (int) $post->post_parent; 3101 3180 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; 3106 3196 } 3107 3197 … … 3145 3235 $this->request = apply_filters( 'posts_request_ids', $this->request, $this ); 3146 3236 3147 $ ids = $wpdb->get_col( $this->request );3148 3149 if ( $ ids ) {3150 $this->posts = $ ids;3237 $post_ids = $wpdb->get_col( $this->request ); 3238 3239 if ( $post_ids ) { 3240 $this->posts = $post_ids; 3151 3241 $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( $post_ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] ); 3153 3243 } else { 3154 3244 $this->posts = array(); … … 3166 3256 } 3167 3257 3258 if ( $q['cache_results'] && $id_query_is_cacheable ) { 3259 $post_ids = wp_list_pluck( $this->posts, 'ID' ); 3260 3261 $cache_value = array( 3262 'posts' => $post_ids, 3263 'found_posts' => $this->found_posts, 3264 'max_num_pages' => $this->max_num_pages, 3265 ); 3266 3267 wp_cache_set( $cache_key, $cache_value, 'posts' ); 3268 } 3269 3168 3270 if ( ! empty( $this->posts ) && $q['update_menu_item_cache'] ) { 3169 3271 update_menu_item_cache( $this->posts ); … … 3202 3304 $comments_request = "SELECT {$wpdb->comments}.comment_ID FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits"; 3203 3305 3204 $ key = md5( $comments_request );3205 $ last_changed = wp_cache_get_last_changed( 'comment' );3206 3207 $c ache_key = "comment_feed:$key:$last_changed";3208 $comment_ids = wp_cache_get( $cache_key, 'comment' );3306 $comment_key = md5( $comments_request ); 3307 $comment_last_changed = wp_cache_get_last_changed( 'comment' ); 3308 3309 $comment_cache_key = "comment_feed:$comment_key:$comment_last_changed"; 3310 $comment_ids = wp_cache_get( $comment_cache_key, 'comment' ); 3209 3311 if ( false === $comment_ids ) { 3210 3312 $comment_ids = $wpdb->get_col( $comments_request ); 3211 wp_cache_add( $c ache_key, $comment_ids, 'comment' );3313 wp_cache_add( $comment_cache_key, $comment_ids, 'comment' ); 3212 3314 } 3213 3315 _prime_comment_caches( $comment_ids, false ); -
trunk/tests/phpunit/tests/query/commentFeed.php
r53066 r53941 40 40 'ignore_sticky_posts' => false, 41 41 'no_found_rows' => true, 42 'cache_results' => false, 42 43 ); 43 44 $q1->query( $args ); … … 99 100 'update_post_term_cache' => false, 100 101 'ignore_sticky_posts' => false, 102 'cache_results' => false, 101 103 ); 102 104
Note: See TracChangeset
for help on using the changeset viewer.