Changeset 56925
- Timestamp:
- 10/12/2023 11:39:05 PM (12 months ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-query.php
r56815 r56925 3193 3193 _prime_post_parent_id_caches( $post_ids ); 3194 3194 3195 $post_parent_cache_keys = array(); 3196 foreach ( $post_ids as $post_id ) { 3197 $post_parent_cache_keys[] = 'post_parent:' . (string) $post_id; 3198 } 3199 3195 3200 /** @var int[] */ 3196 $post_parents = wp_cache_get_multiple( $post_ ids, 'post_parent' );3197 3198 foreach ( $post_parents as $ id=> $post_parent ) {3201 $post_parents = wp_cache_get_multiple( $post_parent_cache_keys, 'posts' ); 3202 3203 foreach ( $post_parents as $cache_key => $post_parent ) { 3199 3204 $obj = new stdClass(); 3200 $obj->ID = (int) $id;3205 $obj->ID = (int) str_replace( 'post_parent:', '', $cache_key ); 3201 3206 $obj->post_parent = (int) $post_parent; 3202 3207 … … 3246 3251 3247 3252 /** @var int[] */ 3248 $post_parents = array(); 3249 $post_ids = array(); 3253 $post_parents = array(); 3254 $post_ids = array(); 3255 $post_parents_cache = array(); 3250 3256 3251 3257 foreach ( $this->posts as $key => $post ) { … … 3255 3261 $post_parents[ (int) $post->ID ] = (int) $post->post_parent; 3256 3262 $post_ids[] = (int) $post->ID; 3263 3264 $post_parents_cache[ 'post_parent:' . (string) $post->ID ] = (int) $post->post_parent; 3257 3265 } 3258 3266 // Prime post parent caches, so that on second run, there is not another database query. 3259 wp_cache_add_multiple( $post_parents , 'post_parent' );3267 wp_cache_add_multiple( $post_parents_cache, 'posts' ); 3260 3268 3261 3269 if ( $q['cache_results'] && $id_query_is_cacheable ) { -
trunk/src/wp-includes/post.php
r56819 r56925 7271 7271 7272 7272 wp_cache_delete( $post->ID, 'posts' ); 7273 wp_cache_delete( 'post_parent:' . (string) $post->ID, 'posts' ); 7273 7274 wp_cache_delete( $post->ID, 'post_meta' ); 7274 wp_cache_delete( $post->ID, 'post_parent' );7275 7275 7276 7276 clean_object_term_cache( $post->ID, $post->post_type ); … … 7818 7818 global $wpdb; 7819 7819 7820 $non_cached_ids = _get_non_cached_ids( $ids, 'post_parent' ); 7820 $ids = array_filter( $ids, '_validate_cache_id' ); 7821 $ids = array_unique( array_map( 'intval', $ids ), SORT_NUMERIC ); 7822 7823 if ( empty( $ids ) ) { 7824 return; 7825 } 7826 7827 $cache_keys = array(); 7828 foreach ( $ids as $id ) { 7829 $cache_keys[ $id ] = 'post_parent:' . (string) $id; 7830 } 7831 7832 $cached_data = wp_cache_get_multiple( array_values( $cache_keys ), 'posts' ); 7833 7834 $non_cached_ids = array(); 7835 foreach ( $cache_keys as $id => $cache_key ) { 7836 if ( false === $cached_data[ $cache_key ] ) { 7837 $non_cached_ids[] = $id; 7838 } 7839 } 7840 7821 7841 if ( ! empty( $non_cached_ids ) ) { 7822 7842 $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.ID, $wpdb->posts.post_parent FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) ) ); … … 7825 7845 $post_parent_data = array(); 7826 7846 foreach ( $fresh_posts as $fresh_post ) { 7827 $post_parent_data[ (int) $fresh_post->ID ] = (int) $fresh_post->post_parent;7847 $post_parent_data[ 'post_parent:' . (string) $fresh_post->ID ] = (int) $fresh_post->post_parent; 7828 7848 } 7829 7849 7830 wp_cache_add_multiple( $post_parent_data, 'post _parent' );7850 wp_cache_add_multiple( $post_parent_data, 'posts' ); 7831 7851 } 7832 7852 } -
trunk/tests/phpunit/tests/post/primePostParentsCaches.php
r56811 r56925 43 43 44 44 $this->assertSame( 1, $num_queries, 'Unexpected number of queries.' ); 45 $this->assertSameSets( array( 0 ), wp_cache_get_multiple( array( $post_id ), 'post_parent' ), 'Array of parent ids' );45 $this->assertSameSets( array( 0 ), wp_cache_get_multiple( array( "post_parent:{$post_id}" ), 'posts' ), 'Array of parent ids' ); 46 46 } 47 47 … … 54 54 $num_queries = get_num_queries() - $before_num_queries; 55 55 56 $cache_keys = array_map( 57 function ( $post_id ) { 58 return "post_parent:{$post_id}"; 59 }, 60 self::$posts 61 ); 62 56 63 $this->assertSame( 1, $num_queries, 'Unexpected number of queries.' ); 57 $this->assertSameSets( array( 0, 0, 0 ), wp_cache_get_multiple( self::$posts, 'post_parent' ), 'Array of parent ids' );64 $this->assertSameSets( array( 0, 0, 0 ), wp_cache_get_multiple( $cache_keys, 'posts' ), 'Array of parent ids' ); 58 65 } 59 66 … … 85 92 86 93 $this->assertSame( 1, $num_queries, 'Unexpected number of queries on first run' ); 87 $this->assertSameSets( array( self::$posts[0] ), wp_cache_get_multiple( array( $page_id ), 'post_parent' ), 'Array of parent ids with post 0 as parent' );94 $this->assertSameSets( array( self::$posts[0] ), wp_cache_get_multiple( array( "post_parent:{$page_id}" ), 'posts' ), 'Array of parent ids with post 0 as parent' ); 88 95 89 96 wp_update_post( … … 99 106 100 107 $this->assertSame( 1, $num_queries, 'Unexpected number of queries on second run' ); 101 $this->assertSameSets( array( self::$posts[1] ), wp_cache_get_multiple( array( $page_id ), 'post_parent' ), 'Array of parent ids with post 1 as parent' );108 $this->assertSameSets( array( self::$posts[1] ), wp_cache_get_multiple( array( "post_parent:{$page_id}" ), 'posts' ), 'Array of parent ids with post 1 as parent' ); 102 109 } 103 110 … … 122 129 123 130 $this->assertSame( 1, $num_queries, 'Unexpected number of queries on first run' ); 124 $this->assertSameSets( array( $parent_page_id ), wp_cache_get_multiple( array( $page_id ), 'post_parent' ), 'Array of parent ids with post 0 as parent' );131 $this->assertSameSets( array( $parent_page_id ), wp_cache_get_multiple( array( "post_parent:{$page_id}" ), 'posts' ), 'Array of parent ids with post 0 as parent' ); 125 132 126 133 wp_delete_post( $parent_page_id, true ); 127 134 128 135 $this->assertSame( 1, $num_queries, 'Unexpected number of queries on second run' ); 129 $this->assertSameSets( array( false ), wp_cache_get_multiple( array( $page_id ), 'post_parent' ), 'Array of parent ids with false values' );136 $this->assertSameSets( array( false ), wp_cache_get_multiple( array( "post_parent:{$page_id}" ), 'posts' ), 'Array of parent ids with false values' ); 130 137 } 131 138 } -
trunk/tests/phpunit/tests/query/cacheResults.php
r56763 r56925 739 739 $query1->query( $args ); 740 740 741 $post_ids = wp_list_pluck( $query1->posts, 'ID' ); 742 wp_cache_delete_multiple( $post_ids, 'post_parent' ); 741 $post_ids = wp_list_pluck( $query1->posts, 'ID' ); 742 $cache_keys = array_map( 743 function ( $post_id ) { 744 return "post_parent:{$post_id}"; 745 }, 746 $post_ids 747 ); 748 749 wp_cache_delete_multiple( $cache_keys, 'posts' ); 743 750 744 751 $queries_before = get_num_queries();
Note: See TracChangeset
for help on using the changeset viewer.