Make WordPress Core

Changeset 56925


Ignore:
Timestamp:
10/12/2023 11:39:05 PM (12 months ago)
Author:
peterwilsoncc
Message:

Query: Cache post parent IDs in posts group.

Move the cache of post parent IDs from the dedicated group post_parents to posts. This maintains backward compatibility for clearing all post object related data by calling wp_cache_flush_group( 'posts' ).

Post parent IDs are now cached with with the prefix post_parent: in the posts group.

Follow up to [56763].

Props spacedmonkey, joemcgill, peterwilsoncc.
See #59188.

Location:
trunk
Files:
4 edited

Legend:

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

    r56815 r56925  
    31933193                        _prime_post_parent_id_caches( $post_ids );
    31943194
     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
    31953200                        /** @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 ) {
    31993204                            $obj              = new stdClass();
    3200                             $obj->ID          = (int) $id;
     3205                            $obj->ID          = (int) str_replace( 'post_parent:', '', $cache_key );
    32013206                            $obj->post_parent = (int) $post_parent;
    32023207
     
    32463251
    32473252            /** @var int[] */
    3248             $post_parents = array();
    3249             $post_ids     = array();
     3253            $post_parents       = array();
     3254            $post_ids           = array();
     3255            $post_parents_cache = array();
    32503256
    32513257            foreach ( $this->posts as $key => $post ) {
     
    32553261                $post_parents[ (int) $post->ID ] = (int) $post->post_parent;
    32563262                $post_ids[]                      = (int) $post->ID;
     3263
     3264                $post_parents_cache[ 'post_parent:' . (string) $post->ID ] = (int) $post->post_parent;
    32573265            }
    32583266            // 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' );
    32603268
    32613269            if ( $q['cache_results'] && $id_query_is_cacheable ) {
  • trunk/src/wp-includes/post.php

    r56819 r56925  
    72717271
    72727272    wp_cache_delete( $post->ID, 'posts' );
     7273    wp_cache_delete( 'post_parent:' . (string) $post->ID, 'posts' );
    72737274    wp_cache_delete( $post->ID, 'post_meta' );
    7274     wp_cache_delete( $post->ID, 'post_parent' );
    72757275
    72767276    clean_object_term_cache( $post->ID, $post->post_type );
     
    78187818    global $wpdb;
    78197819
    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
    78217841    if ( ! empty( $non_cached_ids ) ) {
    78227842        $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 ) ) );
     
    78257845            $post_parent_data = array();
    78267846            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;
    78287848            }
    78297849
    7830             wp_cache_add_multiple( $post_parent_data, 'post_parent' );
     7850            wp_cache_add_multiple( $post_parent_data, 'posts' );
    78317851        }
    78327852    }
  • trunk/tests/phpunit/tests/post/primePostParentsCaches.php

    r56811 r56925  
    4343
    4444        $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' );
    4646    }
    4747
     
    5454        $num_queries = get_num_queries() - $before_num_queries;
    5555
     56        $cache_keys = array_map(
     57            function ( $post_id ) {
     58                return "post_parent:{$post_id}";
     59            },
     60            self::$posts
     61        );
     62
    5663        $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' );
    5865    }
    5966
     
    8592
    8693        $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' );
    8895
    8996        wp_update_post(
     
    99106
    100107        $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' );
    102109    }
    103110
     
    122129
    123130        $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' );
    125132
    126133        wp_delete_post( $parent_page_id, true );
    127134
    128135        $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' );
    130137    }
    131138}
  • trunk/tests/phpunit/tests/query/cacheResults.php

    r56763 r56925  
    739739        $query1->query( $args );
    740740
    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' );
    743750
    744751        $queries_before = get_num_queries();
Note: See TracChangeset for help on using the changeset viewer.