Cache results in `get_objects_in_term()`.

This helps to reduce database queries when generating nav menus.

Props spacedmonkey.
Fixes #37094.

git-svn-id: https://develop.svn.wordpress.org/trunk@40921 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2017-06-22 03:18:18 +00:00
parent 7104971dca
commit ddc823a3e9
2 changed files with 80 additions and 1 deletions

View File

@ -650,7 +650,17 @@ function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
$taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'";
$term_ids = "'" . implode( "', '", $term_ids ) . "'";
$object_ids = $wpdb->get_col("SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order");
$sql = "SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order";
$last_changed = wp_cache_get_last_changed( 'terms' );
$cache_key = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed";
$cache = wp_cache_get( $cache_key, 'terms' );
if ( false === $cache ) {
$object_ids = $wpdb->get_col( $sql );
wp_cache_set( $cache_key, $object_ids, 'terms' );
} else {
$object_ids = (array) $cache;
}
if ( ! $object_ids ){
return array();

View File

@ -291,6 +291,75 @@ class Tests_Taxonomy extends WP_UnitTestCase {
$this->assertEquals( array_reverse( $posts_with_tag ), get_objects_in_term( $tag_id, 'post_tag', array( 'order' => 'desc' ) ) );
}
/**
* @ticket 37094
*/
public function test_term_assignment_should_invalidate_get_objects_in_term_cache() {
register_taxonomy( 'wptests_tax', 'post' );
$posts = self::factory()->post->create_many( 2 );
$term_id = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax',
) );
wp_set_object_terms( $posts[1], $term_id, 'wptests_tax' );
// Prime cache.
$before = get_objects_in_term( $term_id, 'wptests_tax' );
$this->assertEqualSets( array( $posts[1] ), $before );
wp_set_object_terms( $posts[1], array(), 'wptests_tax' );
$after = get_objects_in_term( $term_id, 'wptests_tax' );
$this->assertSame( array(), $after );
}
/**
* @ticket 37094
*/
public function test_term_deletion_should_invalidate_get_objects_in_term_cache() {
register_taxonomy( 'wptests_tax', 'post' );
$posts = self::factory()->post->create_many( 2 );
$term_id = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax',
) );
wp_set_object_terms( $posts[1], $term_id, 'wptests_tax' );
// Prime cache.
$before = get_objects_in_term( $term_id, 'wptests_tax' );
$this->assertEqualSets( array( $posts[1] ), $before );
wp_delete_term( $term_id, 'wptests_tax' );
$after = get_objects_in_term( $term_id, 'wptests_tax' );
$this->assertSame( array(), $after );
}
/**
* @ticket 37094
*/
public function test_post_deletion_should_invalidate_get_objects_in_term_cache() {
register_taxonomy( 'wptests_tax', 'post' );
$posts = self::factory()->post->create_many( 2 );
$term_id = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax',
) );
wp_set_object_terms( $posts[1], $term_id, 'wptests_tax' );
// Prime cache.
$before = get_objects_in_term( $term_id, 'wptests_tax' );
$this->assertEqualSets( array( $posts[1] ), $before );
wp_delete_post( $posts[1], true );
$after = get_objects_in_term( $term_id, 'wptests_tax' );
$this->assertSame( array(), $after );
}
/**
* @ticket 25706
*/