Ticket #5275: cache_archives.diff

File cache_archives.diff, 5.1 KB (added by ryan, 5 years ago)
  • wp-includes/post.php

     
    16321632 
    16331633        clean_object_term_cache($id, 'post'); 
    16341634 
     1635        wp_cache_delete( 'wp_get_archives', 'general' ); 
     1636 
    16351637        do_action('clean_post_cache', $id); 
    16361638} 
    16371639 
  • wp-includes/general-template.php

     
    394394        $join = apply_filters('getarchives_join', "", $r); 
    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; 
    400409                        foreach ( $arcresults as $arcresult ) { 
     
    406415                        } 
    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; 
    412430                        foreach ($arcresults as $arcresult) { 
     
    418436                        } 
    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; 
    424451                        foreach ( $arcresults as $arcresult ) { 
     
    432459                } 
    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; 
    438474                if ( $arcresults ) { 
     
    453489                } 
    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 ) { 
    459504                                if ( $arcresult->post_date != '0000-00-00 00:00:00' ) {