WordPress.org

Make WordPress Core

Changeset 30914


Ignore:
Timestamp:
12/16/2014 07:06:15 PM (6 years ago)
Author:
johnbillion
Message:

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.

Location:
branches/4.1
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.1

  • branches/4.1/src/wp-includes/taxonomy.php

    r30759 r30914  
    13061306    }
    13071307
    1308     $incrementor = wp_cache_get( 'last_changed', 'terms' );
    13091308    if ( is_object($term) && empty($term->filter) ) {
    1310         wp_cache_add( $term->term_id, $term, $taxonomy . ':terms:' . $incrementor );
    1311         wp_cache_add( $term->slug, $term->term_id, $taxonomy . ':slugs:' . $incrementor );
    1312         wp_cache_add( $term->name, $term->term_id, $taxonomy . ':names:' . $incrementor );
     1309        wp_cache_add( $term->term_id, $term, $taxonomy );
    13131310        $_term = $term;
    13141311    } else {
     
    13171314        if ( !$term = (int) $term )
    13181315            return null;
    1319         if ( ! $_term = wp_cache_get( $term, $taxonomy . ':terms:' . $incrementor ) ) {
     1316        if ( ! $_term = wp_cache_get( $term, $taxonomy ) ) {
    13201317            $_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) );
    13211318            if ( ! $_term )
    13221319                return null;
    1323             wp_cache_add( $term, $_term, $taxonomy . ':terms:' . $incrementor );
    1324             wp_cache_add( $_term->slug, $term, $taxonomy . ':slugs:' . $incrementor );
    1325             wp_cache_add( $_term->name, $term, $taxonomy . ':names:' . $incrementor );
     1320            wp_cache_add( $term, $_term, $taxonomy );
    13261321        }
    13271322    }
     
    13941389        return false;
    13951390
    1396     $cache = false;
    1397     $incrementor = wp_cache_get( 'last_changed', 'terms' );
    13981391    if ( 'slug' == $field ) {
    13991392        $field = 't.slug';
     
    14011394        if ( empty($value) )
    14021395            return false;
    1403 
    1404         $term_id = wp_cache_get( $value, $taxonomy . ':slugs:' . $incrementor );
    1405         if ( $term_id ) {
    1406             $value = $term_id;
    1407             $cache = true;
    1408         }
    14091396    } else if ( 'name' == $field ) {
    14101397        // Assume already escaped
    14111398        $value = wp_unslash($value);
    14121399        $field = 't.name';
    1413         $term_id = wp_cache_get( $value, $taxonomy . ':names:' . $incrementor  );
    1414         if ( $term_id ) {
    1415             $value = $term_id;
    1416             $cache = true;
    1417         }
    14181400    } else if ( 'term_taxonomy_id' == $field ) {
    14191401        $value = (int) $value;
    14201402        $field = 'tt.term_taxonomy_id';
    14211403    } else {
    1422         $cache = true;
    1423     }
    1424 
    1425     if ( $cache ) {
    1426         $term = get_term( (int) $value, $taxonomy, $output, $filter);
    1427         if ( is_wp_error( $term ) ) {
     1404        $term = get_term( (int) $value, $taxonomy, $output, $filter );
     1405        if ( is_wp_error( $term ) )
    14281406            $term = false;
    1429         }
    1430     } else {
    1431         $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) );
    1432     }
    1433 
    1434     if ( !$term )
     1407        return $term;
     1408    }
     1409
     1410    $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 ) );
     1411    if ( ! $term )
    14351412        return false;
     1413
     1414    wp_cache_add( $term->term_id, $term, $taxonomy );
    14361415
    14371416    /** This filter is documented in wp-includes/taxonomy.php */
     
    14421421
    14431422    $term = sanitize_term($term, $taxonomy, $filter);
    1444 
    1445     wp_cache_add( $term->term_id, $term, $taxonomy . ':terms:' . $incrementor );
    1446     wp_cache_add( $term->slug, $term->term_id, $taxonomy . ':slugs:' . $incrementor );
    1447     wp_cache_add( $term->name, $term->term_id, $taxonomy . ':names:' . $incrementor );
    14481423
    14491424    if ( $output == OBJECT ) {
     
    36503625 */
    36513626function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) {
    3652     global $_wp_suspend_cache_invalidation, $wpdb;
    3653 
    3654     if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
    3655         return;
    3656     }
     3627    global $wpdb;
    36573628
    36583629    if ( !is_array($ids) )
     
    37933764 */
    37943765function update_term_cache($terms, $taxonomy = '') {
    3795     global $_wp_suspend_cache_invalidation;
    3796 
    3797     if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
    3798         return;
    3799     }
    3800 
    38013766    foreach ( (array) $terms as $term ) {
    38023767        $term_taxonomy = $taxonomy;
     
    38043769            $term_taxonomy = $term->taxonomy;
    38053770
    3806         $incrementor = wp_cache_set( 'last_changed', microtime(), 'terms' );
    3807 
    3808         wp_cache_add( $term->term_id, $term, $term_taxonomy . ':terms:' . $incrementor );
    3809         wp_cache_add( $term->slug, $term->term_id, $taxonomy . ':slugs:' . $incrementor );
    3810         wp_cache_add( $term->name, $term->term_id, $taxonomy . ':names:' . $incrementor );
     3771        wp_cache_add( $term->term_id, $term, $term_taxonomy );
    38113772    }
    38123773}
  • branches/4.1/tests/phpunit/tests/term/cache.php

    r30112 r30914  
    9494        _unregister_taxonomy( $tax );
    9595    }
    96 
    97     /**
    98      * @ticket 21760
    99      */
    100     function test_get_term_by_slug_cache() {
    101         global $wpdb;
    102         $term_id = $this->factory->term->create( array( 'slug' => 'burrito', 'taxonomy' => 'post_tag' ) );
    103 
    104         $queries = $wpdb->num_queries;
    105         get_term_by( 'slug', 'burrito', 'post_tag' );
    106         $initial = $queries + 1;
    107         $this->assertEquals( $initial, $wpdb->num_queries );
    108         $term = get_term_by( 'slug', 'burrito', 'post_tag' );
    109         $this->assertEquals( $initial, $wpdb->num_queries );
    110 
    111         $this->assertEquals( $term, wp_cache_get( $term_id, 'post_tag:terms:' . wp_cache_get( 'last_changed', 'terms' ) ) );
    112 
    113         $this->assertEquals( get_term( $term_id, 'post_tag' ), $term );
    114         $this->assertEquals( $initial, $wpdb->num_queries );
    115     }
    116 
    117     /**
    118      * @ticket 21760
    119      */
    120     function test_get_term_by_slug_cache_update() {
    121         global $wpdb;
    122         $term_id = $this->factory->term->create( array( 'slug' => 'burrito', 'taxonomy' => 'post_tag' ) );
    123 
    124         $queries = $wpdb->num_queries;
    125         get_term_by( 'slug', 'burrito', 'post_tag' );
    126         $initial = $queries + 1;
    127         $this->assertEquals( $initial, $wpdb->num_queries );
    128         $term = get_term_by( 'slug', 'burrito', 'post_tag' );
    129         $this->assertEquals( $initial, $wpdb->num_queries );
    130 
    131         $this->assertEquals( $term, wp_cache_get( $term_id, 'post_tag:terms:' . wp_cache_get( 'last_changed', 'terms' ) ) );
    132 
    133         wp_update_term( $term_id, 'post_tag', array( 'name' => 'Taco' ) );
    134         $this->assertNotEquals( $term, get_term( $term_id, 'post_tag' ) );
    135         $after_queries = $wpdb->num_queries;
    136         get_term_by( 'slug', 'burrito', 'post_tag' );
    137         $this->assertEquals( $after_queries, $wpdb->num_queries );
    138     }
    139 
    140     /**
    141      * @ticket 21760
    142      */
    143     function test_get_term_by_name_cache() {
    144         global $wpdb;
    145         $term_id = $this->factory->term->create( array( 'name' => 'burrito', 'taxonomy' => 'post_tag' ) );
    146 
    147         $queries = $wpdb->num_queries;
    148         get_term_by( 'name', 'burrito', 'post_tag' );
    149         $initial = $queries + 1;
    150         $this->assertEquals( $initial, $wpdb->num_queries );
    151         $term = get_term_by( 'name', 'burrito', 'post_tag' );
    152         $this->assertEquals( $initial, $wpdb->num_queries );
    153 
    154         $this->assertEquals( get_term( $term_id, 'post_tag' ), $term );
    155         $this->assertEquals( $initial, $wpdb->num_queries );
    156     }
    157 
    158     /**
    159      * @ticket 21760
    160      */
    161     function test_get_term_by_name_cache_update() {
    162         global $wpdb;
    163         $term_id = $this->factory->term->create( array( 'name' => 'burrito', 'taxonomy' => 'post_tag' ) );
    164 
    165         $queries = $wpdb->num_queries;
    166         get_term_by( 'name', 'burrito', 'post_tag' );
    167         $initial = $queries + 1;
    168         $this->assertEquals( $initial, $wpdb->num_queries );
    169         $term = get_term_by( 'name', 'burrito', 'post_tag' );
    170         $this->assertEquals( $initial, $wpdb->num_queries );
    171 
    172         wp_update_term( $term_id, 'post_tag', array( 'slug' => 'Taco' ) );
    173         $this->assertNotEquals( $term, get_term( $term_id, 'post_tag' ) );
    174         $after_queries = $wpdb->num_queries;
    175         get_term_by( 'name', 'burrito', 'post_tag' );
    176         $this->assertEquals( $after_queries, $wpdb->num_queries );
    177     }
    178 
    179     /**
    180      * @ticket 21760
    181      */
    182     function test_invalidating_term_caches_should_fail_when_invalidation_is_suspended() {
    183         $slug = 'taco';
    184         $name = 'Taco';
    185         $taxonomy = 'post_tag';
    186         $cache_key_slug = $slug;
    187         $cache_key_name = $name;
    188 
    189         $term_id = $this->factory->term->create( array( 'slug' => $slug, 'name' => $name, 'taxonomy' => $taxonomy ) );
    190 
    191         $last_changed = wp_cache_get( 'last_changed', 'terms' );
    192 
    193         $term = get_term_by( 'slug', $slug, $taxonomy );
    194 
    195         // Verify the term is cached by ID, slug and name
    196         $this->assertEquals( $term, wp_cache_get( $term_id, $taxonomy . ':terms:' . wp_cache_get( 'last_changed', 'terms' ) ) );
    197         $this->assertSame( $term_id, wp_cache_get( $cache_key_slug, $taxonomy . ':slugs:' . wp_cache_get( 'last_changed', 'terms' ) ) );
    198         $this->assertSame( $term_id, wp_cache_get( $cache_key_name, $taxonomy . ':names:' . wp_cache_get( 'last_changed', 'terms' ) ) );
    199 
    200         $suspend = wp_suspend_cache_invalidation();
    201         clean_term_cache( $term_id, $taxonomy );
    202 
    203         // Verify that the cached value still matches the correct value
    204         $this->assertEquals( $term, wp_cache_get( $term_id, $taxonomy . ':terms:' . wp_cache_get( 'last_changed', 'terms' ) ) );
    205         $this->assertSame( $term_id, wp_cache_get( $cache_key_slug, $taxonomy . ':slugs:' . wp_cache_get( 'last_changed', 'terms' ) ) );
    206         $this->assertSame( $term_id, wp_cache_get( $cache_key_name, $taxonomy . ':names:' . wp_cache_get( 'last_changed', 'terms' ) ) );
    207 
    208         // Verify that last changed has not been updated as part of an invalidation routine
    209         $this->assertSame( $last_changed, wp_cache_get( 'last_changed', 'terms' ) );
    210 
    211         // Clean up.
    212         wp_suspend_cache_invalidation( $suspend );
    213     }
    21496}
Note: See TracChangeset for help on using the changeset viewer.