Make WordPress Core


Ignore:
Timestamp:
10/12/2023 11:39:05 PM (9 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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    }
Note: See TracChangeset for help on using the changeset viewer.