Make WordPress Core

Changeset 15875


Ignore:
Timestamp:
10/20/2010 08:22:14 PM (14 years ago)
Author:
ryan
Message:

Update network-wide active user and blog counts via a cron job to avoid costly count queries. see #15170

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/ms-default-filters.php

    r15678 r15875  
    4040add_action( 'publish_phone', 'wpmu_update_blogs_date' );
    4141add_action( 'publish_post', 'wpmu_update_blogs_date' );
     42add_action( 'admin_init', 'wp_schedule_update_network_counts');
     43add_action( 'update_network_counts', 'wp_update_network_counts');
    4244
    4345// Files
     
    5860add_filter( 'force_filtered_html_on_import', '__return_true' );
    5961
    60 
    6162// WP_HOME and WP_SITEURL should not have any effect in MS
    6263remove_filter( 'option_siteurl', '_config_wp_siteurl' );
  • trunk/wp-includes/ms-functions.php

    r15843 r15875  
    149149 * The number of active users in your installation.
    150150 *
    151  * This function also saves the count as a site option,
    152  * which speeds up future lookups.
     151 * The count is cached and updated twice daily. This is not a live count.
    153152 *
    154153 * @since MU 2.7
     
    158157 */
    159158function get_user_count() {
    160     global $wpdb;
    161 
    162     $count_ts = get_site_option( 'user_count_ts' );
    163     if ( time() - $count_ts > 3600 ) {
    164         $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'") );
    165         update_site_option( 'user_count', $count );
    166         update_site_option( 'user_count_ts', time() );
    167     }
    168 
    169     $count = get_site_option( 'user_count' );
    170 
    171     return $count;
     159    return get_site_option( 'user_count' );
    172160}
    173161
     
    175163 * The number of active sites on your installation.
    176164 *
    177  * This function also saves the count as a site option,
    178  * which speeds up future lookups.
     165 * The count is cached and updated twice daily. This is not a live count.
    179166 *
    180167 * @since MU 1.0
     
    185172 */
    186173function get_blog_count( $id = 0 ) {
    187     global $wpdb;
    188 
    189     if ( $id == 0 )
    190         $id = $wpdb->siteid;
    191 
    192     $count_ts = get_site_option( 'blog_count_ts' );
    193     if ( time() - $count_ts > 3600 ) {
    194         $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(blog_id) as c FROM $wpdb->blogs WHERE site_id = %d AND spam = '0' AND deleted = '0' and archived = '0'", $id) );
    195         update_site_option( 'blog_count', $count );
    196         update_site_option( 'blog_count_ts', time() );
    197     }
    198 
    199     $count = get_site_option( 'blog_count' );
    200 
    201     return $count;
     174    return get_site_option( 'blog_count' );
    202175}
    203176
     
    15341507}
    15351508
     1509/**
     1510 * Schedule update of the network-wide counts for the current network.
     1511 *
     1512 * @since 3.1.0
     1513 */
     1514function wp_schedule_update_network_counts() {
     1515    if ( !is_main_site() )
     1516        return;
     1517
     1518    if ( !wp_next_scheduled('update_network_counts') && !defined('WP_INSTALLING') )
     1519        wp_schedule_event(time(), 'twicedaily', 'update_network_counts');
     1520}
     1521
     1522/**
     1523 *  Update the network-wide counts for the current network.
     1524 *
     1525 *  @since 3.1.0
     1526 */
     1527function wp_update_network_counts() {
     1528    global $wpdb;
     1529
     1530    $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(blog_id) as c FROM $wpdb->blogs WHERE site_id = %d AND spam = '0' AND deleted = '0' and archived = '0'", $wpdb->siteid) );
     1531    update_site_option( 'blog_count', $count );
     1532
     1533    $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'") );
     1534    update_site_option( 'user_count', $count );
     1535}
     1536
    15361537?>
Note: See TracChangeset for help on using the changeset viewer.