WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/30/2007 06:03:11 AM (12 years ago)
Author:
ryan
Message:

wp_get_archives caching. fixes #5275

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/general-template.php

    r6195 r6296  
    395395
    396396    if ( 'monthly' == $type ) {
    397         $arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC" . $limit);
     397        $query = "SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
     398        $key = md5($query);
     399        $cache = wp_cache_get( 'wp_get_archives' , 'general');
     400        if ( !isset( $cache[ $key ] ) ) {
     401            $arcresults = $wpdb->get_results($query);
     402            $cache[ $key ] = $arcresults;
     403            wp_cache_add( 'wp_get_archives', $cache, 'general' );
     404        } else {
     405            $arcresults = $cache[ $key ];
     406        }
    398407        if ( $arcresults ) {
    399408            $afterafter = $after;
     
    407416        }
    408417    } elseif ('yearly' == $type) {
    409          $arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS `year`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date DESC" . $limit);
     418        $query = "SELECT DISTINCT YEAR(post_date) AS `year`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date DESC $limit";
     419        $key = md5($query);
     420        $cache = wp_cache_get( 'wp_get_archives' , 'general');
     421        if ( !isset( $cache[ $key ] ) ) {
     422            $arcresults = $wpdb->get_results($query);
     423            $cache[ $key ] = $arcresults;
     424            wp_cache_add( 'wp_get_archives', $cache, 'general' );
     425        } else {
     426            $arcresults = $cache[ $key ];
     427        }
    410428        if ($arcresults) {
    411429            $afterafter = $after;
     
    419437        }
    420438    } elseif ( 'daily' == $type ) {
    421         $arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date DESC" . $limit);
     439        $query = "SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date DESC $limit";
     440        $key = md5($query);
     441        $cache = wp_cache_get( 'wp_get_archives' , 'general');
     442        if ( !isset( $cache[ $key ] ) ) {
     443            $arcresults = $wpdb->get_results($query);
     444            $cache[ $key ] = $arcresults;
     445            wp_cache_add( 'wp_get_archives', $cache, 'general' );
     446        } else {
     447            $arcresults = $cache[ $key ];
     448        }
    422449        if ( $arcresults ) {
    423450            $afterafter = $after;
     
    433460    } elseif ( 'weekly' == $type ) {
    434461        $start_of_week = get_option('start_of_week');
    435         $arcresults = $wpdb->get_results("SELECT DISTINCT WEEK(post_date, $start_of_week) AS `week`, YEAR(post_date) AS yr, DATE_FORMAT(post_date, '%Y-%m-%d') AS yyyymmdd, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY WEEK(post_date, $start_of_week), YEAR(post_date) ORDER BY post_date DESC" . $limit);
     462        $query = "SELECT DISTINCT WEEK(post_date, $start_of_week) AS `week`, YEAR(post_date) AS yr, DATE_FORMAT(post_date, '%Y-%m-%d') AS yyyymmdd, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY WEEK(post_date, $start_of_week), YEAR(post_date) ORDER BY post_date DESC $limit";
     463        $key = md5($query);
     464        $cache = wp_cache_get( 'wp_get_archives' , 'general');
     465        if ( !isset( $cache[ $key ] ) ) {
     466            $arcresults = $wpdb->get_results($query);
     467            $cache[ $key ] = $arcresults;
     468            wp_cache_add( 'wp_get_archives', $cache, 'general' );
     469        } else {
     470            $arcresults = $cache[ $key ];
     471        }
    436472        $arc_w_last = '';
    437473        $afterafter = $after;
     
    454490    } elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) {
    455491        ('alpha' == $type) ? $orderby = "post_title ASC " : $orderby = "post_date DESC ";
    456         $arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit");
     492        $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
     493        $key = md5($query);
     494        $cache = wp_cache_get( 'wp_get_archives' , 'general');
     495        if ( !isset( $cache[ $key ] ) ) {
     496            $arcresults = $wpdb->get_results($query);
     497            $cache[ $key ] = $arcresults;
     498            wp_cache_add( 'wp_get_archives', $cache, 'general' );
     499        } else {
     500            $arcresults = $cache[ $key ];
     501        }
    457502        if ( $arcresults ) {
    458503            foreach ( $arcresults as $arcresult ) {
Note: See TracChangeset for help on using the changeset viewer.