Remove caching for `get_term_by()` calls.

The new cache group scheme causes term invalidation to be overly broad, so
that busting the cache for one term will bust the cache for all terms in the
taxonomy. We'll have another go at more focused use of the 'last_changed'
incrementor in a future release.

Reverts [29915], [30073], [30080], [30108], [30112].
See #21760.

git-svn-id: https://develop.svn.wordpress.org/trunk@30900 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2014-12-16 13:26:19 +00:00
parent 3a63ae0cb2
commit bf62165353
2 changed files with 12 additions and 169 deletions

View File

@ -1305,24 +1305,19 @@ function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
return $error;
}
$incrementor = wp_cache_get( 'last_changed', 'terms' );
if ( is_object($term) && empty($term->filter) ) {
wp_cache_add( $term->term_id, $term, $taxonomy . ':terms:' . $incrementor );
wp_cache_add( $term->slug, $term->term_id, $taxonomy . ':slugs:' . $incrementor );
wp_cache_add( $term->name, $term->term_id, $taxonomy . ':names:' . $incrementor );
wp_cache_add( $term->term_id, $term, $taxonomy );
$_term = $term;
} else {
if ( is_object($term) )
$term = $term->term_id;
if ( !$term = (int) $term )
return null;
if ( ! $_term = wp_cache_get( $term, $taxonomy . ':terms:' . $incrementor ) ) {
if ( ! $_term = wp_cache_get( $term, $taxonomy ) ) {
$_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) );
if ( ! $_term )
return null;
wp_cache_add( $term, $_term, $taxonomy . ':terms:' . $incrementor );
wp_cache_add( $_term->slug, $term, $taxonomy . ':slugs:' . $incrementor );
wp_cache_add( $_term->name, $term, $taxonomy . ':names:' . $incrementor );
wp_cache_add( $term, $_term, $taxonomy );
}
}
@ -1393,47 +1388,31 @@ function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw
if ( ! taxonomy_exists($taxonomy) )
return false;
$cache = false;
$incrementor = wp_cache_get( 'last_changed', 'terms' );
if ( 'slug' == $field ) {
$field = 't.slug';
$value = sanitize_title($value);
if ( empty($value) )
return false;
$term_id = wp_cache_get( $value, $taxonomy . ':slugs:' . $incrementor );
if ( $term_id ) {
$value = $term_id;
$cache = true;
}
} else if ( 'name' == $field ) {
// Assume already escaped
$value = wp_unslash($value);
$field = 't.name';
$term_id = wp_cache_get( $value, $taxonomy . ':names:' . $incrementor );
if ( $term_id ) {
$value = $term_id;
$cache = true;
}
} else if ( 'term_taxonomy_id' == $field ) {
$value = (int) $value;
$field = 'tt.term_taxonomy_id';
} else {
$cache = true;
}
if ( $cache ) {
$term = get_term( (int) $value, $taxonomy, $output, $filter);
if ( is_wp_error( $term ) ) {
$term = get_term( (int) $value, $taxonomy, $output, $filter );
if ( is_wp_error( $term ) )
$term = false;
}
} 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) );
return $term;
}
if ( !$term )
$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 ) );
if ( ! $term )
return false;
wp_cache_add( $term->term_id, $term, $taxonomy );
/** This filter is documented in wp-includes/taxonomy.php */
$term = apply_filters( 'get_term', $term, $taxonomy );
@ -1442,10 +1421,6 @@ function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw
$term = sanitize_term($term, $taxonomy, $filter);
wp_cache_add( $term->term_id, $term, $taxonomy . ':terms:' . $incrementor );
wp_cache_add( $term->slug, $term->term_id, $taxonomy . ':slugs:' . $incrementor );
wp_cache_add( $term->name, $term->term_id, $taxonomy . ':names:' . $incrementor );
if ( $output == OBJECT ) {
return $term;
} elseif ( $output == ARRAY_A ) {
@ -3649,11 +3624,7 @@ function clean_object_term_cache($object_ids, $object_type) {
* @param bool $clean_taxonomy Whether to clean taxonomy wide caches (true), or just individual term object caches (false). Default is true.
*/
function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) {
global $_wp_suspend_cache_invalidation, $wpdb;
if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
return;
}
global $wpdb;
if ( !is_array($ids) )
$ids = array($ids);
@ -3792,22 +3763,12 @@ function update_object_term_cache($object_ids, $object_type) {
* @param string $taxonomy Optional. Update Term to this taxonomy in cache
*/
function update_term_cache($terms, $taxonomy = '') {
global $_wp_suspend_cache_invalidation;
if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
return;
}
foreach ( (array) $terms as $term ) {
$term_taxonomy = $taxonomy;
if ( empty($term_taxonomy) )
$term_taxonomy = $term->taxonomy;
$incrementor = wp_cache_set( 'last_changed', microtime(), 'terms' );
wp_cache_add( $term->term_id, $term, $term_taxonomy . ':terms:' . $incrementor );
wp_cache_add( $term->slug, $term->term_id, $taxonomy . ':slugs:' . $incrementor );
wp_cache_add( $term->name, $term->term_id, $taxonomy . ':names:' . $incrementor );
wp_cache_add( $term->term_id, $term, $term_taxonomy );
}
}

View File

@ -93,122 +93,4 @@ class Tests_Term_Cache extends WP_UnitTestCase {
_unregister_taxonomy( $tax );
}
/**
* @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:terms:' . wp_cache_get( 'last_changed', 'terms' ) ) );
$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:terms:' . wp_cache_get( 'last_changed', 'terms' ) ) );
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;
$cache_key_name = $name;
$term_id = $this->factory->term->create( array( 'slug' => $slug, 'name' => $name, 'taxonomy' => $taxonomy ) );
$last_changed = wp_cache_get( 'last_changed', 'terms' );
$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 . ':terms:' . wp_cache_get( 'last_changed', 'terms' ) ) );
$this->assertSame( $term_id, wp_cache_get( $cache_key_slug, $taxonomy . ':slugs:' . wp_cache_get( 'last_changed', 'terms' ) ) );
$this->assertSame( $term_id, wp_cache_get( $cache_key_name, $taxonomy . ':names:' . wp_cache_get( 'last_changed', 'terms' ) ) );
$suspend = 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 . ':terms:' . wp_cache_get( 'last_changed', 'terms' ) ) );
$this->assertSame( $term_id, wp_cache_get( $cache_key_slug, $taxonomy . ':slugs:' . wp_cache_get( 'last_changed', 'terms' ) ) );
$this->assertSame( $term_id, wp_cache_get( $cache_key_name, $taxonomy . ':names:' . wp_cache_get( 'last_changed', 'terms' ) ) );
// Verify that last changed has not been updated as part of an invalidation routine
$this->assertSame( $last_changed, wp_cache_get( 'last_changed', 'terms' ) );
// Clean up.
wp_suspend_cache_invalidation( $suspend );
}
}