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].

Merges [30900] to the 4.1 branch.

See #21760.

Built from https://develop.svn.wordpress.org/branches/4.1@30914


git-svn-id: http://core.svn.wordpress.org/branches/4.1@30904 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
John Blackbourn 2014-12-16 19:07:23 +00:00
parent b67799149b
commit db3f2449d9
1 changed files with 12 additions and 51 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 );
}
}