WordPress.org

Make WordPress Core

Changeset 27101


Ignore:
Timestamp:
02/06/2014 01:40:05 AM (7 years ago)
Author:
wonderboymusic
Message:

Add cache invalidation when updating a term, example: create a category, assign it to a post, edit the category. Currently, the post's term cache is not updated. When updating terms in a given taxonomy, invalidate the object term caches linked to that taxonomy.

Introduce get_taxonomy_last_changed(), set_taxonomy_last_changed(), and post_taxonomy_is_fresh().

post_taxonomy_is_fresh() is only called in get_object_term_cache() - at which point the taxonomy's last_changed value is checked against the post's {$taxonomy}_last_changed value.

set_taxonomy_last_changed() is called whenever directory database queries are made that insert new terms or affect existing terms.

Fixes #22526.

Location:
trunk
Files:
2 edited

Legend:

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

    r27057 r27101  
    18771877        $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) );
    18781878        do_action( 'edited_term_taxonomies', $edit_tt_ids );
     1879        set_taxonomy_last_changed( $taxonomy );
    18791880    }
    18801881
     
    19111912
    19121913    clean_term_cache($term, $taxonomy);
     1914    set_taxonomy_last_changed( $taxonomy );
    19131915
    19141916    do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term );
     
    21632165            $wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) );
    21642166            do_action( 'edited_terms', $alias->term_id, $taxonomy );
     2167            set_taxonomy_last_changed( $taxonomy );
    21652168        }
    21662169    }
     
    22242227
    22252228    clean_term_cache($term_id, $taxonomy);
     2229    set_taxonomy_last_changed( $taxonomy );
    22262230
    22272231    do_action("created_term", $term_id, $tt_id, $taxonomy);
     
    23302334
    23312335    wp_cache_delete( $object_id, $taxonomy . '_relationships' );
     2336    set_taxonomy_last_changed( $taxonomy );
    23322337
    23332338    do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids);
     
    24082413        do_action( 'deleted_term_relationships', $object_id, $tt_ids );
    24092414        wp_update_term_count( $tt_ids, $taxonomy );
     2415        set_taxonomy_last_changed( $taxonomy );
    24102416
    24112417        return (bool) $deleted;
     
    25662572            $wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) );
    25672573            do_action( 'edited_terms', $alias->term_id, $taxonomy );
     2574            set_taxonomy_last_changed( $taxonomy );
    25682575        }
    25692576    }
     
    26012608
    26022609    clean_term_cache($term_id, $taxonomy);
     2610    set_taxonomy_last_changed( $taxonomy );
    26032611
    26042612    do_action("edited_term", $term_id, $tt_id, $taxonomy);
     
    27392747    $taxonomies = get_object_taxonomies( $object_type );
    27402748
    2741     foreach ( $object_ids as $id )
    2742         foreach ( $taxonomies as $taxonomy )
     2749    foreach ( $object_ids as $id ) {
     2750        foreach ( $taxonomies as $taxonomy ) {
    27432751            wp_cache_delete($id, "{$taxonomy}_relationships");
     2752            set_taxonomy_last_changed( $taxonomy );
     2753        }
     2754    }
    27442755
    27452756    do_action('clean_object_term_cache', $object_ids, $object_type);
     
    28012812
    28022813        do_action('clean_term_cache', $ids, $taxonomy);
     2814        set_taxonomy_last_changed( $taxonomy );
    28032815    }
    28042816
     
    28202832 */
    28212833function get_object_term_cache($id, $taxonomy) {
     2834    if ( ! post_taxonomy_is_fresh( $id, $taxonomy ) ) {
     2835        return false;
     2836    }
    28222837    $cache = wp_cache_get($id, "{$taxonomy}_relationships");
    28232838    return $cache;
     
    31183133        do_action( 'edited_term_taxonomy', $term, $taxonomy );
    31193134    }
     3135    set_taxonomy_last_changed( $taxonomy->name );
    31203136}
    31213137
     
    31433159        do_action( 'edited_term_taxonomy', $term, $taxonomy );
    31443160    }
     3161    set_taxonomy_last_changed( $taxonomy->name );
    31453162}
    31463163
     
    34703487    return $parent;
    34713488}
     3489
     3490/**
     3491 * Retrieve the 'last_changed' value for the passed taxonomy. Retrieves
     3492 *  from cache, if present
     3493 *
     3494 * @since 3.9.0
     3495 *
     3496 * @param string $taxonomy
     3497 * @return int
     3498 */
     3499function get_taxonomy_last_changed( $taxonomy ) {
     3500    $last_changed = wp_cache_get( 'last_changed', $taxonomy );
     3501    if ( ! $last_changed ) {
     3502        $last_changed = microtime();
     3503        wp_cache_set( 'last_changed', $last_changed, $taxonomy );
     3504    }
     3505    return $last_changed;
     3506}
     3507
     3508/**
     3509 * Reset 'last_changed' for the passed taxonomy
     3510 *
     3511 * @since 3.9.0
     3512 *
     3513 * @param string $taxonomy
     3514 * @return int
     3515 */
     3516function set_taxonomy_last_changed( $taxonomy ) {
     3517    wp_cache_delete( 'last_changed', $taxonomy );
     3518    return get_taxonomy_last_changed( $taxonomy );
     3519}
     3520
     3521/**
     3522 * Determine if a post's cache for the passed taxonomy
     3523 *  is in sync.
     3524 * @since 3.9.0
     3525 *
     3526 * @param int $id
     3527 * @param string $taxonomy
     3528 * @return boolean
     3529 */
     3530function post_taxonomy_is_fresh( $id, $taxonomy ) {
     3531    $last_changed = get_taxonomy_last_changed( $taxonomy );
     3532    $post_last_changed = wp_cache_get( $id, $taxonomy . '_last_changed' );
     3533    if ( ! $post_last_changed || $last_changed !== $post_last_changed ) {
     3534        wp_cache_set( $id, $last_changed, $taxonomy . '_last_changed' );
     3535        return false;
     3536    }
     3537    return true;
     3538}
  • trunk/tests/phpunit/tests/term.php

    r27099 r27101  
    572572     * @ticket 22526
    573573     */
     574    function test_get_taxonomy_last_changed() {
     575        $last_changed = get_taxonomy_last_changed( 'category' );
     576        $last_changed_cache = wp_cache_get( 'last_changed', 'category' );
     577        $this->assertEquals( $last_changed, $last_changed_cache );
     578        wp_cache_delete( 'last_changed', 'category' );
     579        $this->assertEquals( $last_changed, $last_changed_cache );
     580        $last_changed = get_taxonomy_last_changed( 'category' );
     581        $this->assertNotEquals( $last_changed, $last_changed_cache );
     582
     583        $last_changed2 = get_taxonomy_last_changed( 'category' );
     584        $this->factory->category->create();
     585        $last_changed3 = get_taxonomy_last_changed( 'category' );
     586        $this->assertNotEquals( $last_changed2, $last_changed3 );
     587    }
     588   
     589    /**
     590     * @ticket 22526
     591     */
     592    function test_set_taxonomy_last_changed() {
     593        $last_changed1 = set_taxonomy_last_changed( 'category' );
     594        $last_changed2 = set_taxonomy_last_changed( 'category' );
     595        $this->assertNotEquals( $last_changed1, $last_changed2 );
     596
     597        $last_changed3 = set_taxonomy_last_changed( 'category' );
     598        $last_changed4 = get_taxonomy_last_changed( 'category' );
     599        $this->assertEquals( $last_changed3, $last_changed4 );
     600    }
     601
     602    /**
     603     * @ticket 22526
     604     */
     605    function test_post_taxonomy_is_fresh() {
     606        $post_id = $this->factory->post->create();
     607        $term_id = $this->factory->category->create( array( 'name' => 'Foo' ) );
     608        wp_set_post_categories( $post_id, $term_id );
     609
     610        $this->assertFalse( post_taxonomy_is_fresh( $post_id, 'category' ) );
     611        $this->assertTrue( post_taxonomy_is_fresh( $post_id, 'category' ) );
     612        $this->assertTrue( post_taxonomy_is_fresh( $post_id, 'category' ) );
     613
     614        wp_update_term( $term_id, 'category', array( 'name' => 'Bar' ) );
     615
     616        $this->assertFalse( post_taxonomy_is_fresh( $post_id, 'category' ) );
     617        get_the_category( $post_id );
     618        $this->assertTrue( post_taxonomy_is_fresh( $post_id, 'category' ) );
     619    }
     620
     621    /**
     622     * @ticket 22526
     623     */
    574624    function test_category_name_change() {
    575625        $term = $this->factory->category->create_and_get( array( 'name' => 'Foo' ) );
Note: See TracChangeset for help on using the changeset viewer.