Make WordPress Core

Changeset 52707


Ignore:
Timestamp:
02/11/2022 06:50:08 PM (3 years ago)
Author:
spacedmonkey
Message:

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

Implement the wp_cache_add_multiplewp_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.

Location:
trunk/src/wp-includes
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-comment-query.php

    r52332 r52707  
    10591059                }
    10601060
     1061                $data = array();
    10611062                foreach ( $parent_map as $parent_id => $children ) {
    1062                     $cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed";
    1063                     wp_cache_set( $cache_key, $children, 'comment' );
    1064                 }
     1063                    $cache_key          = "get_comment_child_ids:$parent_id:$key:$last_changed";
     1064                    $data[ $cache_key ] = $children;
     1065                }
     1066                wp_cache_set_multiple( $data, 'comment' );
    10651067            }
    10661068
  • trunk/src/wp-includes/comment.php

    r52332 r52707  
    18841884function _clear_modified_cache_on_transition_comment_status( $new_status, $old_status ) {
    18851885    if ( 'approved' === $new_status || 'approved' === $old_status ) {
     1886        $data = array();
    18861887        foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
    1887             wp_cache_delete( "lastcommentmodified:$timezone", 'timeinfo' );
    1888         }
     1888            $data[] = "lastcommentmodified:$timezone";
     1889        }
     1890        wp_cache_delete_multiple( $data, 'timeinfo' );
    18891891    }
    18901892}
     
    20462048        wp_update_comment_count( $comment_post_ID );
    20472049
     2050        $data = array();
    20482051        foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
    2049             wp_cache_delete( "lastcommentmodified:$timezone", 'timeinfo' );
    2050         }
     2052            $data[] = "lastcommentmodified:$timezone";
     2053        }
     2054        wp_cache_delete_multiple( $data, 'timeinfo' );
    20512055    }
    20522056
     
    32213225 */
    32223226function clean_comment_cache( $ids ) {
    3223     foreach ( (array) $ids as $id ) {
    3224         wp_cache_delete( $id, 'comment' );
    3225 
     3227    $comment_ids = (array) $ids;
     3228    wp_cache_delete_multiple( $comment_ids, 'comment' );
     3229    foreach ( $comment_ids as $id ) {
    32263230        /**
    32273231         * Fires immediately after a comment has been removed from the object cache.
     
    32513255 */
    32523256function update_comment_cache( $comments, $update_meta_cache = true ) {
     3257    $data = array();
    32533258    foreach ( (array) $comments as $comment ) {
    3254         wp_cache_add( $comment->comment_ID, $comment, 'comment' );
    3255     }
     3259        $data[ $comment->comment_ID ] = $comment;
     3260    }
     3261    wp_cache_add_multiple( $data, 'comment' );
    32563262
    32573263    if ( $update_meta_cache ) {
  • trunk/src/wp-includes/meta.php

    r52140 r52707  
    497497
    498498    if ( $delete_all ) {
    499         foreach ( (array) $object_ids as $o_id ) {
    500             wp_cache_delete( $o_id, $meta_type . '_meta' );
    501         }
     499        $data = (array) $object_ids;
    502500    } else {
    503         wp_cache_delete( $object_id, $meta_type . '_meta' );
    504     }
     501        $data = array( $object_id );
     502    }
     503    wp_cache_delete_multiple( $data, $meta_type . '_meta' );
    505504
    506505    /**
     
    11921191    }
    11931192
     1193    $data = array();
    11941194    foreach ( $non_cached_ids as $id ) {
    11951195        if ( ! isset( $cache[ $id ] ) ) {
    11961196            $cache[ $id ] = array();
    11971197        }
    1198         wp_cache_add( $id, $cache[ $id ], $cache_key );
    1199     }
     1198        $data[ $id ] = $cache[ $id ];
     1199    }
     1200    wp_cache_add_multiple( $data, $cache_key );
    12001201
    12011202    return $cache;
  • trunk/src/wp-includes/ms-network.php

    r49193 r52707  
    8383    }
    8484
    85     foreach ( (array) $ids as $id ) {
    86         wp_cache_delete( $id, 'networks' );
     85    $network_ids = (array) $ids;
     86    wp_cache_delete_multiple( $network_ids, 'networks' );
    8787
     88    foreach ( $network_ids as $id ) {
    8889        /**
    8990         * Fires immediately after a network has been removed from the object cache.
     
    111112 */
    112113function update_network_cache( $networks ) {
     114    $data = array();
    113115    foreach ( (array) $networks as $network ) {
    114         wp_cache_add( $network->id, $network, 'networks' );
     116        $data[ $network->id ] = $network;
    115117    }
     118    wp_cache_add_multiple( $data, 'networks' );
    116119}
    117120
  • trunk/src/wp-includes/ms-site.php

    r52205 r52707  
    372372        return;
    373373    }
    374     $site_ids = array();
     374    $site_ids          = array();
     375    $site_data         = array();
     376    $blog_details_data = array();
    375377    foreach ( $sites as $site ) {
    376         $site_ids[] = $site->blog_id;
    377         wp_cache_add( $site->blog_id, $site, 'sites' );
    378         wp_cache_add( $site->blog_id . 'short', $site, 'blog-details' );
    379     }
     378        $site_ids[]                                    = $site->blog_id;
     379        $site_data[ $site->blog_id ]                   = $site;
     380        $blog_details_data[ $site->blog_id . 'short' ] = $site;
     381
     382    }
     383    wp_cache_add_multiple( $site_data, 'sites' );
     384    wp_cache_add_multiple( $blog_details_data, 'blog-details' );
    380385
    381386    if ( $update_meta_cache ) {
  • trunk/src/wp-includes/option.php

    r52694 r52707  
    345345    $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 ) );
    346346
     347    $data = array();
    347348    foreach ( $options as $option ) {
    348349        $key                = $option->meta_key;
     
    350351        $option->meta_value = maybe_unserialize( $option->meta_value );
    351352
    352         wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
    353     }
     353        $data[ $cache_key ] = $option->meta_value;
     354    }
     355    wp_cache_set_multiple( $data, 'site-options' );
    354356}
    355357
  • trunk/src/wp-includes/post.php

    r52691 r52707  
    73697369    }
    73707370
     7371    $data = array();
    73717372    foreach ( $posts as $post ) {
    7372         wp_cache_add( $post->ID, $post, 'posts' );
    7373     }
     7373        $data[ $post->ID ] = $post;
     7374    }
     7375    wp_cache_add_multiple( $data, 'posts' );
    73747376}
    73757377
  • trunk/src/wp-includes/taxonomy.php

    r52669 r52707  
    35133513    $taxonomies = get_object_taxonomies( $object_type );
    35143514
    3515     foreach ( $object_ids as $id ) {
    3516         foreach ( $taxonomies as $taxonomy ) {
    3517             wp_cache_delete( $id, "{$taxonomy}_relationships" );
    3518         }
     3515    foreach ( $taxonomies as $taxonomy ) {
     3516        wp_cache_delete_multiple( $object_ids, "{$taxonomy}_relationships" );
    35193517    }
    35203518
     
    35683566            $taxonomies[] = $term->taxonomy;
    35693567            $ids[]        = $term->term_id;
    3570             wp_cache_delete( $term->term_id, 'terms' );
    3571         }
    3572 
     3568        }
     3569        wp_cache_delete_multiple( $ids, 'terms' );
    35733570        $taxonomies = array_unique( $taxonomies );
    35743571    } else {
     3572        wp_cache_delete_multiple( $ids, 'terms' );
    35753573        $taxonomies = array( $taxonomy );
    3576 
    3577         foreach ( $taxonomies as $taxonomy ) {
    3578             foreach ( $ids as $id ) {
    3579                 wp_cache_delete( $id, 'terms' );
    3580             }
    3581         }
    35823574    }
    35833575
     
    37533745    }
    37543746
     3747    $cache_values = array();
    37553748    foreach ( $object_terms as $id => $value ) {
    37563749        foreach ( $value as $taxonomy => $terms ) {
    3757             wp_cache_add( $id, $terms, "{$taxonomy}_relationships" );
    3758         }
     3750            $cache_values[ $taxonomy ][ $id ] = $terms;
     3751        }
     3752    }
     3753    foreach ( $cache_values as $taxonomy => $data ) {
     3754        wp_cache_add_multiple( $data, "{$taxonomy}_relationships" );
    37593755    }
    37603756}
     
    37693765 */
    37703766function update_term_cache( $terms, $taxonomy = '' ) {
     3767    $data = array();
    37713768    foreach ( (array) $terms as $term ) {
    37723769        // Create a copy in case the array was passed by reference.
     
    37763773        unset( $_term->object_id );
    37773774
    3778         wp_cache_add( $term->term_id, $_term, 'terms' );
    3779     }
     3775        $data[ $term->term_id ] = $_term;
     3776    }
     3777    wp_cache_add_multiple( $data, 'terms' );
    37803778}
    37813779
Note: See TracChangeset for help on using the changeset viewer.