mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-04-02 11:44:33 +00:00
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 #21760.
git-svn-id: https://develop.svn.wordpress.org/trunk@29915 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -93,4 +93,120 @@ 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' ) );
|
||||
|
||||
$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' ) );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user