Add caching to get_comments(). see #8057

git-svn-id: http://svn.automattic.com/wordpress/trunk@9511 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2008-11-04 16:44:55 +00:00
parent 0bfca053e9
commit feab9ff5f1
1 changed files with 19 additions and 3 deletions

View File

@ -182,8 +182,21 @@ function get_comments( $args = '' ) {
$defaults = array('status' => '', 'orderby' => 'comment_date_gmt', 'order' => 'DESC', 'number' => '', 'offset' => '', 'post_id' => 0);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
// $args can be whatever, only use the args defined in defaults to compute the key
$key = md5( serialize( compact(array_keys($defaults)) ) );
$last_changed = wp_cache_get('last_changed', 'comment');
if ( !$last_changed ) {
$last_changed = time();
wp_cache_set('last_changed', $last_changed, 'comment');
}
$cache_key = "get_comments:$key:$last_changed";
if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
return $cache;
}
$post_id = absint($post_id);
@ -218,7 +231,10 @@ function get_comments( $args = '' ) {
else
$post_where = '';
return $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE $post_where $approved ORDER BY $orderby $order $number" );
$comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE $post_where $approved ORDER BY $orderby $order $number" );
wp_cache_add( $cache_key, $comments, 'comment' );
return $comments;
}
/**