Comment caching. Reduce queries on edit-comments.php page. Add non-persistent cache groups. Hat tip to hovenko. fixes #4387

git-svn-id: http://svn.automattic.com/wordpress/trunk@5666 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-06-08 00:20:22 +00:00
parent fbfafcf27a
commit f2f6101e1c
5 changed files with 47 additions and 15 deletions

View File

@ -151,7 +151,8 @@ if ( $extra_comments ) : ?>
</tr>
</thead>';
foreach ($comments as $comment) {
$authordata = get_userdata($wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = $comment->comment_post_ID"));
$post = get_post($comment->comment_post_ID);
$authordata = get_userdata($post->post_author);
$comment_status = wp_get_comment_status($comment->comment_ID);
$class = ('alternate' == $class) ? '' : 'alternate';
$class .= ('unapproved' == $comment_status) ? ' unapproved' : '';

View File

@ -250,6 +250,8 @@ function _wp_get_comment_list( $s = false, $start, $num ) {
$comments = $wpdb->get_results( "SELECT SQL_CALC_FOUND_ROWS * FROM $wpdb->comments WHERE comment_approved = '0' OR comment_approved = '1' ORDER BY comment_date DESC LIMIT $start, $num" );
}
update_comment_cache($comments);
$total = $wpdb->get_var( "SELECT FOUND_ROWS()" );
return array($comments, $total);
@ -260,7 +262,8 @@ function _wp_comment_list_item( $id, $alt = 0 ) {
$id = (int) $id;
$comment =& get_comment( $id );
$class = '';
$authordata = get_userdata($wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = $comment->comment_post_ID"));
$post = get_post($comment->comment_post_ID);
$authordata = get_userdata($post->post_author);
$comment_status = wp_get_comment_status($comment->comment_ID);
if ( 'unapproved' == $comment_status )
$class .= ' unapproved';

View File

@ -63,6 +63,7 @@ class WP_Object_Cache {
var $dirty_objects = array ();
var $non_existant_objects = array ();
var $global_groups = array ('users', 'userlogins', 'usermeta');
var $non_persistent_groups = array('comment');
var $blog_id;
var $cold_cache_hits = 0;
var $warm_cache_hits = 0;
@ -308,6 +309,9 @@ class WP_Object_Cache {
// Loop over dirty objects and save them.
$errors = 0;
foreach ($this->dirty_objects as $group => $ids) {
if ( in_array($group, $this->non_persistent_groups) )
continue;
$group_dir = $this->make_group_dir($group, $dir_perms);
$ids = array_unique($ids);

View File

@ -301,6 +301,7 @@ function comments_template( $file = '/comments.php' ) {
// keep $comments for legacy's sake (remember $table*? ;) )
$comments = $wp_query->comments = apply_filters( 'comments_array', $comments, $post->ID );
$wp_query->comment_count = count($wp_query->comments);
update_comment_cache($comments);
define('COMMENTS_TEMPLATE', true);
$include = apply_filters('comments_template', TEMPLATEPATH . $file );

View File

@ -71,7 +71,7 @@ function get_approved_comments($post_id) {
// Retrieves comment data given a comment ID or comment object.
// Handles comment caching.
function &get_comment(&$comment, $output = OBJECT) {
global $comment_cache, $wpdb;
global $wpdb;
if ( empty($comment) ) {
if ( isset($GLOBALS['comment']) )
@ -79,21 +79,20 @@ function &get_comment(&$comment, $output = OBJECT) {
else
$_comment = null;
} elseif ( is_object($comment) ) {
if ( !isset($comment_cache[$comment->comment_ID]) )
$comment_cache[$comment->comment_ID] = &$comment;
$_comment = & $comment_cache[$comment->comment_ID];
wp_cache_add($comment->comment_ID, $comment, 'comment');
$_comment = $comment;
} else {
$comment = (int) $comment;
if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) {
$_comment = & $GLOBALS['comment'];
} elseif ( !isset($comment_cache[$comment]) ) {
} elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) {
$_comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment' LIMIT 1");
$comment_cache[$comment->comment_ID] = & $_comment;
} else {
$_comment = & $comment_cache[$comment];
wp_cache_add($_comment->comment_ID, $_comment, 'comment');
}
}
$_comment = apply_filters('get_comment', $_comment);
if ( $output == OBJECT ) {
return $_comment;
} elseif ( $output == ARRAY_A ) {
@ -285,6 +284,8 @@ function wp_delete_comment($comment_id) {
if ( $post_id && $comment->comment_approved == 1 )
wp_update_comment_count($post_id);
clean_comment_cache($comment_id);
do_action('wp_set_comment_status', $comment_id, 'delete');
return true;
}
@ -293,15 +294,19 @@ function wp_delete_comment($comment_id) {
function wp_get_comment_status($comment_id) {
global $wpdb;
$result = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
$comment = get_comment($comment_id);
if ( !$comment )
return false;
if ( $result == NULL )
$approved = $comment->comment_approved;
if ( $approved == NULL )
return 'deleted';
elseif ( $result == '1' )
elseif ( $approved == '1' )
return 'approved';
elseif ( $result == '0' )
elseif ( $approved == '0' )
return 'unapproved';
elseif ( $result == 'spam' )
elseif ( $approved == 'spam' )
return 'spam';
else
return false;
@ -438,9 +443,12 @@ function wp_set_comment_status($comment_id, $comment_status) {
if ( !$wpdb->query($query) )
return false;
clean_comment_cache($comment_id);
do_action('wp_set_comment_status', $comment_id, $comment_status);
$comment = get_comment($comment_id);
wp_update_comment_count($comment->comment_post_ID);
return true;
}
@ -479,6 +487,8 @@ function wp_update_comment($commentarr) {
WHERE comment_ID = $comment_ID" );
$rval = $wpdb->rows_affected;
clean_comment_cache($comment_ID);
wp_update_comment_count($comment_post_ID);
do_action('edit_comment', $comment_ID);
return $rval;
@ -793,4 +803,17 @@ function weblog_ping($server = '', $path = '') {
$client->query('weblogUpdates.ping', get_option('blogname'), $home);
}
//
// Cache
//
function clean_comment_cache($id) {
wp_cache_delete($id, 'comment');
}
function update_comment_cache($comments) {
foreach ( $comments as $comment )
wp_cache_add($comment->comment_ID, $comment, 'comment');
}
?>