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

git-svn-id: http://svn.automattic.com/wordpress/trunk@15875 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2010-10-20 20:22:14 +00:00
parent 0319925e7f
commit 669a403b55
2 changed files with 34 additions and 32 deletions

View File

@ -39,6 +39,8 @@ add_action( 'delete_post', 'wpmu_update_blogs_date' );
add_action( 'private_to_published', 'wpmu_update_blogs_date' );
add_action( 'publish_phone', 'wpmu_update_blogs_date' );
add_action( 'publish_post', 'wpmu_update_blogs_date' );
add_action( 'admin_init', 'wp_schedule_update_network_counts');
add_action( 'update_network_counts', 'wp_update_network_counts');
// Files
add_filter( 'wp_upload_bits', 'upload_is_file_too_big' );
@ -57,7 +59,6 @@ if ( ! defined('EDIT_ANY_USER') || ! EDIT_ANY_USER ) // back compat constant.
add_filter( 'enable_edit_any_user_configuration', '__return_false' );
add_filter( 'force_filtered_html_on_import', '__return_true' );
// WP_HOME and WP_SITEURL should not have any effect in MS
remove_filter( 'option_siteurl', '_config_wp_siteurl' );
remove_filter( 'option_home', '_config_wp_home' );

View File

@ -148,8 +148,7 @@ function is_user_member_of_blog( $user_id, $blog_id = 0 ) {
/**
* The number of active users in your installation.
*
* This function also saves the count as a site option,
* which speeds up future lookups.
* The count is cached and updated twice daily. This is not a live count.
*
* @since MU 2.7
* @uses update_site_option()
@ -157,25 +156,13 @@ function is_user_member_of_blog( $user_id, $blog_id = 0 ) {
* @return int
*/
function get_user_count() {
global $wpdb;
$count_ts = get_site_option( 'user_count_ts' );
if ( time() - $count_ts > 3600 ) {
$count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'") );
update_site_option( 'user_count', $count );
update_site_option( 'user_count_ts', time() );
}
$count = get_site_option( 'user_count' );
return $count;
return get_site_option( 'user_count' );
}
/**
* The number of active sites on your installation.
*
* This function also saves the count as a site option,
* which speeds up future lookups.
* The count is cached and updated twice daily. This is not a live count.
*
* @since MU 1.0
* @uses update_site_option()
@ -184,21 +171,7 @@ function get_user_count() {
* @return int
*/
function get_blog_count( $id = 0 ) {
global $wpdb;
if ( $id == 0 )
$id = $wpdb->siteid;
$count_ts = get_site_option( 'blog_count_ts' );
if ( time() - $count_ts > 3600 ) {
$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) );
update_site_option( 'blog_count', $count );
update_site_option( 'blog_count_ts', time() );
}
$count = get_site_option( 'blog_count' );
return $count;
return get_site_option( 'blog_count' );
}
/**
@ -1533,4 +1506,32 @@ function filter_SSL( $url ) {
return $url;
}
/**
* Schedule update of the network-wide counts for the current network.
*
* @since 3.1.0
*/
function wp_schedule_update_network_counts() {
if ( !is_main_site() )
return;
if ( !wp_next_scheduled('update_network_counts') && !defined('WP_INSTALLING') )
wp_schedule_event(time(), 'twicedaily', 'update_network_counts');
}
/**
* Update the network-wide counts for the current network.
*
* @since 3.1.0
*/
function wp_update_network_counts() {
global $wpdb;
$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) );
update_site_option( 'blog_count', $count );
$count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'") );
update_site_option( 'user_count', $count );
}
?>