Make WordPress Core


Ignore:
Timestamp:
06/08/2007 12:20:22 AM (18 years ago)
Author:
ryan
Message:

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

File:
1 edited

Legend:

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

    r5329 r5666  
    7272// Handles comment caching.
    7373function &get_comment(&$comment, $output = OBJECT) {
    74     global $comment_cache, $wpdb;
     74    global $wpdb;
    7575
    7676    if ( empty($comment) ) {
     
    8080            $_comment = null;
    8181    } elseif ( is_object($comment) ) {
    82         if ( !isset($comment_cache[$comment->comment_ID]) )
    83             $comment_cache[$comment->comment_ID] = &$comment;
    84         $_comment = & $comment_cache[$comment->comment_ID];
     82        wp_cache_add($comment->comment_ID, $comment, 'comment');
     83        $_comment = $comment;
    8584    } else {
    8685        $comment = (int) $comment;
    8786        if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) {
    8887            $_comment = & $GLOBALS['comment'];
    89         } elseif ( !isset($comment_cache[$comment]) ) {
     88        } elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) {
    9089            $_comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment' LIMIT 1");
    91             $comment_cache[$comment->comment_ID] = & $_comment;
    92         } else {
    93             $_comment = & $comment_cache[$comment];
    94         }
    95     }
     90            wp_cache_add($_comment->comment_ID, $_comment, 'comment');
     91        }
     92    }
     93
     94    $_comment = apply_filters('get_comment', $_comment);
    9695
    9796    if ( $output == OBJECT ) {
     
    286285        wp_update_comment_count($post_id);
    287286
     287    clean_comment_cache($comment_id);
     288
    288289    do_action('wp_set_comment_status', $comment_id, 'delete');
    289290    return true;
     
    294295    global $wpdb;
    295296
    296     $result = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
    297 
    298     if ( $result == NULL )
     297    $comment = get_comment($comment_id);
     298    if ( !$comment )
     299        return false;
     300
     301    $approved = $comment->comment_approved;
     302
     303    if ( $approved == NULL )
    299304        return 'deleted';
    300     elseif ( $result == '1' )
     305    elseif ( $approved == '1' )
    301306        return 'approved';
    302     elseif ( $result == '0' )
     307    elseif ( $approved == '0' )
    303308        return 'unapproved';
    304     elseif ( $result == 'spam' )
     309    elseif ( $approved == 'spam' )
    305310        return 'spam';
    306311    else
     
    439444        return false;
    440445
     446    clean_comment_cache($comment_id);
     447
    441448    do_action('wp_set_comment_status', $comment_id, $comment_status);
    442449    $comment = get_comment($comment_id);
    443450    wp_update_comment_count($comment->comment_post_ID);
     451
    444452    return true;
    445453}
     
    480488
    481489    $rval = $wpdb->rows_affected;
     490
     491    clean_comment_cache($comment_ID);
    482492    wp_update_comment_count($comment_post_ID);
    483493    do_action('edit_comment', $comment_ID);
     
    794804}
    795805
     806//
     807// Cache
     808//
     809
     810function clean_comment_cache($id) {
     811    wp_cache_delete($id, 'comment');
     812}
     813
     814function update_comment_cache($comments) {
     815    foreach ( $comments as $comment )
     816        wp_cache_add($comment->comment_ID, $comment, 'comment');
     817}
     818
    796819?>
Note: See TracChangeset for help on using the changeset viewer.