WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/16/2014 04:44:13 AM (7 years ago)
Author:
wonderboymusic
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r29902 r29915  
    12921292    }
    12931293
     1294    $group = $taxonomy . ':' . wp_get_last_changed( 'terms' );
    12941295    if ( is_object($term) && empty($term->filter) ) {
    1295         wp_cache_add($term->term_id, $term, $taxonomy);
     1296        wp_cache_add( $term->term_id, $term, $taxonomy );
     1297        wp_cache_add( "slug:{$term->slug}", $term->term_id, $group );
     1298        wp_cache_add( "name:" . md5( $term->name ), $term->term_id, $group );
    12961299        $_term = $term;
    12971300    } else {
     
    13041307            if ( ! $_term )
    13051308                return null;
    1306             wp_cache_add($term, $_term, $taxonomy);
     1309            wp_cache_add( $term, $_term, $taxonomy );
     1310            wp_cache_add( "slug:{$_term->slug}", $term, $group );
     1311            wp_cache_add( "name:" . md5( $_term->name ), $term, $group );
    13071312        }
    13081313    }
     
    13761381        return false;
    13771382
     1383    $cache = false;
     1384    $group = $taxonomy . ':' . wp_get_last_changed( 'terms' );
    13781385    if ( 'slug' == $field ) {
    13791386        $field = 't.slug';
     
    13811388        if ( empty($value) )
    13821389            return false;
     1390
     1391        $term_id = wp_cache_get( "slug:{$value}", $group );
     1392        if ( $term_id ) {
     1393            $value = $term_id;
     1394            $cache = true;
     1395        }
    13831396    } else if ( 'name' == $field ) {
    13841397        // Assume already escaped
    13851398        $value = wp_unslash($value);
    13861399        $field = 't.name';
     1400        $term_id = wp_cache_get( "name:" . md5( $value ), $group );
     1401        if ( $term_id ) {
     1402            $value = $term_id;
     1403            $cache = true;
     1404        }
    13871405    } else if ( 'term_taxonomy_id' == $field ) {
    13881406        $value = (int) $value;
    13891407        $field = 'tt.term_taxonomy_id';
    13901408    } else {
     1409        $cache = true;
     1410    }
     1411
     1412    if ( $cache ) {
    13911413        $term = get_term( (int) $value, $taxonomy, $output, $filter);
    1392         if ( is_wp_error( $term ) )
     1414        if ( is_wp_error( $term ) ) {
    13931415            $term = false;
    1394         return $term;
    1395     }
    1396 
    1397     $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) );
     1416        }
     1417    } else {
     1418        $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) );
     1419    }
     1420
    13981421    if ( !$term )
    13991422        return false;
    14001423
    1401     wp_cache_add($term->term_id, $term, $taxonomy);
    1402 
    14031424    /** This filter is documented in wp-includes/taxonomy.php */
    14041425    $term = apply_filters( 'get_term', $term, $taxonomy );
     
    14081429
    14091430    $term = sanitize_term($term, $taxonomy, $filter);
     1431
     1432    wp_cache_add( $term->term_id, $term, $taxonomy );
     1433    wp_cache_add( "slug:{$term->slug}", $term->term_id, $group );
     1434    wp_cache_add( "name:" . md5( $term->name ), $term->term_id, $group );
    14101435
    14111436    if ( $output == OBJECT ) {
     
    18911916
    18921917    $terms = $wpdb->get_results($query);
    1893     if ( 'all' == $_fields ) {
    1894         update_term_cache($terms);
    1895     }
    18961918
    18971919    if ( empty($terms) ) {
     
    35853607 */
    35863608function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) {
    3587     global $wpdb;
     3609    global $_wp_suspend_cache_invalidation, $wpdb;
     3610
     3611    if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
     3612        return;
     3613    }
    35883614
    35893615    if ( !is_array($ids) )
     
    36323658    }
    36333659
    3634     wp_cache_set( 'last_changed', microtime(), 'terms' );
     3660    wp_get_last_changed( 'terms', true );
    36353661}
    36363662
     
    37273753 */
    37283754function update_term_cache($terms, $taxonomy = '') {
     3755    global $_wp_suspend_cache_invalidation;
     3756
     3757    if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
     3758        return;
     3759    }
     3760
    37293761    foreach ( (array) $terms as $term ) {
    37303762        $term_taxonomy = $taxonomy;
     
    37323764            $term_taxonomy = $term->taxonomy;
    37333765
    3734         wp_cache_add($term->term_id, $term, $term_taxonomy);
     3766        wp_cache_add( $term->term_id, $term, $term_taxonomy );
     3767        $group = $term_taxonomy . ':' . wp_get_last_changed( 'terms', true );
     3768        wp_cache_add( "slug:{$term->slug}", $term->term_id, $group );
     3769        wp_cache_add( "name:" . md5( $term->name ), $term->term_id, $group );
    37353770    }
    37363771}
Note: See TracChangeset for help on using the changeset viewer.