Make WordPress Core

Changeset 9511


Ignore:
Timestamp:
11/04/2008 04:44:55 PM (18 years ago)
Author:
ryan
Message:

Add caching to get_comments(). see #8057

File:
1 edited

Legend:

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

    r9490 r9511  
    183183        $defaults = array('status' => '', 'orderby' => 'comment_date_gmt', 'order' => 'DESC', 'number' => '', 'offset' => '', 'post_id' => 0);
    184184
    185         $r = wp_parse_args( $args, $defaults );
    186         extract( $r, EXTR_SKIP );
     185        $args = wp_parse_args( $args, $defaults );
     186        extract( $args, EXTR_SKIP );
     187
     188        // $args can be whatever, only use the args defined in defaults to compute the key
     189        $key = md5( serialize( compact(array_keys($defaults)) )  );
     190        $last_changed = wp_cache_get('last_changed', 'comment');
     191        if ( !$last_changed ) {
     192                $last_changed = time();
     193                wp_cache_set('last_changed', $last_changed, 'comment');
     194        }
     195        $cache_key = "get_comments:$key:$last_changed";
     196
     197        if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
     198                return $cache;
     199        }
    187200
    188201        $post_id = absint($post_id);
     
    219232                $post_where = '';
    220233
    221         return $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE $post_where $approved ORDER BY $orderby $order $number" );
     234        $comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE $post_where $approved ORDER BY $orderby $order $number" );
     235        wp_cache_add( $cache_key, $comments, 'comment' );
     236
     237        return $comments;
    222238}
    223239
Note: See TracChangeset for help on using the changeset viewer.