From a4026420cc1845aec29661239e625c5d3cfee452 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Fri, 11 Feb 2022 18:51:59 +0000 Subject: [PATCH] =?UTF-8?q?Cache:=20Use=C2=A0wp=5Fcache=5F*=5Fmultiple()?= =?UTF-8?q?=C2=A0in=20core=20functions.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implement the `wp_cache_add_multiple`, `wp_cache_set_multiple` and `wp_cache_delete_multiple` in a number of core functions after they were introduced in [52700] Props: spacedmonkey, adamsilverstein, flixos90, mitogh. Fixes: #55029. Built from https://develop.svn.wordpress.org/trunk@52707 git-svn-id: http://core.svn.wordpress.org/trunk@52296 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-comment-query.php | 6 ++++-- wp-includes/comment.php | 18 ++++++++++++------ wp-includes/meta.php | 11 ++++++----- wp-includes/ms-network.php | 9 ++++++--- wp-includes/ms-site.php | 13 +++++++++---- wp-includes/option.php | 4 +++- wp-includes/post.php | 4 +++- wp-includes/taxonomy.php | 26 ++++++++++++-------------- wp-includes/version.php | 2 +- 9 files changed, 56 insertions(+), 37 deletions(-) diff --git a/wp-includes/class-wp-comment-query.php b/wp-includes/class-wp-comment-query.php index 902bf25714..ad5fb010d3 100644 --- a/wp-includes/class-wp-comment-query.php +++ b/wp-includes/class-wp-comment-query.php @@ -1058,10 +1058,12 @@ class WP_Comment_Query { $child_ids[] = $level_comment->comment_ID; } + $data = array(); foreach ( $parent_map as $parent_id => $children ) { - $cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed"; - wp_cache_set( $cache_key, $children, 'comment' ); + $cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed"; + $data[ $cache_key ] = $children; } + wp_cache_set_multiple( $data, 'comment' ); } $level++; diff --git a/wp-includes/comment.php b/wp-includes/comment.php index be1fda4e7f..6d83b762b7 100644 --- a/wp-includes/comment.php +++ b/wp-includes/comment.php @@ -1883,9 +1883,11 @@ function wp_transition_comment_status( $new_status, $old_status, $comment ) { */ function _clear_modified_cache_on_transition_comment_status( $new_status, $old_status ) { if ( 'approved' === $new_status || 'approved' === $old_status ) { + $data = array(); foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) { - wp_cache_delete( "lastcommentmodified:$timezone", 'timeinfo' ); + $data[] = "lastcommentmodified:$timezone"; } + wp_cache_delete_multiple( $data, 'timeinfo' ); } } @@ -2045,9 +2047,11 @@ function wp_insert_comment( $commentdata ) { if ( 1 == $comment_approved ) { wp_update_comment_count( $comment_post_ID ); + $data = array(); foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) { - wp_cache_delete( "lastcommentmodified:$timezone", 'timeinfo' ); + $data[] = "lastcommentmodified:$timezone"; } + wp_cache_delete_multiple( $data, 'timeinfo' ); } clean_comment_cache( $id ); @@ -3220,9 +3224,9 @@ function xmlrpc_pingback_error( $ixr_error ) { * @param int|array $ids Comment ID or an array of comment IDs to remove from cache. */ function clean_comment_cache( $ids ) { - foreach ( (array) $ids as $id ) { - wp_cache_delete( $id, 'comment' ); - + $comment_ids = (array) $ids; + wp_cache_delete_multiple( $comment_ids, 'comment' ); + foreach ( $comment_ids as $id ) { /** * Fires immediately after a comment has been removed from the object cache. * @@ -3250,9 +3254,11 @@ function clean_comment_cache( $ids ) { * @param bool $update_meta_cache Whether to update commentmeta cache. Default true. */ function update_comment_cache( $comments, $update_meta_cache = true ) { + $data = array(); foreach ( (array) $comments as $comment ) { - wp_cache_add( $comment->comment_ID, $comment, 'comment' ); + $data[ $comment->comment_ID ] = $comment; } + wp_cache_add_multiple( $data, 'comment' ); if ( $update_meta_cache ) { // Avoid `wp_list_pluck()` in case `$comments` is passed by reference. diff --git a/wp-includes/meta.php b/wp-includes/meta.php index ed53cf579b..adbf4ba9c6 100644 --- a/wp-includes/meta.php +++ b/wp-includes/meta.php @@ -496,12 +496,11 @@ function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $ } if ( $delete_all ) { - foreach ( (array) $object_ids as $o_id ) { - wp_cache_delete( $o_id, $meta_type . '_meta' ); - } + $data = (array) $object_ids; } else { - wp_cache_delete( $object_id, $meta_type . '_meta' ); + $data = array( $object_id ); } + wp_cache_delete_multiple( $data, $meta_type . '_meta' ); /** * Fires immediately after deleting metadata of a specific type. @@ -1191,12 +1190,14 @@ function update_meta_cache( $meta_type, $object_ids ) { } } + $data = array(); foreach ( $non_cached_ids as $id ) { if ( ! isset( $cache[ $id ] ) ) { $cache[ $id ] = array(); } - wp_cache_add( $id, $cache[ $id ], $cache_key ); + $data[ $id ] = $cache[ $id ]; } + wp_cache_add_multiple( $data, $cache_key ); return $cache; } diff --git a/wp-includes/ms-network.php b/wp-includes/ms-network.php index 91001ab9a0..ecefe4c983 100644 --- a/wp-includes/ms-network.php +++ b/wp-includes/ms-network.php @@ -82,9 +82,10 @@ function clean_network_cache( $ids ) { return; } - foreach ( (array) $ids as $id ) { - wp_cache_delete( $id, 'networks' ); + $network_ids = (array) $ids; + wp_cache_delete_multiple( $network_ids, 'networks' ); + foreach ( $network_ids as $id ) { /** * Fires immediately after a network has been removed from the object cache. * @@ -110,9 +111,11 @@ function clean_network_cache( $ids ) { * @param array $networks Array of network row objects. */ function update_network_cache( $networks ) { + $data = array(); foreach ( (array) $networks as $network ) { - wp_cache_add( $network->id, $network, 'networks' ); + $data[ $network->id ] = $network; } + wp_cache_add_multiple( $data, 'networks' ); } /** diff --git a/wp-includes/ms-site.php b/wp-includes/ms-site.php index f71bbe1fee..6daee09d8a 100644 --- a/wp-includes/ms-site.php +++ b/wp-includes/ms-site.php @@ -371,12 +371,17 @@ function update_site_cache( $sites, $update_meta_cache = true ) { if ( ! $sites ) { return; } - $site_ids = array(); + $site_ids = array(); + $site_data = array(); + $blog_details_data = array(); foreach ( $sites as $site ) { - $site_ids[] = $site->blog_id; - wp_cache_add( $site->blog_id, $site, 'sites' ); - wp_cache_add( $site->blog_id . 'short', $site, 'blog-details' ); + $site_ids[] = $site->blog_id; + $site_data[ $site->blog_id ] = $site; + $blog_details_data[ $site->blog_id . 'short' ] = $site; + } + wp_cache_add_multiple( $site_data, 'sites' ); + wp_cache_add_multiple( $blog_details_data, 'blog-details' ); if ( $update_meta_cache ) { update_sitemeta_cache( $site_ids ); diff --git a/wp-includes/option.php b/wp-includes/option.php index d0eb6f9c83..f8d6703a98 100644 --- a/wp-includes/option.php +++ b/wp-includes/option.php @@ -344,13 +344,15 @@ function wp_load_core_site_options( $network_id = null ) { $core_options_in = "'" . implode( "', '", $core_options ) . "'"; $options = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE meta_key IN ($core_options_in) AND site_id = %d", $network_id ) ); + $data = array(); foreach ( $options as $option ) { $key = $option->meta_key; $cache_key = "{$network_id}:$key"; $option->meta_value = maybe_unserialize( $option->meta_value ); - wp_cache_set( $cache_key, $option->meta_value, 'site-options' ); + $data[ $cache_key ] = $option->meta_value; } + wp_cache_set_multiple( $data, 'site-options' ); } /** diff --git a/wp-includes/post.php b/wp-includes/post.php index 51752b5070..93eaed4294 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -7368,9 +7368,11 @@ function update_post_cache( &$posts ) { return; } + $data = array(); foreach ( $posts as $post ) { - wp_cache_add( $post->ID, $post, 'posts' ); + $data[ $post->ID ] = $post; } + wp_cache_add_multiple( $data, 'posts' ); } /** diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index 201864cbc1..b7f303d3bc 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -3512,10 +3512,8 @@ function clean_object_term_cache( $object_ids, $object_type ) { $taxonomies = get_object_taxonomies( $object_type ); - foreach ( $object_ids as $id ) { - foreach ( $taxonomies as $taxonomy ) { - wp_cache_delete( $id, "{$taxonomy}_relationships" ); - } + foreach ( $taxonomies as $taxonomy ) { + wp_cache_delete_multiple( $object_ids, "{$taxonomy}_relationships" ); } wp_cache_delete( 'last_changed', 'terms' ); @@ -3567,18 +3565,12 @@ function clean_term_cache( $ids, $taxonomy = '', $clean_taxonomy = true ) { foreach ( (array) $terms as $term ) { $taxonomies[] = $term->taxonomy; $ids[] = $term->term_id; - wp_cache_delete( $term->term_id, 'terms' ); } - + wp_cache_delete_multiple( $ids, 'terms' ); $taxonomies = array_unique( $taxonomies ); } else { + wp_cache_delete_multiple( $ids, 'terms' ); $taxonomies = array( $taxonomy ); - - foreach ( $taxonomies as $taxonomy ) { - foreach ( $ids as $id ) { - wp_cache_delete( $id, 'terms' ); - } - } } foreach ( $taxonomies as $taxonomy ) { @@ -3752,11 +3744,15 @@ function update_object_term_cache( $object_ids, $object_type ) { } } + $cache_values = array(); foreach ( $object_terms as $id => $value ) { foreach ( $value as $taxonomy => $terms ) { - wp_cache_add( $id, $terms, "{$taxonomy}_relationships" ); + $cache_values[ $taxonomy ][ $id ] = $terms; } } + foreach ( $cache_values as $taxonomy => $data ) { + wp_cache_add_multiple( $data, "{$taxonomy}_relationships" ); + } } /** @@ -3768,6 +3764,7 @@ function update_object_term_cache( $object_ids, $object_type ) { * @param string $taxonomy Not used. */ function update_term_cache( $terms, $taxonomy = '' ) { + $data = array(); foreach ( (array) $terms as $term ) { // Create a copy in case the array was passed by reference. $_term = clone $term; @@ -3775,8 +3772,9 @@ function update_term_cache( $terms, $taxonomy = '' ) { // Object ID should not be cached. unset( $_term->object_id ); - wp_cache_add( $term->term_id, $_term, 'terms' ); + $data[ $term->term_id ] = $_term; } + wp_cache_add_multiple( $data, 'terms' ); } // diff --git a/wp-includes/version.php b/wp-includes/version.php index efc0924872..06954bbd72 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.0-alpha-52706'; +$wp_version = '6.0-alpha-52707'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.