<!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>[29915] trunk: Cache `get_term_by()` calls:</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 { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { 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" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/29915">29915</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/29915","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>wonderboymusic</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2014-10-16 04:44:13 +0000 (Thu, 16 Oct 2014)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Cache `get_term_by()` calls:
* Add a helper function, `wp_get_last_changed()`, to retrieve a last-modified timestamp by cache group
* When caching a term, also make cache entries for slug and name via `slug:{$term_id}` and `name:{$term_id}` keys in the `$taxonomy:$last_changed` bucket that reference the term_id
* In `clean_term_cache()` and `update_term_cache()`, respect `$_wp_suspend_cache_invalidation`
* Original term cache entries maintain BC
Adds unit tests.
Props wonderboymusic, tollmanz, boonebgorges.
Fixes <a href="https://core.trac.wordpress.org/ticket/21760">#21760</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesfunctionsphp">trunk/src/wp-includes/functions.php</a></li>
<li><a href="#trunksrcwpincludestaxonomyphp">trunk/src/wp-includes/taxonomy.php</a></li>
<li><a href="#trunktestsphpunitteststermcachephp">trunk/tests/phpunit/tests/term/cache.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/functions.php 2014-10-16 04:27:07 UTC (rev 29914)
+++ trunk/src/wp-includes/functions.php 2014-10-16 04:44:13 UTC (rev 29915)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4629,3 +4629,21 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return (bool) $var;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Helper function to retrieve an incrementer identified by $group
+ *
+ * @since 4.1.0
+ *
+ * @param string $group The cache group for the incrementer.
+ * @param bool $force Whether or not to generate a new incrementor.
+ * @return int The timestamp representing 'last_changed'.
+ */
+function wp_get_last_changed( $group, $force = false ) {
+ $last_changed = wp_cache_get( 'last_changed', $group );
+ if ( ! $last_changed || true === $force ) {
+ $last_changed = microtime();
+ wp_cache_set( 'last_changed', $last_changed, $group );
+ }
+ return $last_changed;
+}
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of file
</span></span></pre></div>
<a id="trunksrcwpincludestaxonomyphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/taxonomy.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/taxonomy.php 2014-10-16 04:27:07 UTC (rev 29914)
+++ trunk/src/wp-includes/taxonomy.php 2014-10-16 04:44:13 UTC (rev 29915)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1291,8 +1291,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return $error;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $group = $taxonomy . ':' . wp_get_last_changed( 'terms' );
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_object($term) && empty($term->filter) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_cache_add($term->term_id, $term, $taxonomy);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_cache_add( $term->term_id, $term, $taxonomy );
+ wp_cache_add( "slug:{$term->slug}", $term->term_id, $group );
+ wp_cache_add( "name:" . md5( $term->name ), $term->term_id, $group );
</ins><span class="cx" style="display: block; padding: 0 10px"> $_term = $term;
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_object($term) )
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1303,7 +1306,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND t.term_id = %d LIMIT 1", $taxonomy, $term) );
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $_term )
</span><span class="cx" style="display: block; padding: 0 10px"> return null;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_cache_add($term, $_term, $taxonomy);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_cache_add( $term, $_term, $taxonomy );
+ wp_cache_add( "slug:{$_term->slug}", $term, $group );
+ wp_cache_add( "name:" . md5( $_term->name ), $term, $group );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1375,31 +1380,47 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! taxonomy_exists($taxonomy) )
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $cache = false;
+ $group = $taxonomy . ':' . wp_get_last_changed( 'terms' );
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( 'slug' == $field ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $field = 't.slug';
</span><span class="cx" style="display: block; padding: 0 10px"> $value = sanitize_title($value);
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty($value) )
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ $term_id = wp_cache_get( "slug:{$value}", $group );
+ if ( $term_id ) {
+ $value = $term_id;
+ $cache = true;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> } else if ( 'name' == $field ) {
</span><span class="cx" style="display: block; padding: 0 10px"> // Assume already escaped
</span><span class="cx" style="display: block; padding: 0 10px"> $value = wp_unslash($value);
</span><span class="cx" style="display: block; padding: 0 10px"> $field = 't.name';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $term_id = wp_cache_get( "name:" . md5( $value ), $group );
+ if ( $term_id ) {
+ $value = $term_id;
+ $cache = true;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> } else if ( 'term_taxonomy_id' == $field ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $value = (int) $value;
</span><span class="cx" style="display: block; padding: 0 10px"> $field = 'tt.term_taxonomy_id';
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $cache = true;
+ }
+
+ if ( $cache ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $term = get_term( (int) $value, $taxonomy, $output, $filter);
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( is_wp_error( $term ) )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( is_wp_error( $term ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $term = false;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $term;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
+ } else {
+ $term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND $field = %s LIMIT 1", $taxonomy, $value) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND $field = %s LIMIT 1", $taxonomy, $value) );
</del><span class="cx" style="display: block; padding: 0 10px"> if ( !$term )
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_cache_add($term->term_id, $term, $taxonomy);
-
</del><span class="cx" style="display: block; padding: 0 10px"> /** This filter is documented in wp-includes/taxonomy.php */
</span><span class="cx" style="display: block; padding: 0 10px"> $term = apply_filters( 'get_term', $term, $taxonomy );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1408,6 +1429,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $term = sanitize_term($term, $taxonomy, $filter);
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_cache_add( $term->term_id, $term, $taxonomy );
+ wp_cache_add( "slug:{$term->slug}", $term->term_id, $group );
+ wp_cache_add( "name:" . md5( $term->name ), $term->term_id, $group );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $output == OBJECT ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $term;
</span><span class="cx" style="display: block; padding: 0 10px"> } elseif ( $output == ARRAY_A ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1890,9 +1915,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $terms = $wpdb->get_results($query);
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 'all' == $_fields ) {
- update_term_cache($terms);
- }
</del><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty($terms) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3584,8 +3606,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param bool $clean_taxonomy Whether to clean taxonomy wide caches (true), or just individual term object caches (false). Default is true.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wpdb;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $_wp_suspend_cache_invalidation, $wpdb;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
+ return;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( !is_array($ids) )
</span><span class="cx" style="display: block; padding: 0 10px"> $ids = array($ids);
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3631,7 +3657,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> do_action( 'clean_term_cache', $ids, $taxonomy );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_cache_set( 'last_changed', microtime(), 'terms' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_get_last_changed( 'terms', true );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3726,12 +3752,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $taxonomy Optional. Update Term to this taxonomy in cache
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function update_term_cache($terms, $taxonomy = '') {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $_wp_suspend_cache_invalidation;
+
+ if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
+ return;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( (array) $terms as $term ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $term_taxonomy = $taxonomy;
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty($term_taxonomy) )
</span><span class="cx" style="display: block; padding: 0 10px"> $term_taxonomy = $term->taxonomy;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_cache_add($term->term_id, $term, $term_taxonomy);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_cache_add( $term->term_id, $term, $term_taxonomy );
+ $group = $term_taxonomy . ':' . wp_get_last_changed( 'terms', true );
+ wp_cache_add( "slug:{$term->slug}", $term->term_id, $group );
+ wp_cache_add( "name:" . md5( $term->name ), $term->term_id, $group );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunitteststermcachephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/term/cache.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/term/cache.php 2014-10-16 04:27:07 UTC (rev 29914)
+++ trunk/tests/phpunit/tests/term/cache.php 2014-10-16 04:44:13 UTC (rev 29915)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -93,4 +93,120 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> _unregister_taxonomy( $tax );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * @ticket 21760
+ */
+ function test_get_term_by_slug_cache() {
+ global $wpdb;
+ $term_id = $this->factory->term->create( array( 'slug' => 'burrito', 'taxonomy' => 'post_tag' ) );
+
+ $queries = $wpdb->num_queries;
+ get_term_by( 'slug', 'burrito', 'post_tag' );
+ $initial = $queries + 1;
+ $this->assertEquals( $initial, $wpdb->num_queries );
+ $term = get_term_by( 'slug', 'burrito', 'post_tag' );
+ $this->assertEquals( $initial, $wpdb->num_queries );
+
+ $this->assertEquals( $term, wp_cache_get( $term_id, 'post_tag' ) );
+
+ $this->assertEquals( get_term( $term_id, 'post_tag' ), $term );
+ $this->assertEquals( $initial, $wpdb->num_queries );
+ }
+
+ /**
+ * @ticket 21760
+ */
+ function test_get_term_by_slug_cache_update() {
+ global $wpdb;
+ $term_id = $this->factory->term->create( array( 'slug' => 'burrito', 'taxonomy' => 'post_tag' ) );
+
+ $queries = $wpdb->num_queries;
+ get_term_by( 'slug', 'burrito', 'post_tag' );
+ $initial = $queries + 1;
+ $this->assertEquals( $initial, $wpdb->num_queries );
+ $term = get_term_by( 'slug', 'burrito', 'post_tag' );
+ $this->assertEquals( $initial, $wpdb->num_queries );
+
+ $this->assertEquals( $term, wp_cache_get( $term_id, 'post_tag' ) );
+
+ wp_update_term( $term_id, 'post_tag', array( 'name' => 'Taco' ) );
+ $this->assertNotEquals( $term, get_term( $term_id, 'post_tag' ) );
+ $after_queries = $wpdb->num_queries;
+ get_term_by( 'slug', 'burrito', 'post_tag' );
+ $this->assertEquals( $after_queries, $wpdb->num_queries );
+ }
+
+ /**
+ * @ticket 21760
+ */
+ function test_get_term_by_name_cache() {
+ global $wpdb;
+ $term_id = $this->factory->term->create( array( 'name' => 'burrito', 'taxonomy' => 'post_tag' ) );
+
+ $queries = $wpdb->num_queries;
+ get_term_by( 'name', 'burrito', 'post_tag' );
+ $initial = $queries + 1;
+ $this->assertEquals( $initial, $wpdb->num_queries );
+ $term = get_term_by( 'name', 'burrito', 'post_tag' );
+ $this->assertEquals( $initial, $wpdb->num_queries );
+
+ $this->assertEquals( get_term( $term_id, 'post_tag' ), $term );
+ $this->assertEquals( $initial, $wpdb->num_queries );
+ }
+
+ /**
+ * @ticket 21760
+ */
+ function test_get_term_by_name_cache_update() {
+ global $wpdb;
+ $term_id = $this->factory->term->create( array( 'name' => 'burrito', 'taxonomy' => 'post_tag' ) );
+
+ $queries = $wpdb->num_queries;
+ get_term_by( 'name', 'burrito', 'post_tag' );
+ $initial = $queries + 1;
+ $this->assertEquals( $initial, $wpdb->num_queries );
+ $term = get_term_by( 'name', 'burrito', 'post_tag' );
+ $this->assertEquals( $initial, $wpdb->num_queries );
+
+ wp_update_term( $term_id, 'post_tag', array( 'slug' => 'Taco' ) );
+ $this->assertNotEquals( $term, get_term( $term_id, 'post_tag' ) );
+ $after_queries = $wpdb->num_queries;
+ get_term_by( 'name', 'burrito', 'post_tag' );
+ $this->assertEquals( $after_queries, $wpdb->num_queries );
+ }
+
+ /**
+ * @ticket 21760
+ */
+ function test_invalidating_term_caches_should_fail_when_invalidation_is_suspended() {
+ $slug = 'taco';
+ $name = 'Taco';
+ $taxonomy = 'post_tag';
+ $cache_key_slug = 'slug:' . $slug;
+ $cache_key_name = 'name:' . md5( $name );
+
+ $term_id = $this->factory->term->create( array( 'slug' => $slug, 'name' => $name, 'taxonomy' => $taxonomy ) );
+
+ $last_changed = wp_get_last_changed( 'terms' );
+ $group = $taxonomy . ':' . $last_changed;
+
+ $term = get_term_by( 'slug', $slug, $taxonomy );
+
+ // Verify the term is cached by ID, slug and name
+ $this->assertEquals( $term, wp_cache_get( $term_id, $taxonomy ) );
+ $this->assertEquals( $term_id, wp_cache_get( $cache_key_slug, $group ) );
+ $this->assertEquals( $term_id, wp_cache_get( $cache_key_name, $group ) );
+
+ wp_suspend_cache_invalidation();
+ clean_term_cache( $term_id, $taxonomy );
+
+ // Verify that the cached value still matches the correct value
+ $this->assertEquals( $term, wp_cache_get( $term_id, $taxonomy ) );
+ $this->assertEquals( $term_id, wp_cache_get( $cache_key_slug, $group ) );
+ $this->assertEquals( $term_id, wp_cache_get( $cache_key_name, $group ) );
+
+ // Verify that last changed has not been updated as part of an invalidation routine
+ $this->assertEquals( $last_changed, wp_get_last_changed( 'terms' ) );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of file
</span></span></pre>
</div>
</div>
</body>
</html>