Make WordPress Core


Ignore:
Timestamp:
02/07/2023 12:07:46 PM (22 months ago)
Author:
spacedmonkey
Message:

Taxonomy: Implement wp_cache_get_multiple in wp_queue_posts_for_term_meta_lazyload.

In [47938] the wp_cache_get_multiple function was added to core. This function allows for multiple cache keys to be received from cache in a single function call. wp_queue_posts_for_term_meta_lazyload function does many calls to cache. To get taxonomy relationship for multiple posts and get all terms. Replace calls to get_object_term_cache with calls to wp_cache_get_multiple and _prime_term_caches. This improves performance on sites that implement the wp_cache_get_multiple in their object caching drop-in.

Props spacedmonkey, ocean90, SergeyBiryukov, costdev, flixos90, joemcgill, 10upsimon.
Fixes #57150.

File:
1 edited

Legend:

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

    r55217 r55252  
    77807780function wp_queue_posts_for_term_meta_lazyload( $posts ) {
    77817781    $post_type_taxonomies = array();
    7782     $term_ids             = array();
     7782    $prime_post_terms     = array();
    77837783    foreach ( $posts as $post ) {
    77847784        if ( ! ( $post instanceof WP_Post ) ) {
     
    77917791
    77927792        foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) {
    7793             // Term cache should already be primed by `update_post_term_cache()`.
    7794             $terms = get_object_term_cache( $post->ID, $taxonomy );
    7795             if ( false !== $terms ) {
    7796                 foreach ( $terms as $term ) {
    7797                     if ( ! in_array( $term->term_id, $term_ids, true ) ) {
    7798                         $term_ids[] = $term->term_id;
     7793            $prime_post_terms[ $taxonomy ][] = $post->ID;
     7794        }
     7795    }
     7796
     7797    $term_ids = array();
     7798    if ( $prime_post_terms ) {
     7799        $prime_term_ids     = array();
     7800        $prime_taxonomy_ids = array();
     7801        foreach ( $prime_post_terms as $taxonomy => $post_ids ) {
     7802            $cached_term_ids = wp_cache_get_multiple( $post_ids, "{$taxonomy}_relationships" );
     7803            if ( is_array( $cached_term_ids ) ) {
     7804                $cached_term_ids = array_filter( $cached_term_ids );
     7805                foreach ( $cached_term_ids as $term_ids ) {
     7806                    // Backward compatibility for if a plugin is putting objects into the cache, rather than IDs.
     7807                    foreach ( $term_ids as $term_id ) {
     7808                        if ( is_numeric( $term_id ) ) {
     7809                            $prime_term_ids[]                  = (int) $term_id;
     7810                            $prime_taxonomy_ids[ $taxonomy ][] = (int) $term_id;
     7811                        } elseif ( isset( $term_id->term_id ) ) {
     7812                            $prime_taxonomy_ids[ $taxonomy ][] = (int) $term_id->term_id;
     7813                            $prime_term_ids[]                  = (int) $term_id->term_id;
     7814                        }
    77997815                    }
     7816                }
     7817            }
     7818        }
     7819
     7820        if ( $prime_term_ids ) {
     7821            $prime_term_ids = array_unique( $prime_term_ids );
     7822            // Do not prime term meta at this point, let the lazy loader take care of that.
     7823            _prime_term_caches( $prime_term_ids, false );
     7824
     7825            foreach ( $prime_taxonomy_ids as $taxonomy => $_term_ids ) {
     7826                foreach ( $_term_ids as $term_id ) {
     7827                    if ( in_array( $term_id, $term_ids, true ) ) {
     7828                        continue;
     7829                    }
     7830                    $term = get_term( $term_id, $taxonomy );
     7831                    if ( is_wp_error( $term ) ) {
     7832                        continue;
     7833                    }
     7834
     7835                    $term_ids[] = $term_id;
    78007836                }
    78017837            }
Note: See TracChangeset for help on using the changeset viewer.