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.
Built from https://develop.svn.wordpress.org/trunk@55252


git-svn-id: http://core.svn.wordpress.org/trunk@54785 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2023-02-07 12:10:18 +00:00
parent 47ca4980d8
commit 63e73fb860
3 changed files with 46 additions and 8 deletions

View File

@ -1948,6 +1948,8 @@ class WP_Query {
if ( ! isset( $q['lazy_load_term_meta'] ) ) { if ( ! isset( $q['lazy_load_term_meta'] ) ) {
$q['lazy_load_term_meta'] = $q['update_post_term_cache']; $q['lazy_load_term_meta'] = $q['update_post_term_cache'];
} elseif ( $q['lazy_load_term_meta'] ) { // Lazy loading term meta only works if term caches are primed.
$q['update_post_term_cache'] = true;
} }
if ( ! isset( $q['update_post_meta_cache'] ) ) { if ( ! isset( $q['update_post_meta_cache'] ) ) {

View File

@ -7779,7 +7779,7 @@ function wp_delete_auto_drafts() {
*/ */
function wp_queue_posts_for_term_meta_lazyload( $posts ) { function wp_queue_posts_for_term_meta_lazyload( $posts ) {
$post_type_taxonomies = array(); $post_type_taxonomies = array();
$term_ids = array(); $prime_post_terms = array();
foreach ( $posts as $post ) { foreach ( $posts as $post ) {
if ( ! ( $post instanceof WP_Post ) ) { if ( ! ( $post instanceof WP_Post ) ) {
continue; continue;
@ -7790,16 +7790,52 @@ function wp_queue_posts_for_term_meta_lazyload( $posts ) {
} }
foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) { foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) {
// Term cache should already be primed by `update_post_term_cache()`. $prime_post_terms[ $taxonomy ][] = $post->ID;
$terms = get_object_term_cache( $post->ID, $taxonomy ); }
if ( false !== $terms ) { }
foreach ( $terms as $term ) {
if ( ! in_array( $term->term_id, $term_ids, true ) ) { $term_ids = array();
$term_ids[] = $term->term_id; if ( $prime_post_terms ) {
$prime_term_ids = array();
$prime_taxonomy_ids = array();
foreach ( $prime_post_terms as $taxonomy => $post_ids ) {
$cached_term_ids = wp_cache_get_multiple( $post_ids, "{$taxonomy}_relationships" );
if ( is_array( $cached_term_ids ) ) {
$cached_term_ids = array_filter( $cached_term_ids );
foreach ( $cached_term_ids as $term_ids ) {
// Backward compatibility for if a plugin is putting objects into the cache, rather than IDs.
foreach ( $term_ids as $term_id ) {
if ( is_numeric( $term_id ) ) {
$prime_term_ids[] = (int) $term_id;
$prime_taxonomy_ids[ $taxonomy ][] = (int) $term_id;
} elseif ( isset( $term_id->term_id ) ) {
$prime_taxonomy_ids[ $taxonomy ][] = (int) $term_id->term_id;
$prime_term_ids[] = (int) $term_id->term_id;
}
} }
} }
} }
} }
if ( $prime_term_ids ) {
$prime_term_ids = array_unique( $prime_term_ids );
// Do not prime term meta at this point, let the lazy loader take care of that.
_prime_term_caches( $prime_term_ids, false );
foreach ( $prime_taxonomy_ids as $taxonomy => $_term_ids ) {
foreach ( $_term_ids as $term_id ) {
if ( in_array( $term_id, $term_ids, true ) ) {
continue;
}
$term = get_term( $term_id, $taxonomy );
if ( is_wp_error( $term ) ) {
continue;
}
$term_ids[] = $term_id;
}
}
}
} }
if ( $term_ids ) { if ( $term_ids ) {

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.2-alpha-55251'; $wp_version = '6.2-alpha-55252';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.