Clean up `get_term_by()` caching.

* Fix cache key/group modification that was missed in [30073].
* Update unit tests to reflect new key/group format.

Props tollmanz.
Fixes #21760.

git-svn-id: https://develop.svn.wordpress.org/trunk@30108 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2014-10-30 02:49:51 +00:00
parent cf1d77c101
commit 6a7443671b
2 changed files with 13 additions and 14 deletions

View File

@ -1295,7 +1295,7 @@ function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
$term = $term->term_id;
if ( !$term = (int) $term )
return null;
if ( ! $_term = wp_cache_get($term, $taxonomy) ) {
if ( ! $_term = wp_cache_get( $term, $taxonomy . ':terms:' . $incrementor ) ) {
$_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;

View File

@ -108,7 +108,7 @@ class Tests_Term_Cache extends WP_UnitTestCase {
$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( $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 );
@ -128,7 +128,7 @@ class Tests_Term_Cache extends WP_UnitTestCase {
$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( $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' ) );
@ -183,30 +183,29 @@ class Tests_Term_Cache extends WP_UnitTestCase {
$slug = 'taco';
$name = 'Taco';
$taxonomy = 'post_tag';
$cache_key_slug = 'slug:' . $slug;
$cache_key_name = 'name:' . md5( $name );
$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' );
$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 ) );
$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' ) ) );
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 ) );
$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->assertEquals( $last_changed, wp_get_last_changed( 'terms' ) );
$this->assertSame( $last_changed, wp_cache_get( 'last_changed', 'terms' ) );
}
}
}