Cache: Use wp_cache_*_multiple() in core functions.

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
This commit is contained in:
spacedmonkey 2022-02-11 18:51:59 +00:00
parent 6f3fcdcb4b
commit a4026420cc
9 changed files with 56 additions and 37 deletions

View File

@ -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++;

View File

@ -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.

View File

@ -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;
}

View File

@ -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' );
}
/**

View File

@ -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 );

View File

@ -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' );
}
/**

View File

@ -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' );
}
/**

View File

@ -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' );
}
//

View File

@ -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.