<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[23385] trunk/wp-includes/general-template.php: In wp_get_archives(), cache queries to individual cache buckets instead of storing them in one cached array.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/23385">23385</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2013-02-04 13:54:15 +0000 (Mon, 04 Feb 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>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 <a href="http://core.trac.wordpress.org/ticket/23206">#23206</a>
see <a href="http://core.trac.wordpress.org/ticket/23173">#23173</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesgeneraltemplatephp">trunk/wp-includes/general-template.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesgeneraltemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/general-template.php (23384 => 23385)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/general-template.php        2013-02-04 13:48:46 UTC (rev 23384)
+++ trunk/wp-includes/general-template.php        2013-02-04 13:54:15 UTC (rev 23385)
</span><span class="lines">@@ -912,103 +912,97 @@
</span><span class="cx">                 $archive_week_end_date_format = get_option('date_format');
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        //filters
</del><span class="cx">         $where = apply_filters( 'getarchives_where', &quot;WHERE post_type = 'post' AND post_status = 'publish'&quot;, $r );
</span><span class="cx">         $join = apply_filters( 'getarchives_join', '', $r );
</span><span class="cx"> 
</span><span class="cx">         $output = '';
</span><span class="cx"> 
</span><ins>+        $last_changed = wp_cache_get( 'last_changed', 'posts' );
+        if ( ! $last_changed ) {
+                $last_changed = 1;
+                wp_cache_set( 'last_changed', $last_changed, 'posts' );
+        }
+
</ins><span class="cx">         if ( 'monthly' == $type ) {
</span><span class="cx">                 $query = &quot;SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb-&gt;posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit&quot;;
</span><del>-                $key = md5($query);
-                $cache = wp_cache_get( 'wp_get_archives' , 'general');
-                if ( !isset( $cache[ $key ] ) ) {
-                        $arcresults = $wpdb-&gt;get_results($query);
-                        $cache[ $key ] = $arcresults;
-                        wp_cache_set( 'wp_get_archives', $cache, 'general' );
-                } else {
-                        $arcresults = $cache[ $key ];
</del><ins>+                $key = md5( $query );
+                $key = &quot;wp_get_archives:$key:$last_changed&quot;;
+                if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
+                        $results = $wpdb-&gt;get_results( $query );
+                        wp_cache_set( $key, $results, 'posts' );
</ins><span class="cx">                 }
</span><del>-                if ( $arcresults ) {
</del><ins>+                if ( $results ) {
</ins><span class="cx">                         $afterafter = $after;
</span><del>-                        foreach ( (array) $arcresults as $arcresult ) {
-                                $url = get_month_link( $arcresult-&gt;year, $arcresult-&gt;month );
</del><ins>+                        foreach ( (array) $results as $result ) {
+                                $url = get_month_link( $result-&gt;year, $result-&gt;month );
</ins><span class="cx">                                 /* translators: 1: month name, 2: 4-digit year */
</span><del>-                                $text = sprintf(__('%1$s %2$d'), $wp_locale-&gt;get_month($arcresult-&gt;month), $arcresult-&gt;year);
</del><ins>+                                $text = sprintf(__('%1$s %2$d'), $wp_locale-&gt;get_month($result-&gt;month), $result-&gt;year);
</ins><span class="cx">                                 if ( $show_post_count )
</span><del>-                                        $after = '&amp;nbsp;('.$arcresult-&gt;posts.')' . $afterafter;
</del><ins>+                                        $after = '&amp;nbsp;('.$result-&gt;posts.')' . $afterafter;
</ins><span class="cx">                                 $output .= get_archives_link($url, $text, $format, $before, $after);
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">         } elseif ('yearly' == $type) {
</span><span class="cx">                 $query = &quot;SELECT YEAR(post_date) AS `year`, count(ID) as posts FROM $wpdb-&gt;posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date $order $limit&quot;;
</span><del>-                $key = md5($query);
-                $cache = wp_cache_get( 'wp_get_archives' , 'general');
-                if ( !isset( $cache[ $key ] ) ) {
-                        $arcresults = $wpdb-&gt;get_results($query);
-                        $cache[ $key ] = $arcresults;
-                        wp_cache_set( 'wp_get_archives', $cache, 'general' );
-                } else {
-                        $arcresults = $cache[ $key ];
</del><ins>+                $key = md5( $query );
+                $key = &quot;wp_get_archives:$key:$last_changed&quot;;
+                if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
+                        $results = $wpdb-&gt;get_results( $query );
+                        wp_cache_set( $key, $results, 'posts' );
</ins><span class="cx">                 }
</span><del>-                if ($arcresults) {
</del><ins>+                if ( $results ) {
</ins><span class="cx">                         $afterafter = $after;
</span><del>-                        foreach ( (array) $arcresults as $arcresult) {
-                                $url = get_year_link($arcresult-&gt;year);
-                                $text = sprintf('%d', $arcresult-&gt;year);
</del><ins>+                        foreach ( (array) $results as $result) {
+                                $url = get_year_link($result-&gt;year);
+                                $text = sprintf('%d', $result-&gt;year);
</ins><span class="cx">                                 if ($show_post_count)
</span><del>-                                        $after = '&amp;nbsp;('.$arcresult-&gt;posts.')' . $afterafter;
</del><ins>+                                        $after = '&amp;nbsp;('.$result-&gt;posts.')' . $afterafter;
</ins><span class="cx">                                 $output .= get_archives_link($url, $text, $format, $before, $after);
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">         } elseif ( 'daily' == $type ) {
</span><span class="cx">                 $query = &quot;SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb-&gt;posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date $order $limit&quot;;
</span><del>-                $key = md5($query);
-                $cache = wp_cache_get( 'wp_get_archives' , 'general');
-                if ( !isset( $cache[ $key ] ) ) {
-                        $arcresults = $wpdb-&gt;get_results($query);
-                        $cache[ $key ] = $arcresults;
-                        wp_cache_set( 'wp_get_archives', $cache, 'general' );
-                } else {
-                        $arcresults = $cache[ $key ];
</del><ins>+                $key = md5( $query );
+                $key = &quot;wp_get_archives:$key:$last_changed&quot;;
+                if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
+                        $results = $wpdb-&gt;get_results( $query );
+                        $cache[ $key ] = $results;
+                        wp_cache_set( $key, $results, 'posts' );
</ins><span class="cx">                 }
</span><del>-                if ( $arcresults ) {
</del><ins>+                if ( $results ) {
</ins><span class="cx">                         $afterafter = $after;
</span><del>-                        foreach ( (array) $arcresults as $arcresult ) {
-                                $url        = get_day_link($arcresult-&gt;year, $arcresult-&gt;month, $arcresult-&gt;dayofmonth);
-                                $date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $arcresult-&gt;year, $arcresult-&gt;month, $arcresult-&gt;dayofmonth);
</del><ins>+                        foreach ( (array) $results as $result ) {
+                                $url        = get_day_link($result-&gt;year, $result-&gt;month, $result-&gt;dayofmonth);
+                                $date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $result-&gt;year, $result-&gt;month, $result-&gt;dayofmonth);
</ins><span class="cx">                                 $text = mysql2date($archive_day_date_format, $date);
</span><span class="cx">                                 if ($show_post_count)
</span><del>-                                        $after = '&amp;nbsp;('.$arcresult-&gt;posts.')'.$afterafter;
</del><ins>+                                        $after = '&amp;nbsp;('.$result-&gt;posts.')'.$afterafter;
</ins><span class="cx">                                 $output .= get_archives_link($url, $text, $format, $before, $after);
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">         } elseif ( 'weekly' == $type ) {
</span><span class="cx">                 $week = _wp_mysql_week( '`post_date`' );
</span><span class="cx">                 $query = &quot;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-&gt;posts` $join $where GROUP BY $week, YEAR( `post_date` ) ORDER BY `post_date` $order $limit&quot;;
</span><del>-                $key = md5($query);
-                $cache = wp_cache_get( 'wp_get_archives' , 'general');
-                if ( !isset( $cache[ $key ] ) ) {
-                        $arcresults = $wpdb-&gt;get_results($query);
-                        $cache[ $key ] = $arcresults;
-                        wp_cache_set( 'wp_get_archives', $cache, 'general' );
-                } else {
-                        $arcresults = $cache[ $key ];
</del><ins>+                $key = md5( $query );
+                $key = &quot;wp_get_archives:$key:$last_changed&quot;;
+                if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
+                        $results = $wpdb-&gt;get_results( $query );
+                        wp_cache_set( $key, $results, 'posts' );
</ins><span class="cx">                 }
</span><span class="cx">                 $arc_w_last = '';
</span><span class="cx">                 $afterafter = $after;
</span><del>-                if ( $arcresults ) {
-                                foreach ( (array) $arcresults as $arcresult ) {
-                                        if ( $arcresult-&gt;week != $arc_w_last ) {
-                                                $arc_year = $arcresult-&gt;yr;
-                                                $arc_w_last = $arcresult-&gt;week;
-                                                $arc_week = get_weekstartend($arcresult-&gt;yyyymmdd, get_option('start_of_week'));
</del><ins>+                if ( $results ) {
+                                foreach ( (array) $results as $result ) {
+                                        if ( $result-&gt;week != $arc_w_last ) {
+                                                $arc_year = $result-&gt;yr;
+                                                $arc_w_last = $result-&gt;week;
+                                                $arc_week = get_weekstartend($result-&gt;yyyymmdd, get_option('start_of_week'));
</ins><span class="cx">                                                 $arc_week_start = date_i18n($archive_week_start_date_format, $arc_week['start']);
</span><span class="cx">                                                 $arc_week_end = date_i18n($archive_week_end_date_format, $arc_week['end']);
</span><del>-                                                $url  = sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&amp;amp;', '=', $arcresult-&gt;week);
</del><ins>+                                                $url  = sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&amp;amp;', '=', $result-&gt;week);
</ins><span class="cx">                                                 $text = $arc_week_start . $archive_week_separator . $arc_week_end;
</span><span class="cx">                                                 if ($show_post_count)
</span><del>-                                                        $after = '&amp;nbsp;('.$arcresult-&gt;posts.')'.$afterafter;
</del><ins>+                                                        $after = '&amp;nbsp;('.$result-&gt;posts.')'.$afterafter;
</ins><span class="cx">                                                 $output .= get_archives_link($url, $text, $format, $before, $after);
</span><span class="cx">                                         }
</span><span class="cx">                                 }
</span><span class="lines">@@ -1016,23 +1010,20 @@
</span><span class="cx">         } elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) {
</span><span class="cx">                 $orderby = ('alpha' == $type) ? 'post_title ASC ' : 'post_date DESC ';
</span><span class="cx">                 $query = &quot;SELECT * FROM $wpdb-&gt;posts $join $where ORDER BY $orderby $limit&quot;;
</span><del>-                $key = md5($query);
-                $cache = wp_cache_get( 'wp_get_archives' , 'general');
-                if ( !isset( $cache[ $key ] ) ) {
-                        $arcresults = $wpdb-&gt;get_results($query);
-                        $cache[ $key ] = $arcresults;
-                        wp_cache_set( 'wp_get_archives', $cache, 'general' );
-                } else {
-                        $arcresults = $cache[ $key ];
</del><ins>+                $key = md5( $query );
+                $key = &quot;wp_get_archives:$key:$last_changed&quot;;
+                if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
+                        $results = $wpdb-&gt;get_results( $query );
+                        wp_cache_set( $key, $results, 'posts' );
</ins><span class="cx">                 }
</span><del>-                if ( $arcresults ) {
-                        foreach ( (array) $arcresults as $arcresult ) {
-                                if ( $arcresult-&gt;post_date != '0000-00-00 00:00:00' ) {
-                                        $url  = get_permalink( $arcresult );
-                                        if ( $arcresult-&gt;post_title )
-                                                $text = strip_tags( apply_filters( 'the_title', $arcresult-&gt;post_title, $arcresult-&gt;ID ) );
</del><ins>+                if ( $results ) {
+                        foreach ( (array) $results as $result ) {
+                                if ( $result-&gt;post_date != '0000-00-00 00:00:00' ) {
+                                        $url  = get_permalink( $result );
+                                        if ( $result-&gt;post_title )
+                                                $text = strip_tags( apply_filters( 'the_title', $result-&gt;post_title, $result-&gt;ID ) );
</ins><span class="cx">                                         else
</span><del>-                                                $text = $arcresult-&gt;ID;
</del><ins>+                                                $text = $result-&gt;ID;
</ins><span class="cx">                                         $output .= get_archives_link($url, $text, $format, $before, $after);
</span><span class="cx">                                 }
</span><span class="cx">                         }
</span></span></pre>
</div>
</div>

</body>
</html>