WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/04/2013 01:54:15 PM (9 years ago)
Author:
ryan
Message:

In wp_get_archives(), cache queries to individual cache buckets instead of storing them in one cached array. Use incrementor style passive cache invalidation.

fixes #23206
see #23173

File:
1 edited

Legend:

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

    r23355 r23385  
    913913    }
    914914
    915     //filters
    916915    $where = apply_filters( 'getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
    917916    $join = apply_filters( 'getarchives_join', '', $r );
     
    919918    $output = '';
    920919
     920    $last_changed = wp_cache_get( 'last_changed', 'posts' );
     921    if ( ! $last_changed ) {
     922        $last_changed = 1;
     923        wp_cache_set( 'last_changed', $last_changed, 'posts' );
     924    }
     925
    921926    if ( 'monthly' == $type ) {
    922927        $query = "SELECT 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 $order $limit";
    923         $key = md5($query);
    924         $cache = wp_cache_get( 'wp_get_archives' , 'general');
    925         if ( !isset( $cache[ $key ] ) ) {
    926             $arcresults = $wpdb->get_results($query);
    927             $cache[ $key ] = $arcresults;
    928             wp_cache_set( 'wp_get_archives', $cache, 'general' );
    929         } else {
    930             $arcresults = $cache[ $key ];
     928        $key = md5( $query );
     929        $key = "wp_get_archives:$key:$last_changed";
     930        if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
     931            $results = $wpdb->get_results( $query );
     932            wp_cache_set( $key, $results, 'posts' );
    931933        }
    932         if ( $arcresults ) {
     934        if ( $results ) {
    933935            $afterafter = $after;
    934             foreach ( (array) $arcresults as $arcresult ) {
    935                 $url = get_month_link( $arcresult->year, $arcresult->month );
     936            foreach ( (array) $results as $result ) {
     937                $url = get_month_link( $result->year, $result->month );
    936938                /* translators: 1: month name, 2: 4-digit year */
    937                 $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
     939                $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($result->month), $result->year);
    938940                if ( $show_post_count )
    939                     $after = ' ('.$arcresult->posts.')' . $afterafter;
     941                    $after = ' ('.$result->posts.')' . $afterafter;
    940942                $output .= get_archives_link($url, $text, $format, $before, $after);
    941943            }
     
    943945    } elseif ('yearly' == $type) {
    944946        $query = "SELECT YEAR(post_date) AS `year`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date $order $limit";
    945         $key = md5($query);
    946         $cache = wp_cache_get( 'wp_get_archives' , 'general');
    947         if ( !isset( $cache[ $key ] ) ) {
    948             $arcresults = $wpdb->get_results($query);
    949             $cache[ $key ] = $arcresults;
    950             wp_cache_set( 'wp_get_archives', $cache, 'general' );
    951         } else {
    952             $arcresults = $cache[ $key ];
     947        $key = md5( $query );
     948        $key = "wp_get_archives:$key:$last_changed";
     949        if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
     950            $results = $wpdb->get_results( $query );
     951            wp_cache_set( $key, $results, 'posts' );
    953952        }
    954         if ($arcresults) {
     953        if ( $results ) {
    955954            $afterafter = $after;
    956             foreach ( (array) $arcresults as $arcresult) {
    957                 $url = get_year_link($arcresult->year);
    958                 $text = sprintf('%d', $arcresult->year);
     955            foreach ( (array) $results as $result) {
     956                $url = get_year_link($result->year);
     957                $text = sprintf('%d', $result->year);
    959958                if ($show_post_count)
    960                     $after = ' ('.$arcresult->posts.')' . $afterafter;
     959                    $after = ' ('.$result->posts.')' . $afterafter;
    961960                $output .= get_archives_link($url, $text, $format, $before, $after);
    962961            }
     
    964963    } elseif ( 'daily' == $type ) {
    965964        $query = "SELECT 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 $order $limit";
    966         $key = md5($query);
    967         $cache = wp_cache_get( 'wp_get_archives' , 'general');
    968         if ( !isset( $cache[ $key ] ) ) {
    969             $arcresults = $wpdb->get_results($query);
    970             $cache[ $key ] = $arcresults;
    971             wp_cache_set( 'wp_get_archives', $cache, 'general' );
    972         } else {
    973             $arcresults = $cache[ $key ];
     965        $key = md5( $query );
     966        $key = "wp_get_archives:$key:$last_changed";
     967        if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
     968            $results = $wpdb->get_results( $query );
     969            $cache[ $key ] = $results;
     970            wp_cache_set( $key, $results, 'posts' );
    974971        }
    975         if ( $arcresults ) {
     972        if ( $results ) {
    976973            $afterafter = $after;
    977             foreach ( (array) $arcresults as $arcresult ) {
    978                 $url    = get_day_link($arcresult->year, $arcresult->month, $arcresult->dayofmonth);
    979                 $date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $arcresult->year, $arcresult->month, $arcresult->dayofmonth);
     974            foreach ( (array) $results as $result ) {
     975                $url    = get_day_link($result->year, $result->month, $result->dayofmonth);
     976                $date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth);
    980977                $text = mysql2date($archive_day_date_format, $date);
    981978                if ($show_post_count)
    982                     $after = ' ('.$arcresult->posts.')'.$afterafter;
     979                    $after = ' ('.$result->posts.')'.$afterafter;
    983980                $output .= get_archives_link($url, $text, $format, $before, $after);
    984981            }
     
    987984        $week = _wp_mysql_week( '`post_date`' );
    988985        $query = "SELECT DISTINCT $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, YEAR( `post_date` ) ORDER BY `post_date` $order $limit";
    989         $key = md5($query);
    990         $cache = wp_cache_get( 'wp_get_archives' , 'general');
    991         if ( !isset( $cache[ $key ] ) ) {
    992             $arcresults = $wpdb->get_results($query);
    993             $cache[ $key ] = $arcresults;
    994             wp_cache_set( 'wp_get_archives', $cache, 'general' );
    995         } else {
    996             $arcresults = $cache[ $key ];
     986        $key = md5( $query );
     987        $key = "wp_get_archives:$key:$last_changed";
     988        if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
     989            $results = $wpdb->get_results( $query );
     990            wp_cache_set( $key, $results, 'posts' );
    997991        }
    998992        $arc_w_last = '';
    999993        $afterafter = $after;
    1000         if ( $arcresults ) {
    1001                 foreach ( (array) $arcresults as $arcresult ) {
    1002                     if ( $arcresult->week != $arc_w_last ) {
    1003                         $arc_year = $arcresult->yr;
    1004                         $arc_w_last = $arcresult->week;
    1005                         $arc_week = get_weekstartend($arcresult->yyyymmdd, get_option('start_of_week'));
     994        if ( $results ) {
     995                foreach ( (array) $results as $result ) {
     996                    if ( $result->week != $arc_w_last ) {
     997                        $arc_year = $result->yr;
     998                        $arc_w_last = $result->week;
     999                        $arc_week = get_weekstartend($result->yyyymmdd, get_option('start_of_week'));
    10061000                        $arc_week_start = date_i18n($archive_week_start_date_format, $arc_week['start']);
    10071001                        $arc_week_end = date_i18n($archive_week_end_date_format, $arc_week['end']);
    1008                         $url  = sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&', '=', $arcresult->week);
     1002                        $url  = sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&', '=', $result->week);
    10091003                        $text = $arc_week_start . $archive_week_separator . $arc_week_end;
    10101004                        if ($show_post_count)
    1011                             $after = ' ('.$arcresult->posts.')'.$afterafter;
     1005                            $after = ' ('.$result->posts.')'.$afterafter;
    10121006                        $output .= get_archives_link($url, $text, $format, $before, $after);
    10131007                    }
     
    10171011        $orderby = ('alpha' == $type) ? 'post_title ASC ' : 'post_date DESC ';
    10181012        $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
    1019         $key = md5($query);
    1020         $cache = wp_cache_get( 'wp_get_archives' , 'general');
    1021         if ( !isset( $cache[ $key ] ) ) {
    1022             $arcresults = $wpdb->get_results($query);
    1023             $cache[ $key ] = $arcresults;
    1024             wp_cache_set( 'wp_get_archives', $cache, 'general' );
    1025         } else {
    1026             $arcresults = $cache[ $key ];
     1013        $key = md5( $query );
     1014        $key = "wp_get_archives:$key:$last_changed";
     1015        if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
     1016            $results = $wpdb->get_results( $query );
     1017            wp_cache_set( $key, $results, 'posts' );
    10271018        }
    1028         if ( $arcresults ) {
    1029             foreach ( (array) $arcresults as $arcresult ) {
    1030                 if ( $arcresult->post_date != '0000-00-00 00:00:00' ) {
    1031                     $url  = get_permalink( $arcresult );
    1032                     if ( $arcresult->post_title )
    1033                         $text = strip_tags( apply_filters( 'the_title', $arcresult->post_title, $arcresult->ID ) );
     1019        if ( $results ) {
     1020            foreach ( (array) $results as $result ) {
     1021                if ( $result->post_date != '0000-00-00 00:00:00' ) {
     1022                    $url  = get_permalink( $result );
     1023                    if ( $result->post_title )
     1024                        $text = strip_tags( apply_filters( 'the_title', $result->post_title, $result->ID ) );
    10341025                    else
    1035                         $text = $arcresult->ID;
     1026                        $text = $result->ID;
    10361027                    $output .= get_archives_link($url, $text, $format, $before, $after);
    10371028                }
Note: See TracChangeset for help on using the changeset viewer.